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

So I Want A Program That Will Turn Lights On At Certain Times

Started by GeniusName3, 24 November 2013 - 03:35 AM
GeniusName3 #1
Posted 24 November 2013 - 04:35 AM
I'm confused on how to use the tonumber on this it says I need to add then on the if statement,but it already says then

time = os.time()
time = textutils.formatTime(time, false)
if time > 18:00 then
rs.setOutput(bottom,true)
end
if time 18:00 then
rs.setOutput(bottom,false)
end
Roboguy99 #2
Posted 24 November 2013 - 05:39 AM
You need to format the time differently. The computer reads it as a string of numbers (I think something more like if time > 18000 then is needed). You could find out by printing time and then formatting it the same way.

EDIT: The computer reads it as a long integer, not a string like you've tried to use. There is a way of converting it but you don't need it as you're not making a visual clock.
EDIT 2: I really should read people's code before posting! You don't need to format your time, just do it with the unformatted number. Read the guy below's post, he hasn' made a mess of explaining.
Edited on 24 November 2013 - 04:54 AM
theoriginalbit #3
Posted 24 November 2013 - 05:47 AM
textutils.formatTime returns a string, not a number you need to do this


if time >= "18:00" then
  --# its after 6pm but before midnight in-game
else
  --# its after midnight but before 6pm in-game
end
MKlegoman357 #4
Posted 24 November 2013 - 07:45 AM
And for your error you get:


if time 18:00 then --// Here you are missing <= and "
  rs.setOutput(bottom,false)
end

--//Should be:

if time <= "18:00" then
  rs.setOutput(bottom,false)
end

But the problem is that if time is 3 AM your lights will turn off because it checks if time is greater than 18:00. You then would have to check if it is less than 6:00 or your own desired time of morning:


time = os.time()
time = textutils.formatTime(time, true) --// We want it to return in 24 hour format, not 12 hour format

if time > "18:00" or time < "6:00" then
  rs.setOutput(bottom,true)
else
  rs.setOutput(bottom,false)
end  

PS: didn't test that code.
Lyqyd #5
Posted 24 November 2013 - 02:08 PM
If you want a program that will constantly run and turn lights on and off at certain times, you probably want to use os.setAlarm instead of trying to check the time constantly.