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

wireless turtle help

Started by kain184, 30 June 2014 - 12:20 PM
kain184 #1
Posted 30 June 2014 - 02:20 PM
hello all. i am working on a small planter program to use with pnumaticraft seeds for the squid seeds as it works a little better than the other planters i have seen but i am trying to control a turtle wirelessly but am having a bit of trouble after having it check fuel and refuel it opens communication than just sits and does nothing i know it is in range as i have done basic lua console to test if it will recieve the message. i just need a little help with my code which is as fallows

os.loadAPI("rom/apis/rednet")

while true do
com = rednet.isOpen()
if com == false then
rednet.open("left")
else
if com == true then
print("communication open")
turtle.select(2)
fuel = turtle.getItemCount(2)
level = turtle.getFuelLevel()
if fuel < 64 then
if level < 20000 then
turtle.suckDown(64-fuel)
turtle.refuel()
else
if level < 20000 then
turtle.refuel()
else

sleep(1)
id, message = rednet.recieve()
if id == "20" and message == "plant" then 
print("message recieved")
turtle.select(1)
slot = turtle.getSelectedSlot()
number = turtle.getItemCount(slot)
if number == 8 then
turtle.forward()
turtle.dropDown(1)
sleep(15)
turtle.forward()
turtle.dropDown(1)
sleep(15)
turtle.forward()
turtle.dropDown(1)
sleep(15)
turtle.forward()
turtle.dropDown(1)
sleep(15)
turtle.forward()
turtle.dropDown(1)
sleep(15)
turtle.forward()
turtle.dropDown(1)
sleep(15)
turtle.forward()
turtle.dropDown(1)
sleep(15)
turtle.forward()
turtle.dropDown(1)
sleep(15)
turtle.turnRight()
turtle.turnRight()
turtle.forward()
sleep(1)
turtle.forward()
sleep(1)
turtle.forward()
sleep(1)
turtle.forward()
sleep(1)
turtle.forward()
sleep(1)
turtle.forward()
sleep(1)
turtle.forward()
sleep(1)
turtle.forward()
sleep(1)
turtle.turnRight()
turtle.turnRight()
else
end
end
end
end
end
end
end
end
if anyone can help i would appreciate it
SquidDev #2
Posted 30 June 2014 - 02:34 PM
What does it print out? The main areas for error I can see is line 22-23:

id, message = rednet.recieve()
if id == "20" and message == "plant" then

If it never receives a message then it will wait until it does. You should also be checking if id=20 (as id is a number not a string)

On lines 27-28 you also have:


number = turtle.getItemCount(slot)
if number == 8 then

Are there exactly 8 items in the selected slot?

I'd add lots of print statements - find out where the code is getting to.

You might also want to look at indenting the code:

Spoiler

os.loadAPI("rom/apis/rednet")
while true do
com = rednet.isOpen()
if com == false then
  rednet.open("left")
else
  if com == true then
   print("communication open")
   turtle.select(2)
   fuel = turtle.getItemCount(2)
   level = turtle.getFuelLevel()
   if fuel < 64 then
    if level < 20000 then
	 turtle.suckDown(64-fuel)
	 turtle.refuel()
    else
	 if level < 20000 then
	  turtle.refuel()
	 else
	  sleep(1)
	  id, message = rednet.recieve()
	  if id == "20" and message == "plant" then
	   print("message recieved")
	   turtle.select(1)
	   slot = turtle.getSelectedSlot()
	   number = turtle.getItemCount(slot)
	   if number == 8 then
	    turtle.forward()
	    turtle.dropDown(1)
	    sleep(15)
	    turtle.forward()
	    turtle.dropDown(1)
	    sleep(15)
	    turtle.forward()
	    turtle.dropDown(1)
	    sleep(15)
	    turtle.forward()
	    turtle.dropDown(1)
	    sleep(15)
	    turtle.forward()
	    turtle.dropDown(1)
	    sleep(15)
	    turtle.forward()
	    turtle.dropDown(1)
	    sleep(15)
	    turtle.forward()
	    turtle.dropDown(1)
	    sleep(15)
	    turtle.forward()
	    turtle.dropDown(1)
	    sleep(15)
	    turtle.turnRight()
	    turtle.turnRight()
	    turtle.forward()
	    sleep(1)
	    turtle.forward()
	    sleep(1)
	    turtle.forward()
	    sleep(1)
	    turtle.forward()
	    sleep(1)
	    turtle.forward()
	    sleep(1)
	    turtle.forward()
	    sleep(1)
	    turtle.forward()
	    sleep(1)
	    turtle.forward()
	    sleep(1)
	    turtle.turnRight()
	    turtle.turnRight()
	   end
	  end
	 end
    end
   end
  end
end
end

You don't need to load the rednet API either.
Edited on 30 June 2014 - 12:36 PM
kain184 #3
Posted 30 June 2014 - 04:36 PM
it says 20 test on the response and yes i checked the items in slot one there are always 8 when the program is supposed to run as i have an item supply pipe above the turtle that is giving it 8 before every movement cycle
kain184 #4
Posted 30 June 2014 - 06:10 PM

os.loadAPI("rom/apis/rednet")
print("running")
  sleep(1)
com = rednet.isOpen()
if com == false then
  rednet.open("left")
else
if com == true then

  print("communication open")
 
  while true do
 
   turtle.select(2)
   fuel = turtle.getItemCount(2)
   level = turtle.getFuelLevel()
   if fuel < 64 and level < 20000 then
    turtle.suckDown(64-fuel)
    turtle.refuel()
 
   else
    sleep(1)
    event, id, message = os.pullEvent("rednet_message")
    print(id)print(message)
    if id ==20  and message =="plant" then
	 print("message recieved")
	 print("sending response")
	 rednet.send(20, "plant")
	 turtle.select(1)
	 slot = turtle.getSelectedSlot()
	 number = turtle.getItemCount(slot)
	 if number == 8 then
	  turtle.forward()
	  turtle.dropDown(1)
	  sleep(15)
	  turtle.forward()
	  turtle.dropDown(1)
	  sleep(15)
	  turtle.forward()
	  turtle.dropDown(1)
	  sleep(15)
	  turtle.forward()
	  turtle.dropDown(1)
	  sleep(15)
	  turtle.forward()
	  turtle.dropDown(1)
	  sleep(15)
	  turtle.forward()
	  turtle.dropDown(1)
	  sleep(15)
	  turtle.forward()
	  turtle.dropDown(1)
	  sleep(15)
	  turtle.forward()
	  turtle.dropDown(1)
	  sleep(15)
	  turtle.turnRight()
	  turtle.turnRight()
	  turtle.forward()
	  sleep(1)
	  turtle.forward()
	  sleep(1)
	  turtle.forward()
	  sleep(1)
	  turtle.forward()
	  sleep(1)
	  turtle.forward()
	  sleep(1)
	  turtle.forward()
	  sleep(1)
	  turtle.forward()
	  sleep(1)
	  turtle.forward()
	  sleep(1)
	  turtle.turnRight()
	  turtle.turnRight()
	 else
	 print("not enough items")
	 end	 
    else
    print("message error")print(id)print(message)
    end
   end
 
  end
else
print("communication error")
end
end
fixed it thanks for the help and the advice here is the code that worked i seem to have been forcing it into an end point with the code as it was written before
kain184 #5
Posted 30 June 2014 - 06:20 PM
one thing tho i cant use this on a startup scrip it exits after opening rednet and does not keep the loop running
Dog #6
Posted 30 June 2014 - 06:38 PM
Take a look at your beginning logic. It is essentially saying that if the modem isn't open, then open the modem and stop; but if the modem is already open, then go ahead and do everything else. You need to rework your if statement check for the modem's state and open the modem, then move on to the other actions.

if not rednet.isOpen("side") then
  rednet.open("side")
end

... reset of script...

Good luck :)/>
kain184 #7
Posted 30 June 2014 - 11:59 PM
thing is the program works perfictly not on a startup script if i just run the program itself its when i call it from outside that it fails
Dog #8
Posted 01 July 2014 - 01:51 AM
first, get rid of the first line…

os.loadAPI("rom/apis/rednet")

You don't need that since rednet is a 'built-in' api and doesn't need to be explicitly loaded.

Next, please post your code after making that change and post the entire text of any error messages it outputs. We'll go from there :)/>
kain184 #9
Posted 18 July 2014 - 03:53 AM
been a little pause in this program ill get that info back to you but i had to reset my world do to something messing up so i have to rebuild the entire system anyway