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

Need help with a turtle-control program!

Started by Arne303, 07 November 2012 - 07:39 AM
Arne303 #1
Posted 07 November 2012 - 08:39 AM
Hello everybody!
I need help with another program:
Some friends and me would like to control a turtle wireless by hitting keys on the keyboard but no program works:
The CC-Version:1.3.3
My version:
Computer:

local b
rednet.open("top")
while true do
print("Taste drücken!")
b = os.pullEvent("key")
rednet.send(150,:P/>/>
end

Turtl(wireless, mining):

rednet.open("right")
while true do
x,y,z = rednet.receive()
if y == "17" then
turtle.forward()
elseif y == "31" then
turtle.back()
elseif y == "30" then
turtle.turnLeft()
elseif y == "32" then
turtle.turnRight()
elseif y == "42" then
turtle.down()
elseif y == "57" then
turtle.up()
elseif y == "75" then
turtle.dig()
elseif y == "71" then
turtle.digUp()
elseif y == "79" then
turtle.digDown()
elseif y == "77" then
turtle.place()
elseif y == "73" then
turtle.placeUp()
elseif y == "81" then
turtle.placeDown()
elseif y == "21" then
turtle.select(1)
elseif y == "3" then
turtle.select(2)
elseif y == "4" then
turtle.select(3)
elseif y == "5" then
turtle.select(4)
elseif y == "6" then
turtle.select(5)
elseif y == "7" then
turtle.select(6)
elseif y == "8" then
turtle.select(7)
elseif y == "9" then
turtle.select(8)
elseif y == "10" then
turtle.select(9)
end
end

The result: No error-message. Nothings happens by hitting a key, e.g. "A".


Code from my friend:
Computer:

shell.run("clear")
rednet.open("left")
while true do
print("Bitte Befehle angeben!")
cmd = os.pullEvent("key")
if cmd == 44 then
print("")
print("Welche Seite? (Bitte auf Englisch angeben!)")
write("->")
cmd = io.read()
rednet.send(150, cmd)
else
rednet.send(150, cmd)
end
shell.run("clear")
end

Turtle:

rednet.open("right")
while true do
x,y,z == rednet.receive()
if y = 17 then
turtle.forward()
elseif y == 32 then
turtle.turnRight()
elseif y == 30 then
turtle.turnLeft()
elseif y == 42 then
turtle.down()
elseif y == 57 then
turtle.up()
elseif y == 31 then
turtle.back()
elseif y == 203 then
turtle.dig()
elseif y == 208 then
turtle.digDown()
elseif y == 200 then
turtle.digUp()
elseif y == 37 then
turtle.place()
elseif y == 24 then
turtle.placeUp()
elseif y == 38 then
turtle.placeDown()
end
end

The Result: Nothing happens, too.

Hope you can help me.
Thanks
sjele #2
Posted 07 November 2012 - 08:45 AM
rednet.receive() return everything as strings, witch you are comparing to a number.

x,y,z = rednet.receive()
x = tonumber(x)
y = tonumber(y)
z = tonumber(z)
--REst of code
Arne303 #3
Posted 07 November 2012 - 08:57 AM
OK. Thanks, we'll try!
Arne303 #4
Posted 07 November 2012 - 09:00 AM
It doesn't work. Can rednet send numbers?
Lyqyd #5
Posted 07 November 2012 - 09:02 AM
Have you ensured that the turtles are receiving the rednet messages at all?
Arne303 #6
Posted 07 November 2012 - 09:07 AM
I think so. Is that in the code above Ok?
Kingdaro #7
Posted 07 November 2012 - 10:57 AM
In your sender code, you're sending the event instead of the key pressed.


b = os.pullEvent("key")

Should be:


ev, b = os.pullEvent("key")

Also, on this line:


rednet.send(150,:P/>/>

"B" should be lowercase.
Lyqyd #8
Posted 07 November 2012 - 11:15 AM
The forum automatically transforms most instances of "b)" to "B)", even in code chunks.

(And we will see if that even posts correctly!)
Kingdaro #9
Posted 07 November 2012 - 11:21 AM
Ahaha, I love IP.Board.
Arne303 #10
Posted 08 November 2012 - 01:03 AM
Thank You! Now it works great!