SemlerPDX icon

Power Cycle Controller ComputerCraft

SemlerPDX | PRO | 10/16/15 08:18:41 PM UTC | 0 ⭐ | 840 👁️ | Never ⏰ | []
Lua |

5.04 KB

|

None

|

0 👍

/

0 👎

--Power Cycle Program (v3)
-- with Text Timer Countdown and Key Press Cancel Functions
-- by SemlerPDX Oct2015
 
--Usage:
-- Computer will turn on power to item(s) for desired time, then off, to better control fuel consumption.
-- Connect redstone wiring from computer to power generator, or fuel supply output i.e. multi-block tank valve,
-- or any redstone powered device such as lights or appliances.
-- MUST Change variable below for PoweredSide to match side of computer that wiring extends from.
 
--Optional Light:
-- If you would like an indicator light to go on and off with this program, set LightConnected variable to true below.
-- Also, change variable below for LightSide to match side of computer where wiring extends to the light.
-- (cannot be same as power output side)
local LightConnected = false
local LightSide = "top" --MUST change to desired output side if used (top,bottom,left,right,back)
--------------------
 
--Customizable Power Side:
local PoweredSide = "bottom" --MUST change to desired output side (top,bottom,left,right,back)
 
 
-- REQUIRED VARS --
local args = {...}
local RequestedHours = args[1] or 0
local RequestedMinutes = math.ceil(RequestedHours*60)
local RemainingSeconds = math.floor(RequestedHours*60*60)
local FunctionEnabled = 0
 
 
-- F-ACK --
if #args ~= 1 then
  print("<error419: syntax>")
  print("<ex: Power 4.75  (starts 4 hour 45 minute cycle)>")
  return
end
 
if LightSide == PoweredSide then
  print("<error420: lines 16 and 20 - same side>")
  print("<PoweredSide = "..tostring(PoweredSide).." and LightSide = "..tostring(LightSide)..">")
  return
end
 
if RequestedMinutes >= 1441 then
  print("<error421: power cycle duration too long>")
  print("<set requested hours to 24 or less>")
  return
end
 
if RequestedMinutes <= 0.9999 then
  print("<error422: power cycle duration too short>")
  print("<set requested hours more than zero minutes>")
  return
end
 
 
-- FUNCS --
local function fset()
  term.clear()
  term.setCursorPos(1,1)
end
 
local function ftimer(RemainingSeconds)
  local H = math.floor(RemainingSeconds/3600)
  local M = math.floor((RemainingSeconds % 3600)/60)
  local S = RemainingSeconds % 60
  fset()
  print(tostring(RequestedHours).." Hour Power Cycle Active")
  if H >= 1 then
    if H <= 9 then
      if M <= 9 then
        if S <= 9 then
          print("Time Remaining: 0",H,":0",M,":0",S,"")
        else         
          print("Time Remaining: 0",H,":0",M,":",S,"")
        end
      else
        if S <= 9 then
          print("Time Remaining: 0",H,":",M,":0",S,"")
        else
          print("Time Remaining: 0",H,":",M,":",S,"")
        end
      end
    else
      if M <= 9 then
        if S <= 9 then
          print("Time Remaining: ",H,":0",M,":0",S,"")
        else
          print("Time Remaining: ",H,":0",M,":",S,"")
        end
      else
        if S <= 9 then
          print("Time Remaining: ",H,":",M,":0",S,"")
        else
          print("Time Remaining: ",H,":",M,":",S,"")
        end
      end
    end
  else
    if M <= 9 then
      if S <= 9 then
        print("Time Remaining: 00:0",M,":0",S,"")
      else
        print("Time Remaining: 00:0",M,":",S,"")
      end
    else
      if S <= 9 then
        print("Time Remaining: 00:",M,":0",S,"")
      else
        print("Time Remaining: 00:",M,":",S,"")
      end
    end
  end
  print("(press any key to terminate)")
end
 
 
-- POWER ON --
redstone.setOutput(PoweredSide,true)
if LightConnected then
  redstone.setOutput(LightSide,true)
end
 
----  Main Loop  ----
 
while RemainingSeconds ~= 0 do
 
  local function fcancel()
    local event, key = os.pullEvent("key")
  end
 
  local function fmain()
    ftimer(RemainingSeconds)
    RemainingSeconds = RemainingSeconds - 1
    sleep(1)
  end
 
  FunctionEnabled = parallel.waitForAny(fcancel,fmain)
 
  if FunctionEnabled == 1 then
    textutils.slowPrint("Cancelling Power Cycle...", 15)
    sleep(1.65)
    break
  end
 
end
 
-- POWER OFF --
redstone.setOutput(PoweredSide,false)
if LightConnected then
  redstone.setOutput(LightSide,false)
end
 
 
-- Evaluate Ending --
if FunctionEnabled == 1 then    --(1 = functions cancelled, 2 = standard function looped)
  local ElapsedHours = math.floor((RequestedHours*60*60 - RemainingSeconds)/3600)
  if ElapsedHours >= 1 then
    print(tostring(RequestedHours).." hour Power Cycle cancelled after "..tostring(ElapsedHours).." hour(s)")
    print("")
    sleep(0.75)
  else
    local ElapsedMinutes = math.ceil((RequestedHours*60*60 - RemainingSeconds)/60)
    print(tostring(RequestedHours).." hour Power Cycle cancelled after "..tostring(ElapsedMinutes).." minute(s)")
    print("")
    sleep(0.75)
  end
else
  textutils.slowPrint("Completing Power Cycle...", 13)
  sleep(2)
  fset()
  if RequestedMinutes >= 60 then
    print(tostring(RequestedHours).." hour Power Cycle completed")
    print("")
    sleep(0.75)
  else
    print(tostring(RequestedMinutes).." minute Power Cycle completed")
    print("")
    sleep(0.75)
  end
end

Comments