4 posts
Posted 12 August 2012 - 11:37 AM
Hello everyone,
I'm attempting to build a very simple elevator using a computer, elevator rails, and a button at either end of the track connected to the same input. The goal is for the button to toggle the on/off state of the track and for the program to loop forever, constantly checking for the button.
This is my code:
while true do
b = rs.getInput("back")
r = rs.getOutput("left")
if b == "true" then
if r == "true" then
rs.setOutput("left",false)
else
rs.setOutput("left",true)
end
end
end
The error I get is "filename:2: Too long without yielding". I've never use Lua before. Could someone tell me what's wrong with my code or if there is a better way to code that function?
318 posts
Location
Somewhere on the planet called earth
Posted 12 August 2012 - 11:46 AM
Stick a sleep(0) or sleep(.1) in there
1548 posts
Location
That dark shadow under your bed...
Posted 12 August 2012 - 01:16 PM
rather put os.pullEvent() that command listens for any event so it will just wait until something happens (a redstone change for example)
4 posts
Posted 12 August 2012 - 11:25 PM
Okay, here's what I have now, but it still doesn't work.
while true do
os.pullEvent()
t = rs.getOutput("left")
if t == "true" then
rs.setOutput("left",false)
else
rs.setOutput("left",true)
end
end
1548 posts
Location
That dark shadow under your bed...
Posted 13 August 2012 - 01:59 PM
don't put quotes around 'true' it is a boolean, not a string
11 posts
Posted 13 August 2012 - 03:35 PM
Hi, sorry if i understood wrong, but you want a button on each side of the track to toggle on or off the rail?
If so you don't need a computer at all. If you have redpower2 you can connect 2 buttons to a "toggle latch" and the rail on the other side.
For the computer i would use something like
while true do
local b = rs.getInput("back")
local l = rs.getInput("left")
os.pullEvent()
if b == true and l == true then
rs.setOutput("left", false)
sleep(1)
elseif b == true and l == false then
rs.setOutput("left", true)
sleep(1)
end
end
i didn't test it but it should work.
PS. Added the sleep(1) because of the button delay
1548 posts
Location
That dark shadow under your bed...
Posted 13 August 2012 - 03:41 PM
you should get the output for l, this is what I would do
While true do
os.pullEvent("redstone")
if rs.getInput("right") then
if rs.getOutput("left") then
rs.setOutput("left", false)
else
rs.setOutput("left", true)
end
end
end
4 posts
Posted 14 August 2012 - 04:23 AM
you should get the output for l, this is what I would do
I tried this and got a bios 206 error.
Hi, sorry if i understood wrong, but you want a button on each side of the track to toggle on or off the rail?
If so you don't need a computer at all. If you have redpower2 you can connect 2 buttons to a "toggle latch" and the rail on the other side.
Actually, the buttons are wired to the same input.
I'm gonna try adapting the code you posted. I'll return with results.
1111 posts
Location
Portland OR
Posted 14 August 2012 - 04:26 AM
you should get the output for l, this is what I would do
I tried this and got a bios 206 error.
Did you do While true do or while true do? Make sure its all lower case. Otherwise KaoS code should work.
4 posts
Posted 14 August 2012 - 08:22 PM
Haha, yeah, I capped the "while". It was being a bit frigidity, so I added a sleep(1) at the end of the loop. It all works perfectly now. Thanks all for the help!