Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Initialize variables
- local running = false
- local pulseDuration = 0.5
- local timeBetweenOutputs = 0.5
- local pulseCount = 3
- local channel = 11
- local modem = peripheral.find("modem")
- local stateFile = "state.txt"
- modem.open(channel)
- local function resetAllOutputs()
- redstone.setOutput("left", false)
- redstone.setOutput("back", false)
- redstone.setOutput("right", false)
- redstone.setOutput("top", false)
- end
- local function saveState(side)
- local file = fs.open(stateFile, "w")
- file.write(side)
- file.close()
- end
- local function readState()
- if fs.exists(stateFile) then
- local file = fs.open(stateFile, "r")
- local state = file.readAll()
- file.close()
- return state
- end
- return nil
- end
- local function sendPulse(side)
- saveState(side)
- for i = 1, pulseCount do
- redstone.setOutput(side, true)
- sleep(pulseDuration)
- redstone.setOutput(side, false)
- sleep(pulseDuration)
- end
- end
- local function runSequence()
- local startingState = readState()
- local sides = {"left", "back", "right", "top"}
- local start = 1
- for i, side in ipairs(sides) do
- if side == startingState then
- start = i
- break
- end
- end
- resetAllOutputs()
- while running do
- for i = start, #sides do
- sendPulse(sides[i])
- sleep(timeBetweenOutputs)
- end
- start = 1 -- Reset to the beginning for the next loop
- end
- resetAllOutputs()
- fs.delete(stateFile) -- Clear saved state
- end
- -- Main loop
- while true do
- resetAllOutputs()
- print("Listening for 'start' or 'stop' on channel " .. channel .. ".")
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- if message == "start" then
- running = true
- print("Starting sequence.")
- parallel.waitForAny(runSequence, function()
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- if message == "stop" then
- running = false
- print("Stopping sequence.")
- end
- end)
- elseif message == "stop" then
- running = false
- print("Stopping sequence.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement