luaminecraftcomputercraft

computercraft/lua program looping unable to maintain signal output


I'm trying to write a program that gets the time from the os.time() function and use that to change a redstone output. When rebooting, the program is set to auto-run, but since I reboot to cause the program to restart, it interrupts then starts the code again. I have already tried loops in several places and forms to update the time variable without rebooting, but to no avail. Any help would be appreciated. (I'm still open to a solution with loops if it will work)

Code:

shell.run("time") 
x = os.time()
print(x)
if x > 18.500 then
  redstone.setOutput("left", true)
elseif x < 6.500 then
  redstone.setOutput("left",true)
else redstone.setOutput("left",false)
end
sleep(2)
os.reboot()

Solution

  • you shouldnt need the os.reboot(), your code should simply look like this:

    while true
    shell.run("time") 
    x = os.time()
    print(x)
    if x > 18.500 then
      redstone.setOutput("left", true)
    elseif x < 6.500 then
      redstone.setOutput("left",true)
    else redstone.setOutput("left",false)
    end
    sleep(2)
    end