Advertisement
SemlerPDX

Power Cycle Controller ComputerCraft

Oct 16th, 2015
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.04 KB | None | 0 0
  1. --Power Cycle Program (v3)
  2. -- with Text Timer Countdown and Key Press Cancel Functions
  3. -- by SemlerPDX Oct2015
  4.  
  5. --Usage:
  6. -- Computer will turn on power to item(s) for desired time, then off, to better control fuel consumption.
  7. -- Connect redstone wiring from computer to power generator, or fuel supply output i.e. multi-block tank valve,
  8. -- or any redstone powered device such as lights or appliances.
  9. -- MUST Change variable below for PoweredSide to match side of computer that wiring extends from.
  10.  
  11. --Optional Light:
  12. -- If you would like an indicator light to go on and off with this program, set LightConnected variable to true below.
  13. -- Also, change variable below for LightSide to match side of computer where wiring extends to the light.
  14. -- (cannot be same as power output side)
  15. local LightConnected = false
  16. local LightSide = "top" --MUST change to desired output side if used (top,bottom,left,right,back)
  17. --------------------
  18.  
  19. --Customizable Power Side:
  20. local PoweredSide = "bottom" --MUST change to desired output side (top,bottom,left,right,back)
  21.  
  22.  
  23. -- REQUIRED VARS --
  24. local args = {...}
  25. local RequestedHours = args[1] or 0
  26. local RequestedMinutes = math.ceil(RequestedHours*60)
  27. local RemainingSeconds = math.floor(RequestedHours*60*60)
  28. local FunctionEnabled = 0
  29.  
  30.  
  31. -- F-ACK --
  32. if #args ~= 1 then
  33.   print("<error419: syntax>")
  34.   print("<ex: Power 4.75  (starts 4 hour 45 minute cycle)>")
  35.   return
  36. end
  37.  
  38. if LightSide == PoweredSide then
  39.   print("<error420: lines 16 and 20 - same side>")
  40.   print("<PoweredSide = "..tostring(PoweredSide).." and LightSide = "..tostring(LightSide)..">")
  41.   return
  42. end
  43.  
  44. if RequestedMinutes >= 1441 then
  45.   print("<error421: power cycle duration too long>")
  46.   print("<set requested hours to 24 or less>")
  47.   return
  48. end
  49.  
  50. if RequestedMinutes <= 0.9999 then
  51.   print("<error422: power cycle duration too short>")
  52.   print("<set requested hours more than zero minutes>")
  53.   return
  54. end
  55.  
  56.  
  57. -- FUNCS --
  58. local function fset()
  59.   term.clear()
  60.   term.setCursorPos(1,1)
  61. end
  62.  
  63. local function ftimer(RemainingSeconds)
  64.   local H = math.floor(RemainingSeconds/3600)
  65.   local M = math.floor((RemainingSeconds % 3600)/60)
  66.   local S = RemainingSeconds % 60
  67.   fset()
  68.   print(tostring(RequestedHours).." Hour Power Cycle Active")
  69.   if H >= 1 then
  70.     if H <= 9 then
  71.       if M <= 9 then
  72.         if S <= 9 then
  73.           print("Time Remaining: 0",H,":0",M,":0",S,"")
  74.         else        
  75.           print("Time Remaining: 0",H,":0",M,":",S,"")
  76.         end
  77.       else
  78.         if S <= 9 then
  79.           print("Time Remaining: 0",H,":",M,":0",S,"")
  80.         else
  81.           print("Time Remaining: 0",H,":",M,":",S,"")
  82.         end
  83.       end
  84.     else
  85.       if M <= 9 then
  86.         if S <= 9 then
  87.           print("Time Remaining: ",H,":0",M,":0",S,"")
  88.         else
  89.           print("Time Remaining: ",H,":0",M,":",S,"")
  90.         end
  91.       else
  92.         if S <= 9 then
  93.           print("Time Remaining: ",H,":",M,":0",S,"")
  94.         else
  95.           print("Time Remaining: ",H,":",M,":",S,"")
  96.         end
  97.       end
  98.     end
  99.   else
  100.     if M <= 9 then
  101.       if S <= 9 then
  102.         print("Time Remaining: 00:0",M,":0",S,"")
  103.       else
  104.         print("Time Remaining: 00:0",M,":",S,"")
  105.       end
  106.     else
  107.       if S <= 9 then
  108.         print("Time Remaining: 00:",M,":0",S,"")
  109.       else
  110.         print("Time Remaining: 00:",M,":",S,"")
  111.       end
  112.     end
  113.   end
  114.   print("(press any key to terminate)")
  115. end
  116.  
  117.  
  118. -- POWER ON --
  119. redstone.setOutput(PoweredSide,true)
  120. if LightConnected then
  121.   redstone.setOutput(LightSide,true)
  122. end
  123.  
  124. ----  Main Loop  ----
  125.  
  126. while RemainingSeconds ~= 0 do
  127.  
  128.   local function fcancel()
  129.     local event, key = os.pullEvent("key")
  130.   end
  131.  
  132.   local function fmain()
  133.     ftimer(RemainingSeconds)
  134.     RemainingSeconds = RemainingSeconds - 1
  135.     sleep(1)
  136.   end
  137.  
  138.   FunctionEnabled = parallel.waitForAny(fcancel,fmain)
  139.  
  140.   if FunctionEnabled == 1 then
  141.     textutils.slowPrint("Cancelling Power Cycle...", 15)
  142.     sleep(1.65)
  143.     break
  144.   end
  145.  
  146. end
  147.  
  148. -- POWER OFF --
  149. redstone.setOutput(PoweredSide,false)
  150. if LightConnected then
  151.   redstone.setOutput(LightSide,false)
  152. end
  153.  
  154.  
  155. -- Evaluate Ending --
  156. if FunctionEnabled == 1 then    --(1 = functions cancelled, 2 = standard function looped)
  157.   local ElapsedHours = math.floor((RequestedHours*60*60 - RemainingSeconds)/3600)
  158.   if ElapsedHours >= 1 then
  159.     print(tostring(RequestedHours).." hour Power Cycle cancelled after "..tostring(ElapsedHours).." hour(s)")
  160.     print("")
  161.     sleep(0.75)
  162.   else
  163.     local ElapsedMinutes = math.ceil((RequestedHours*60*60 - RemainingSeconds)/60)
  164.     print(tostring(RequestedHours).." hour Power Cycle cancelled after "..tostring(ElapsedMinutes).." minute(s)")
  165.     print("")
  166.     sleep(0.75)
  167.   end
  168. else
  169.   textutils.slowPrint("Completing Power Cycle...", 13)
  170.   sleep(2)
  171.   fset()
  172.   if RequestedMinutes >= 60 then
  173.     print(tostring(RequestedHours).." hour Power Cycle completed")
  174.     print("")
  175.     sleep(0.75)
  176.   else
  177.     print(tostring(RequestedMinutes).." minute Power Cycle completed")
  178.     print("")
  179.     sleep(0.75)
  180.   end
  181. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement