Advertisement
Himeki

DoorClock

Jul 7th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.19 KB | None | 0 0
  1. --Predeclare Functions
  2. local clear
  3. local rnd
  4. local dbug
  5. local doSunrise
  6. local doSunset
  7.  
  8. --Variable defaults and settings
  9. local debugmode = false --set to true for debug readout
  10. local mon = peripheral.wrap( "left" )
  11. local side = "right" --redstone output side
  12. local redside = "bottom" --redstone input side
  13. local readout = "Waiting"
  14.  
  15. local time1 = 6.10
  16. local time2 = 18.00
  17. local event = "blank"
  18. local input = "blank"
  19. local timecheck
  20.  
  21. --Functions
  22.  
  23. function clear()
  24.     term.clear()
  25.     term.setCursorPos(1,1)
  26. end
  27.  
  28. function rnd()  --reset and display
  29.     clear()
  30.     mon.clear()
  31.     mon.setCursorPos(1,1)
  32.     sunrise = os.setAlarm(time1)
  33.     sunset = os.setAlarm(time2)
  34.     print(readout)
  35.     mon.write(readout)
  36. end
  37.  
  38. function dbug()
  39.     print("event:" .. event)
  40.     print("input:" .. input)
  41.     print("readout:" .. readout)
  42. end
  43.  
  44. function doSunrise()
  45.     redstone.setOutput(side, true)
  46.     sleep(0.1)
  47.     redstone.setOutput(side, false)
  48.     sleep(0.1)
  49.     redstone.setOutput(side, true)
  50.     readout = "Morning."
  51.     rnd()
  52.     if debugmode == true then
  53.         dbug()
  54.     end
  55.     print("Door Open.")
  56.     sleep(0.8)
  57. end
  58.  
  59. function doSunset()
  60.     redstone.setOutput(side, true)
  61.     sleep(0.1)
  62.     redstone.setOutput(side, false)
  63.     readout = "Night."
  64.     rnd()
  65.     if debugmode == true then
  66.         dbug()
  67.     end
  68.     print("Door Shut.")
  69.     sleep(0.9)
  70. end
  71.  
  72.  
  73. --Code Start
  74.  
  75. mon.setTextScale(1)
  76.  
  77. rnd()
  78.  
  79. --Fires once to set current time and door state
  80. timecheck = os.time()
  81. if ( timecheck >= 6.10 and timecheck < 18.00 ) then
  82.     doSunrise()
  83. elseif ( timecheck >= 18.00 or timecheck < 6.00 ) then
  84.     doSunset()
  85. end
  86.  
  87. --Main block
  88. while true do
  89.     event, input = os.pullEvent()
  90.     if event == "alarm" and input == sunrise then
  91.         doSunrise()
  92.     elseif event == "alarm" and input == sunset then
  93.         doSunset()
  94.     elseif event == "redstone" and redstone.getInput(redside) == true then
  95.         if redstone.getOutput(side) == false then
  96.             redstone.setOutput(side, true)
  97.             print("Door Open.")
  98.             sleep(1)
  99.             redstone.setOutput(side, false)
  100.             print("Door Shut.")
  101.         end
  102.     end
  103. end
  104.  
  105. clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement