Posted 20 December 2013 - 07:16 AM
Hello there! I'm not sure if someone has already asked about it, but i have a problem. I'm trying to make a turtle do something with a rednet message, while the turtle is already doing something else. Here's a sample code, to illustrate my point:
now, when i try to broadcast a message (for example,"right"), the turtle starts to turn right, as expected; but when i try to broadcast "left", the turtle keeps turning right. Obviously, i'm doing something wrong with "parallel": how can i make a program as the one mentioned? Maybe i have a fix for this, something like (pseudo code):
1)a central computer start spamming "right" or "left" or whatever i type at constant pace
2)turtle start to turn right, at each step send a message to a central computer asking for "job" (right or left, in the above example), hopefully receiving one of the "spammed" messages
3)then the turtle sends back to the computer a message like "received", so the central will stop spamming and waits for another input
Anyways, what's wrong? Thank you for any reply and excuse me for my english, i'm italian :)/>
rednet.open("right")
function rec() --this is a "receive" function
id,message,distance=rednet.receive()
end
function right() --this is just a sample function
while message=="right" do
turtle.turnRight()
end
end
function left() -- another sample funct
while message=="left" do
turtle.turnLeft()
end
end
--this is the actual program, i'm using a while true do loop so when i rename the prog as "startup" it will load automatically at reboot and keep running
while true do
parallel.waitForAny(rec(),right(),left())
end
now, when i try to broadcast a message (for example,"right"), the turtle starts to turn right, as expected; but when i try to broadcast "left", the turtle keeps turning right. Obviously, i'm doing something wrong with "parallel": how can i make a program as the one mentioned? Maybe i have a fix for this, something like (pseudo code):
1)a central computer start spamming "right" or "left" or whatever i type at constant pace
2)turtle start to turn right, at each step send a message to a central computer asking for "job" (right or left, in the above example), hopefully receiving one of the "spammed" messages
3)then the turtle sends back to the computer a message like "received", so the central will stop spamming and waits for another input
Anyways, what's wrong? Thank you for any reply and excuse me for my english, i'm italian :)/>