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

Parallel.waitforany

Started by Lollerofinni, 14 September 2013 - 11:04 AM
Lollerofinni #1
Posted 14 September 2013 - 01:04 PM
Hello!

I'm working on this lift code. My problem is the marked part of the code. So, what kind of code should I use so that ths part of the code works so that it lets you input and the same time checks the rs-signal. By using a while loop it does it so fast that you simply can't input.

function inputting()
input=read()
end

function rsCheckUp()
if rs.testBundledInput("back",colors.brown)== true then
print("hello")
end
end


function callLift()
for i=1,26 do
rs.setBundledOutput("back",colors.yellow)
sleep(1)
rs.setBundledOutput("back",0)
sleep(1)
end
end

function Up()
end


function start()
rs.setBundledOutput("back",0)
term.clear()
term.setCursorPos(1,1)
print("Lift Control")
print("")
print("1) Call Lift")


parallel.waitForAny(inputting, rsCheckUp) <———————————————————————————————————– THE PROBLEM


if input=="1" and rs.testBundledInput("back",colors.black)==true then
print("Lift Is Already On Current Floor")
sleep(2)
elseif input=="1" then
callLift()
end
end

while true do start()
sleep(5)
end
Yevano #2
Posted 14 September 2013 - 02:34 PM
The rsCheckup function returns immediately. If you want to wait for both to finish, use parallel.waitForAll. However, since it does return immediately, I don't see why you need to use parallelism in the first place. If you later change rsCheckup to wait for a redstone signal using an event, parallel.waitForAll is what you want.

You should put any code you post here in code tags, by the way. "
some code here[.code]" (replace the "." with a "/").
Lollerofinni #3
Posted 14 September 2013 - 03:53 PM
The thing is that the computer that uses this codes is meant for calling the lift from the other floor and I'm trying to get this computer to control the lift motors too. There is another computer in the lift that is going to send information with redstone signals to this computer. The rsCheckUp is going to be the request of going up. So I want this computer to wait for the user input of calling the lift and simlutaneosly checking for incoming redstone signals. That's why I'm not using the parallel.waitForAll. How should that part of the code look like so it would work perfectly?
Yevano #4
Posted 14 September 2013 - 04:37 PM
You might want to rewrite your code, because it looks like you're doing a hybrid of parallelism and busy waiting. Really what you want to do is have two threads, one being the input thread and the other being the redstone thread. You just want to accept input input forever in the input thread, and wait for redstone events forever on your redstone thread using os.pullEvent. You don't need the sleep loop.