Advertisement
mariojuggernaut

Redstone timer code

Apr 21st, 2020
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. --Thanks to Forecaster for writing this for me--
  2. local sides = require("sides")
  3. local event = require("event")
  4. local computer = require("computer")
  5.  
  6. local allowAnySide = true
  7. local startSide = sides.north
  8. local stopSide = sides.south
  9.  
  10. local startTime = nil
  11.  
  12. function timeFormat(seconds)
  13. seconds = tonumber(seconds)
  14.  
  15. if seconds <= 0 then
  16. return "00:00:00";
  17. else
  18. hours = string.format("%02.f", math.floor(seconds/3600));
  19. mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
  20. secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60));
  21. return hours..":"..mins..":"..secs
  22. end
  23. end
  24.  
  25. local function onStart()
  26. if startTime == nil then
  27. startTime = computer.uptime()
  28. print("Timer is running...")
  29. end
  30. end
  31.  
  32. local function onStop()
  33. local time = computer.uptime()
  34. if startTime ~= nil then
  35. local diff = time - startTime
  36. print("Stop! Duration: " .. timeFormat(diff))
  37. startTime = nil
  38. end
  39. end
  40.  
  41. local function onRedstone(e, address, side, oldValue, newValue)
  42. if oldValue == 0 then
  43. local signal = false
  44. if (allowAnySide == true and startTime == nil) or side == startSide then
  45. onStart()
  46. signal = true
  47. else
  48. if (allowAnySide == true and startTime ~= nil) or side == stopSide then
  49. onStop()
  50. signal = true
  51. end
  52. end
  53. if signal == false then
  54. if sides[side] ~= nil then
  55. side = sides[side]
  56. end
  57. print("Received redstone signal on side " .. side .. " matching no inputs.")
  58. end
  59. end
  60. end
  61.  
  62. local run = true
  63. local function onInterrupt()
  64. run = false
  65. end
  66.  
  67. event.listen("redstone_changed", onRedstone)
  68. event.listen("interrupted", onInterrupt)
  69.  
  70. print("Program started, waiting for input...")
  71.  
  72. while run do
  73. os.sleep(1)
  74. end
  75.  
  76. event.ignore("redstone_changed", onRedstone)
  77. event.ignore("interrupted", onInterrupt)
  78.  
  79. print("Program stopped")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement