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

I made a Timeprogram but it doesn´t work!

Started by Keego, 13 December 2012 - 06:37 AM
Keego #1
Posted 13 December 2012 - 07:37 AM
Hello I made a program that the RedstoneLightstoneblocks are glowing when it gets 8:00 PM in minecraft but it doesn´t work and i cant stop the program. And the same program when it has to stop. Thanks for helping me :)/> !

while true do
term.clear()
time = textutils.formatTime(os.time(), false)
if time == "8:00 PM" then
rs.setOutput("left", true)
end
shell.run("time")
sleep(10)
shell.run("clear")
end

while true do
term.clear()
time = textutils.formatTime(os.time(), false)
if time == "6:00 AM" then
rs.setOutput("left", false)
end
shell.run("time")
sleep(10)
shell.run("clear")
end
PixelToast #2
Posted 13 December 2012 - 08:01 AM

if condition then
break
end

that will exit out of the innermost loop
you can also hold down ctrl+t to stop a program thats not responding

and can you please make your sig smaller .-.
huettner94 #3
Posted 13 December 2012 - 08:04 AM
What are the programs time and clear?
Keego #4
Posted 13 December 2012 - 09:08 AM
PixelToast your help is good but my program still won´t work . :(/>
PixelToast #5
Posted 13 December 2012 - 09:13 AM
PixelToast your help is good but my program still won´t work . :(/>
when are you trying to make it exit?
Kingdaro #6
Posted 13 December 2012 - 09:22 AM
The main problem, is that you have a sleep(10) in your script, which pauses for 10 seconds each loop, and more than likely, it's going to bypass 8 and 6 during that time. It's better to check if it's within a certain range, instead of if it's the time exactly.

For the record, converting the time with textutils.formatTime isn't necessary, as the time is in hours of the day, 0-24.

Here's a nice illustration:

-----|-------------|----
     6 AM          8 PM
light     light     light
on        off       on

Because hours reset right after 23, you'll have to check instead if it's greater than 20 (or 8 PM) or less than 6 (6 AM).


while true do
  time = os.time()
  if time > 6 and time < 20 then
    rs.setOutput('left', false)
  elseif time < 6 or time > 20 then
    rs.setOutput('left', true)
  end
  sleep(1)
end
Keego #7
Posted 13 December 2012 - 09:26 AM
Thank you very much Kingdaro. But i tried everything and i still won´t work :(/>.
GopherAtl #8
Posted 13 December 2012 - 11:47 AM
:sigh: here, try this, wrote it a long time ago when learning cc, just tweaked it a bit and added comments for you…

--make some vars to let us easily adjust the times we want the lights to go on and off
local lightsOffTime=6  --6am
local lightsOnTime=20  --8pm (8 + 12)

--this function takes one parameter, time, and waits until that time to return
local function waitUntilTime(time)
	--make an alarm set for time
	local alarm=os.setAlarm(time)
	
	while true do
		--wait for the alarm event
		local eventType, param=os.pullEvent("alarm")
		--is it MY alarm?
		if param==alarm then
			return   --yawp, exit
		end
	end
end

--necessary to give the computer's clock time to set when run from startup
--on bootup it's 0, but it corrects after first yield
os.sleep(0)

--grab current time
local timeNow=os.time()
--if it's later than lightsOffTime and earlier than lightsOnTime...
if timeNow>=lightsOffTime and timeNow<=lightsOnTime then
	--...then it's day now, wait for night
	waitUntilTime(lightsOnTime)
end

rs.setOutput("left", true)	

--start our loop
while true do
	--wait for day
	waitUntilTime(lightsOffTime)
	rs.setOutput("left",false)

	--wait for night
	waitUntilTime(lightsOnTime)
	rs.setOutput("left",true)
end

if you want to be able to use the computer for other stuff while that runs in the background, save it as "lightControl" and put this in your startup script. If you have anything else in startup, this must be last! Anything after this in startup won't happen at all.

term.clear()
term.setCursorPos(1,1)
parallel.waitForAny(
	function() shell.run("shell") end,
	function() shell.run("lightControl") end
  )
Keego #9
Posted 14 December 2012 - 03:54 AM
:(/> I am sorry about that Gopher but I dont know why it doesn´t want to work. I tried your script but it won´t work :/ .
Lyqyd #10
Posted 14 December 2012 - 04:50 AM
Well, "doesn't work" isn't a useful error message. What error are you receiving? Alternatively, what does it do when you run the code, if it doesn't error? Do you have your redstone output on the side of the computer that the script specifies?
Keego #11
Posted 14 December 2012 - 08:06 AM
I dont get a error it just won´t work!
Kingdaro #12
Posted 14 December 2012 - 09:15 AM
Post your current script.
GopherAtl #13
Posted 14 December 2012 - 09:25 AM
I tested the code before I posted it. It works perfectly.
PixelToast #14
Posted 14 December 2012 - 09:29 AM
the OP needs to clarify what he is trying to do

what does the code do not do that you want it to
Keego #15
Posted 15 December 2012 - 02:11 AM
I use the script from Gopher.
Lyqyd #16
Posted 15 December 2012 - 03:44 AM
Which side of your computer is the redstone dust that controls the lights connected to?