Advertisement
mariojuggernaut

Redstone timer code

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