Hello everyone!
Could you help me please with my code?
The issue that I'm having is that I want computer will continue to send signal(rednet.send) at the beginning to the turtle until he finds it(until I turn on the turtle). Here is my code:
rednet.open("right")
term.clear()
term.setCursorPos(1,1)
while true do
rednet.send(13,"true")
id, X = rednet.receive()
if X == "slot1" then
term.setCursorPos(1,18)
print("Slot1")
elseif X== "slot9" then
term.setCursorPos(1,18)
print("Slot9")
end
term.setCursorPos(1,1)
print("1-forward 2-back 11-slot1 19-slot9")
write("Enter command: ")
input = io.read()
input = tostring(input)
rednet.send(13, input)
term.clear()
term.setCursorPos(1,1)
if input == "1" then
write("Enter the number of repetitions: ")
repet = io.read()
repet = tostring(repet)
rednet.send(13,repet)
end
id, valid = rednet,receive()
if valid == "true" then
print("Will be done Boss!")
else
print("No such command!")
end
sleep(1)
term.clear()
term.setCursorPos(1,1)
end
It sounds like the parallel API will help you.
I found these two, but I'm not really clear how they work and how it can help with my case
I will be very greatfull if someone could explain me
Thanks!
local function receive () -- Define function1.
print(rednet.receive())
end
local function send () -- Define function2.
rednet.broadcast(read())
end
parallel.waitForAny(receive, send) -- Run both functions in parallel until one finishes.
Yep, that's the gist of how to use parallel.
I don't really understand your issue and your code so I'm finding it a little difficult to help.
Problem: The problem is if I first turn on the computer it will send right away "true" to the turtle and if while that
the turtle is still turned off it will not receive the "true" and the computer will move to next string id, X = rednet.receive(), but how can I make that it will continue to send the "true" till the moment I turn on the turtle and it will be able to "receive" the "true"? FYI: I don't want to use edit startup, I know how to make it work with it but I want to use another approach.
-- Here is my Computer's code:
rednet.open("right")
term.clear()
term.setCursorPos(1,1)
while true do
rednet.send(17,"true") -----------------issue
id, X = rednet.receive()----------------issue
if X == "slot1" then--------------------issue
term.setCursorPos(1,18)---------issue
print("Slot1")------------------issue
elseif X == "slot2" then----------------issue
term.setCursorPos(1,18)---------issue
print("Slot2")------------------issue
end-------------------------------------issue
term.setCursorPos(1,1)
print("1-forward 2-back 11-slot1 19-slot9")
write("Enter command number: ")
input = io.read()
input = tostring(input)
rednet.send(17, input)
term.clear()
term.setCursorPos(1,1)
if input == "1" then
write("Enter repetition number: ")
repet = io.read()
repet = tostring(repet)
rednet.send(17, repet)
end
if input == "2" then
write("Enter repetition number: ")
repet = io.read()
repet = tostring(repet)
rednet.send(17, repet)
end
id, valid = rednet.receive()
if valid == "true" then
print("Will be done Boss!")
else
print("No such command!")
end
sleep(1)
term.clear()
term.setCursorPos()
end
-- My turtle's code:
rednet.open("left")
term.clear()
term.setCursorPos(1,1)
forward = "1"
back = "2"
slot1 = "11"
slot9 = "19"
while true do
id, X = rednet.receive()
if X == "true" then
if turtle.getSelectedSlot() == 1 then
rednet.send(18, "slot1")
elseif turtle.getSelectedSlot() == 2 then
rednet.send(18, "slot2")
else
rednet.send(18, "false")
end
end
id, input = rednet.receive()
if input == forward then
id, repet = rednet.receive()
for N = 1, repet do
turtle.forward()
rednet.send(id, "true")
end
elseif input == back then
id, repet = rednet.receive()
for N = 1, repet do
turtle.back()
rednet.send(id, "true")
end
elseif input == slot1 then
turtle.select(1)
rednet.send(id, "true")
elseif input == slot2 then
turtle.select(2)
rednet.send(id, "true")
else
rednet.send(id, "false")
end
end
You need to get the turtle to acknowledge the message (send a message back) and change the sending computer to keep sending the message until it is acknowledged by the turtle. YOu might also want to handle the turtle getting multiple of the same message if it gets resent before the turtle could send the acknowledgement.
Turtle is acknowledging message and sending it back.
But I don't know how to write the part "keep sending the message until it is acknowledged by the turtle". Do you have any idea?
Something like this should work, be warned I'm using the rednet API from memory and might get bits of it wrong.
repeat
rednet.send(turtleId, command)
local senderId, message = rednet.receive(timeoutInSeconds)
local ack = false --this might need to be outside of the loop
if senderId == turtleId and message == "ack" then
ack = true
end
until ack
Thank you so much!!!!! :D
Problem Solved!:D
rednet.open("right")
term.clear()
term.setCursorPos(1,1)
local timeoutInSeconds = 1
while true do
local success = false
repeat
rednet.send(17,"true")
id, X = rednet.receive(timeoutInSeconds)
if id == 17 and X == "true" then
success = true
elseif id == nil then
os.sleep(1)
end
until success
if success then
id, X = rednet.receive()
if X == "slot1" then
term.setCursorPos(1,18)
print("Slot1")
elseif X == "slot2" then
term.setCursorPos(1,18)
print("Slot2")
... -- Other part of the code
end
end
... -- Other part of the code