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

Question

Started by Dale310, 09 April 2012 - 06:55 PM
Dale310 #1
Posted 09 April 2012 - 08:55 PM
Hello all this is my first post on here. I am looking for someone to help me figure out how to use timers. From what I have read about them they are a one time use. Meaning I would have to run my two timers then reboot the computer to reset the program and timers. would this be correct? What I am trying to use them for is to replace my redstone circuit on my nuclear reactors with a cc program. It seems like it would be simple enough to code if I knew how to use the timers.

Any help would be appreciated. Thank you :P/>/>
Hawk777 #2
Posted 09 April 2012 - 10:16 PM
Each timer runs only once, but you can create as many of them as you want. If you want to make a program that does something every five seconds while also handling other events, consider the following code, which shows also handling keypresses:

os.startTimer(5)
while true do
  local e, p1, p2, p3 = os.pullEvent()
  if e == "timer" then
    print("Tick")
    os.startTimer(5)
  elseif e == "key" then
    print("Key ", p1)
  end
end
Dale310 #3
Posted 10 April 2012 - 05:19 AM
Here is my code the only problem i have encountered so far is that i keep getting a didn't yield long enough error and i haven't really tested the timers to see if they are running simultaneously witch would be bad or if the are running individually.
term.clear()
term.setCursorPos(1,1)
p1 = on
on = "activate reactor"
p2 = off
off = "de-activate reactor"
p3 = exit
exit = "elseif"
print("Reactor 1")
print("Mark - V")
print("")
print("Reactor Status")
if rs.getOutput("back") == false then
print("Warning Reactor Live !")
end
if rs.getOutput("back") == true then
print("Reactor is In-Active and Safe")
end
print("")
print("Generation Time 3 Minutes 30 Seconds")
print("Cooldown Time 48 Minutes 50 Seconds")
print("Maximum 2.24 % of One Cycle")
print("120 EU/t at Efficiency 4.00")
print("Total EU Generated 164,000,000")
print("")
write("Command:")
answer = read()
if answer == on then
term.clear()
term.setCursorPos(1,1)
print("Reactor is Warming Up")
sleep(5)
term.clear()
term.setCursorPos(1,1)
print("Reactor is now Active")
sleep(2)
term.clear()
term.setCursorPos(1,1)
print("Reactor 1")
print("Mark - V")
print("")
print("Reactor Status")
if rs.getOutput("back") == true then
print("Reactor is In-Active and Safe")
end
if rs.getOutput("back") == false then
print("Warning Reactor is Active !")
end
print("")
print("Generation Time 3 Minutes 30 Seconds")
print("Cooldown Time 48 Minutes 50 Seconds")
print("Maximum 2.24 % of One Cycle")
print("820 EU/t at Efficiency 4.00")
print("Total EU Generated 164,000,000")
print("")
write("Command:")
answer = read()
os.startTimer(210)
while true do
rs.setOutput("back", false)
local event, p1, p2, p3 = os.pullEvent()
os.startTimer(2930)
while true do
rs.setOutput("back", true)
local event, p1, p2, p3 = os.pullEvent()
os.reboot()
end
end
end
if answer == off then
rs.setOutput("back", true)
term.clear()
term.setCursorPos(1,1)
end
print("Warning Reactor was Unexpectedly Shutdown")
print("Reactor is In-Active but May Not be Safe")
write("Command:")
answer = read()
Hawk777 #4
Posted 10 April 2012 - 07:57 AM
Starting a timer doesn’t put your code to sleep. The “os.startTimer” function returns instantly; you would only ever want to use it in combination with “os.pullEvents”. If you just want your code to delay at a certain point, use “sleep” instead.
Dale310 #5
Posted 10 April 2012 - 08:15 AM
so a timer would be more like an alarm clock then anything?
and i made a slight change to my code
p3 = elseif
elseif = "exit"
Dale310 #6
Posted 10 April 2012 - 08:17 AM
while the sleep answers one question it still does not solve my problem of the yield error
term.clear()
term.setCursorPos(1,1)
p1 = on
on = "activate reactor"
p2 = off
off = "de-activate reactor"
p3 = exit
exit = "elseif"
print("Reactor 1")
print("Mark - V")
print("")
print("Reactor Status")
if rs.getOutput("back") == false then
print("Warning Reactor Live !")
end
if rs.getOutput("back") == true then
print("Reactor is In-Active and Safe")
end
print("")
print("Generation Time 3 Minutes 30 Seconds")
print("Cooldown Time 48 Minutes 50 Seconds")
print("Maximum 2.24 % of One Cycle")
print("820 EU/t at Efficiency 4.00")
print("Total EU Generated 164,000,000")
print("")
write("Command:")
answer = read()
if answer == on then
term.clear()
term.setCursorPos(1,1)
print("Reactor is Warming Up")
sleep(5)
term.clear()
term.setCursorPos(1,1)
print("Reactor is now Active")
sleep(2)
term.clear()
term.setCursorPos(1,1)
print("Reactor 1")
print("Mark - V")
print("")
print("Reactor Status")
if rs.getOutput("back") == true then
print("Reactor is In-Active and Safe")
end
if rs.getOutput("back") == false then
print("Warning Reactor is Active !")
end
print("")
print("Generation Time 3 Minutes 30 Seconds")
print("Cooldown Time 48 Minutes 50 Seconds")
print("Maximum 2.24 % of One Cycle")
print("820 EU/t at Efficiency 4.00")
print("Total EU Generated 164,000,000")
print("")
write("Command:")
answer = read()
sleep(210)
while true do
rs.setOutput("back", false)
local event, p1, p2, p3 = os.pullEvent()
sleep(2930)
while true do
rs.setOutput("back", true)
local event, p1, p2, p3 = os.pullEvent()
os.reboot()
end
end
end
if answer == off then
rs.setOutput("back", true)
term.clear()
term.setCursorPos(1,1)
end
print("Warning Reactor was Unexpectedly Shutdown")
print("Reactor is In-Active but May Not be Safe")
write("Command:")
answer = read()
Luanub #7
Posted 10 April 2012 - 08:25 AM
A sleep or os.pullEvent should fix the yield error. Where do you run into that problem at? What is the code doing when it errors?
Dale310 #8
Posted 10 April 2012 - 08:27 AM
sorry i only made half the changes the first time and i can see one issue with sleep the whole time the reactor is running or cooling i can not make an input to the computer so if the reactor were to need an emergency shutdown i couldn't do it. Is there even any way to go around this?
term.clear()
term.setCursorPos(1,1)
p1 = on
on = "activate reactor"
p2 = off
off = "de-activate reactor"
p3 = exit
exit = "elseif"
print("Reactor 1")
print("Mark - V")
print("")
print("Reactor Status")
if rs.getOutput("back") == false then
print("Warning Reactor Live !")
end
if rs.getOutput("back") == true then
print("Reactor is In-Active and Safe")
end
print("")
print("Generation Time 3 Minutes 30 Seconds")
print("Cooldown Time 48 Minutes 50 Seconds")
print("Maximum 2.24 % of One Cycle")
print("120 EU/t at Efficiency 4.00")
print("Total EU Generated 164,000,000")
print("")
write("Command:")
answer = read()
if answer == on then
term.clear()
term.setCursorPos(1,1)
print("Reactor is Warming Up")
sleep(5)
term.clear()
term.setCursorPos(1,1)
print("Reactor is now Active")
sleep(2)
term.clear()
term.setCursorPos(1,1)
print("Reactor 1")
print("Mark - V")
print("")
print("Reactor Status")
if rs.getOutput("back") == true then
print("Reactor is In-Active and Safe")
end
if rs.getOutput("back") == false then
print("Warning Reactor is Active !")
end
print("")
print("Generation Time 3 Minutes 30 Seconds")
print("Cooldown Time 48 Minutes 50 Seconds")
print("Maximum 2.24 % of One Cycle")
print("820 EU/t at Efficiency 4.00")
print("Total EU Generated 164,000,000")
print("")
write("Command:")
answer = read()
rs.setOutput("back", false)
sleep(210)
rs.setOutput("back", true)
sleep(2930)
local event, p1, p2, p3 = os.pullEvent()
os.reboot()
end
end
end
if answer == off then
rs.setOutput("back", true)
term.clear()
term.setCursorPos(1,1)
end
print("Warning Reactor was Unexpectedly Shutdown")
print("Reactor is In-Active but May Not be Safe")
write("Command:")
answer = read()
Dale310 #9
Posted 10 April 2012 - 08:28 AM
i get the error after i enter one command such as "activate reactor" and then while it is running if i were to want to shut it down and i go to type in a second command it errors
Luanub #10
Posted 10 April 2012 - 08:35 AM
Is this sleep required?

sleep(210)
rs.setOutput("back", true)
sleep(2930)  <---- This one
local event, p1, p2, p3 = os.pullEvent() <--- the program will stop and wait here even without the sleep.
os.reboot()

It's nearly an hour long sleep, it might be causing your problem.

the os.pullEvent() after it will cause the program to stop and wait for an event
Dale310 #11
Posted 10 April 2012 - 08:41 AM
the sleep is a cool down time i realize it is ridiculously long witch is why i was looking for another way to do it but it doesn't seem like there is such a thing. the pullEvent was there from the timer however i believe it is rendered useless now due to the removal of the timer
Luanub #12
Posted 10 April 2012 - 08:50 AM
So for your setup the program needs to reach that point and wait a specific amount of time then continue? I'm at work right now but about to head home. I'll play around with it some when I get home.
Dale310 #13
Posted 10 April 2012 - 08:55 AM
you have the idea and that would be greatly appreciated :P/>/> thank you
Luanub #14
Posted 10 April 2012 - 11:25 AM
Try this, I haven't really tested it beyond the long sleep but hopefully i've chunked it down enough that it will not error.

I did find 2 extra ends that you did not need and some vars that were undeed as well. Also put some other comments in the code that you should read.

Spoiler

term.clear()
term.setCursorPos(1,1)
--p1 = on -- not sure what these were for
on = "activate reactor"
--p2 = off
off = "de-activate reactor"
--p3 = exit  --??
--exit = "elseif"
print("Reactor 1")
print("Mark - V")
print("")
print("Reactor Status")
if rs.getOutput("back") == false then
	print("Warning Reactor Live !")
end
if rs.getOutput("back") == true then
	print("Reactor is In-Active and Safe")
end
print("")
print("Generation Time 3 Minutes 30 Seconds")
print("Cooldown Time 48 Minutes 50 Seconds")
print("Maximum 2.24 % of One Cycle")
print("120 EU/t at Efficiency 4.00")
print("Total EU Generated 164,000,000")
print("")
write("Command:")
answer = read()
if answer == on then
	term.clear()
	term.setCursorPos(1,1)
	print("Reactor is Warming Up")
	sleep(5)
	term.clear()
	term.setCursorPos(1,1)
	print("Reactor is now Active")
	sleep(2)
	term.clear()
	term.setCursorPos(1,1)
	print("Reactor 1")
	print("Mark - V")
	print("")
	print("Reactor Status")
	if rs.getOutput("back") == true then
		print("Reactor is In-Active and Safe")
	end
	if rs.getOutput("back") == false then
		print("Warning Reactor is Active !")
	end
	print("")
	print("Generation Time 3 Minutes 30 Seconds")
	print("Cooldown Time 48 Minutes 50 Seconds")
	print("Maximum 2.24 % of One Cycle")
	print("820 EU/t at Efficiency 4.00")
	print("Total EU Generated 164,000,000")
	print("")
	--write("Command:")
	--answer = read() -- what does this do? it basically acts as a press any key to continue
	-- there is nothing checking the answer and the code takes the same logical path regardless of whats entered
	-- assuming so replaced with an any key function
	print ("Press any key to continue")
	os.pullEvent("key")
	print ()
	textutils.slowWrite "Cooldown In Progress."
	rs.setOutput("back", false)
	sleep(210)
	sleep (1)
	write "."
	rs.setOutput("back", true)
	--sleep(2930) -- lets handle this a little differently...
	for x=1, 9 do
		sleep (150)
		write "."
		sleep (150)
		write "."
	end
	sleep(230)
	print ("n System is restarting")
	--local event, p1, p2, p3 = os.pullEvent() not needed
	os.reboot()
end
if answer == off then
	rs.setOutput("back", true)
	term.clear()
	term.setCursorPos(1,1)
end
--end extra end's
--end
print("Warning Reactor was Unexpectedly Shutdown") -- since there is no loops or functions being used everything from here down is pointless. the program will just exit when you enter the command.
print("Reactor is In-Active but May Not be Safe") -- There is nothing telling it what to do with answer.
write("Command:")
answer = read()

I'm halfway through the long sleep and so far no issues. Going to bed now, will check back tomorrow.

I got sidetracked…. It made it through the entire sleep and executed the reboot.
Edited on 10 April 2012 - 10:23 AM
Dale310 #15
Posted 10 April 2012 - 06:59 PM
wow this is great thank you :P/>/> ill fix the comments you added and put it in an actual scenario. then i will work on a menu with loops on the side.