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

[LUA][Error] Having problems with code

Started by Bandus, 28 March 2012 - 12:16 AM
Bandus #1
Posted 28 March 2012 - 02:16 AM
Having trouble with some code and can't seem to figure out what I'm doing wrong.



term.clear()
term.setCursorPos(1,1)

textutils.slowPrint("Train Control System v1.0")
textutils.slowPrint("Copyright 2012. Rights Reserved.")

print("Waiting for input...")

while true do
event, param1, param2 = os.pullEvent()
if event == "redstone" then
sleep(10)
redstone.setOutput("left", true)
sleep(1)
redstone.setOutput("left", false)
end
end

I'm reasonably confident that the issue is with the loop. That wouldn't surprise me because its an error I'm having trouble understanding with regards to programming for some reason. In any event, when the "while true do" and the "end" that closes it are removed, the program works as expected as long as someone is at the computer terminal with it open. When the "while true do" and "end" code are added in, the program appears to run properly, however, the redstone never activates despite the computer receiving redstone input.

Thanks for any help!
MysticT #2
Posted 28 March 2012 - 02:54 AM
The code has no errors, but it might be the sleep(10) call that causes trouble.
Is really necesary that much sleep time?
Have you waited that time to see if it activates the redstone?
Remember that when sleeping the computer ignores any event, so if you send the redstone signal while sleeping it won't do anything.
Try reducing the sleep time, if that doesn't work… well i'm not sure what the problem could be :o/>/>
Bandus #3
Posted 28 March 2012 - 03:47 AM
Actually, I plan to have sleep(30) or so in that spot when the final code is done. If the "while" loop isn't there, the code works exactly as expected. The objective at this point is to make that code run w/o someone having to be sitting at the terminal the entire time.

So, it definitely activates the redstone as long as the while loop isn't there and someone is sitting, active on the terminal with the program running.

Also, as far as sleeping goes, with sleep(10) won't it wait 10 seconds and then execute more code?
Cloudy #4
Posted 28 March 2012 - 12:58 PM
How far away is the terminal? It could be getting unloaded due to the chunk being unloaded (in which case, it couldn't detect a redstone signal anyway).
Bandus #5
Posted 28 March 2012 - 08:17 PM
I have resolved this issue. Thanks all, for the help!