Advertisement
Mojokojo69

light

Sep 8th, 2023 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. -- Initialize variables
  2. local running = false
  3. local pulseDuration = 0.5
  4. local timeBetweenOutputs = 0.5
  5. local pulseCount = 3
  6. local channel = 11
  7. local modem = peripheral.find("modem")
  8. local stateFile = "state.txt"
  9.  
  10. modem.open(channel)
  11.  
  12. local function resetAllOutputs()
  13. redstone.setOutput("left", false)
  14. redstone.setOutput("back", false)
  15. redstone.setOutput("right", false)
  16. redstone.setOutput("top", false)
  17. end
  18.  
  19. local function saveState(side)
  20. local file = fs.open(stateFile, "w")
  21. file.write(side)
  22. file.close()
  23. end
  24.  
  25. local function readState()
  26. if fs.exists(stateFile) then
  27. local file = fs.open(stateFile, "r")
  28. local state = file.readAll()
  29. file.close()
  30. return state
  31. end
  32. return nil
  33. end
  34.  
  35. local function sendPulse(side)
  36. saveState(side)
  37. for i = 1, pulseCount do
  38. redstone.setOutput(side, true)
  39. sleep(pulseDuration)
  40. redstone.setOutput(side, false)
  41. sleep(pulseDuration)
  42. end
  43. end
  44.  
  45. local function runSequence()
  46. local startingState = readState()
  47. local sides = {"left", "back", "right", "top"}
  48. local start = 1
  49.  
  50. for i, side in ipairs(sides) do
  51. if side == startingState then
  52. start = i
  53. break
  54. end
  55. end
  56.  
  57. resetAllOutputs()
  58.  
  59. while running do
  60. for i = start, #sides do
  61. sendPulse(sides[i])
  62. sleep(timeBetweenOutputs)
  63. end
  64. start = 1 -- Reset to the beginning for the next loop
  65. end
  66.  
  67. resetAllOutputs()
  68. fs.delete(stateFile) -- Clear saved state
  69. end
  70.  
  71. -- Main loop
  72. while true do
  73. resetAllOutputs()
  74. print("Listening for 'start' or 'stop' on channel " .. channel .. ".")
  75. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  76.  
  77. if message == "start" then
  78. running = true
  79. print("Starting sequence.")
  80. parallel.waitForAny(runSequence, function()
  81. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  82. if message == "stop" then
  83. running = false
  84. print("Stopping sequence.")
  85. end
  86. end)
  87. elseif message == "stop" then
  88. running = false
  89. print("Stopping sequence.")
  90. end
  91. end
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement