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

Continue until keypressed

Started by KaosuJoufu, 07 November 2016 - 12:30 AM
KaosuJoufu #1
Posted 07 November 2016 - 01:30 AM
Hi,

I am new to posting on the forums. I have a question, and while I have looked for an answer, both on the wiki and here, I am not sure where to find an answer.
I am hoping I can get a little help.
Here is what I'm trying to do:
I have a computer putting off a redstone signal; 40 seconds on, 20 seconds off. It is outputting to a monitor, showing how much time remains on the present status. So far, so good.
Now, I would like to be able to press any key and stop the program, preferably putting a notice on the monitor that the system is not running.
I have looked up using os.pullEvent(), however since I am using pause(1) in my program, it does not appear to work.
Any suggestions?

Present code: function timer is working.
Spoilerfunction timer()
local monitor = peripheral.wrap("top")
term.clear()
term.setCursorPos(1,1)
while true do
rs.setOutput("bottom",true)
monitor.clear()
monitor.setCursorPos(2,1)
monitor.write("Spawn")
monitor.setCursorPos(1,4)
monitor.write("Seconds")
for x = 40 , 1 , -1 do
monitor.setCursorPos(3,3)
if x>9 then
monitor.write(tostring(x))
else
monitor.write(" ")
monitor.write(tostring(x))
end
sleep(1)
end
rs.setOutput("bottom",false)
monitor.clear()
monitor.setCursorPos(2,1)
monitor.write("Clear")
monitor.setCursorPos(1,4)
monitor.write("Seconds")
for x = 20 , 1, -1 do
monitor.setCursorPos(3,3)
if x>9 then
monitor.write(tostring(x))
else
monitor.write(" ")
monitor.write(tostring(x))
end
sleep(1)
end
end
end

function stop()
while true do
e = os.pullEvent("key")
if e == "key" then
return
end
end
end

parallel.waitForAny(timer(),stop())
Bomb Bloke #2
Posted 07 November 2016 - 12:07 PM
parallel.waitForAny(timer(),stop())

This line calls timer / stop and then, when they've completed (which never happens - they run infinite loops), attempts to pass whatever they return (nothing) to parallel.waitForAny().

Pass the function pointers instead:

parallel.waitForAny(timer, stop)
KaosuJoufu #3
Posted 07 November 2016 - 03:33 PM
Oh wow. Such a small thing. Thank you so much. I used to program in Pascal, and while the logic is the same, the language is really different. Thank you again!