This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Avention's profile picture

Redstone Output at Night

Started by Avention, 28 January 2016 - 01:18 AM
Avention #1
Posted 28 January 2016 - 02:18 AM
I am currently trying to write a program that checks if it is day or night, toggles a redstone output, then reboots and runs the program again (program is saved in startup) as to continuously check the time. It doesn't seem to be working, not sure why. Here's the code:


local time = os.time()

local function day()
redstone.setOutput("left", false)
sleep(1)
os.reboot()
end

local function night()
redstone.setOutput("left", true)
sleep(1)
os.reboot()
end

if time < 6 and time > 18 then
night()

else

day()

end

Any suggestions on how to fix this?
KingofGamesYami #2
Posted 28 January 2016 - 05:19 AM
Well, every time you reboot the computer the redstone is reset… so you shouldn't be doing that. Actually, you shouldn't be doing that anyway - using an infinite loop is much better.


while true do
  local time = os.time()
  if time < 6 and time > 18 then
    night()
  else
    day()
  end
end

..oh, and time will never be less than 6 and greater than 18. Might want to flip your comparison operators around.
Edited on 28 January 2016 - 04:19 AM
Bomb Bloke #3
Posted 28 January 2016 - 05:22 AM
Would it not be easier to right-click a daylight sensor, inverting its signal?
Lyqyd #4
Posted 28 January 2016 - 03:50 PM
Alternatively, one could use os.setAlarm to generate events at the correct times to make the changes and remain yielded at all other times.
Avention #5
Posted 28 January 2016 - 07:19 PM
Thanks for all of your help! The infinite loop is definitely much better than whatever I was trying to do.