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

Rednet code question.

Started by brattus123, 16 September 2013 - 03:30 AM
brattus123 #1
Posted 16 September 2013 - 05:30 AM
i'm only posting here because i cant' create a new topic but i need help with this code i want it to make a turtle move forward when a redstone signal is applied to a computer here is what i have so far

Computer:
rednet.open ("left")
while true do
local event, id, msg = os.pullEvent("rednet_message")
if msg == "foward" then
turtle.forward (10)
end
end

Turtle:
rednet.open ("left")
while true do
local event, id, msg = os.pullEvent(rednet_message)
if msg == "forward" then
turtle.forward (10)
end
end

Please Help thank you
Lyqyd #2
Posted 16 September 2013 - 12:39 PM
Split into new topic.

You clearly haven't read any of the stickies (I can tell because you didn't know where to post this), or even the titles of the sticky posts. Go read them.

When you come back, make sure that you have the correct code for the computer side in your post, because it looks to me like you pasted the turtle side twice.
plazter #3
Posted 16 September 2013 - 08:10 PM
Computer should be like:

local side = "<sideinhere>"
rednet.open(side)
while true do
  if redstone.getInput("right") then
   -- either use broadcast or send, tho i use broadcast as my default
    rednet.broadcast("forward")
	  else
    sleep(2)
  end
end


and for the turtle:

rednet.open ("right")
while true do
   local event, id, msg = os.pullEvent("rednet_message")
   -- for the broadcast you proberly only want the turtle to react from --the one computer that sends the message so that would meaby
-- be computer 1
   if id == 1 then
	 if msg == "forward" then
	  turtle.forward (10)
	 end
   end
end

Hope that helped :)/>/&amp;gt;</sideinhere>
Edited on 16 September 2013 - 06:11 PM
brattus123 #4
Posted 17 September 2013 - 10:53 PM
sorry lyqyd was not sure what i was doing in terms of posting thanks for the help
brattus123 #5
Posted 17 September 2013 - 11:02 PM
ok so in the
"<sideinhere>"

part which side am i ment to type and also im getting an error that says rednet : 44: too long witout yielding

any help would be much appreciated
campicus #6
Posted 17 September 2013 - 11:24 PM
ok so in the
"<sideinhere>"

part which side am i ment to type and also im getting an error that says rednet : 44: too long witout yielding

any help would be much appreciated

Whichever side the wireless modem is on.

No idea about your error, post your full program (use code tags and format your code too please)
brattus123 #7
Posted 21 September 2013 - 11:38 PM
here is the code im currently using

Computer

local side = ("right")
rednet.open ("right")
while true do
if redstone.getInput ("left") then
rednet.broadcast ("forward")
else
sleep (2)
  end
end

Turtle

rednet.open ("left")
while true do
local event, id, msg = os.pullEvent(rednet_message)
if id == 1 then
if msg == "forward" then
turtle.forward (10)
	 end  
end
end
BigTwisty #8
Posted 21 September 2013 - 11:58 PM
The code on the computer as it is will produce far more messages than you think. Every time the message is sent, it will loop back to the beginning and the redstone signal will still be active. You probably want to only send a single signal when the redstone state changes from off to on.

Try an os.pullEvent("redstone") at the top of your loop, and get rid of that else sleep at the end. Then the code in the loop will only run every time the redstone state changes.
brattus123 #9
Posted 22 September 2013 - 12:10 AM
when in use this code i pull a lever to send a signal into the left side of the computer but the turtle does not move

Computer

local side = ("right")
rednet.open ("right")
os.pullEvent ("redtone")
while true do
if redstone.getInput ("left") then
rednet.send (23,"forward")
  end
end

Turtle:

rednet.open ("left")
while true do
local event, id, msg = os.pullEvent(rednet_message)
if id == 1 then
if msg == "forward" then
turtle.forward (10)
	 end  
end
end
BigTwisty #10
Posted 22 September 2013 - 12:25 AM
Computer

local side = ("right")
rednet.open ("right")
os.pullEvent ("redtone")
while true do
if redstone.getInput ("left") then
rednet.send (23,"forward")
  end
end
2 things in the computer code:
Move the os.pullEvent inside the loop (after the while line)
Spell "redstone" inside that pullEvent correctly.
brattus123 #11
Posted 22 September 2013 - 12:39 AM
still got same problems as last time

Computer:

local side = ("right")
rednet.open ("right")
while true do
os.pullEvent ("redstone")
if redstone.getInput ("left") then
rednet.broadcast ("forward")
  end
end

Turtle:

rednet.open ("left")
while true do
local event, id, msg = os.pullEvent(rednet_message)
if id == 1 then
if msg == "forward" then
turtle.forward (10)
	 end  
end
end
BigTwisty #12
Posted 22 September 2013 - 12:44 AM
Turtle:

rednet.open ("left")
while true do
local event, id, msg = os.pullEvent(rednet_message)
if id == 1 then
if msg == "forward" then
turtle.forward (10)
	 end  
end
end
Put quotes around rednet_message.

Also, are you sure the rednet ID of the computer is 1?
brattus123 #13
Posted 22 September 2013 - 12:47 AM
i just need to type the id command into the computer to find it's rednet id right
BigTwisty #14
Posted 22 September 2013 - 12:52 AM
Not sure, I don't use rednet much, and when I do I don't hard code the IDs. Either way, it's easy to just stop checking for ID to see if it works. And the quotes around rednet_message are needed anyway. Otherwise os.pullEvent is treating it like a variable and use its value, nil. Then it will return every single event that comes through.
brattus123 #15
Posted 22 September 2013 - 12:54 AM
it works! as a last thing i want to know how to change it from sending the turtle forward to instead run a new program

Computer:

local side = ("right")
rednet.open ("right")
while true do
os.pullEvent ("redstone")
if redstone.getInput ("left") then
rednet.send (23,"forward")
  end
end

Turtle:
rednet.open ("left")
while true do
local event, id, msg = os.pullEvent("rednet_message")
if id == 18 then
if msg == "forward" then
turtle.forward (5)
end
end
end
BigTwisty #16
Posted 22 September 2013 - 12:59 AM
Look at the documentation for the shell API, specifically shell.run(). You can probably figure it out from there. Glad it's working for you!
brattus123 #17
Posted 22 September 2013 - 01:03 AM
thank you for the help i will look that up
brattus123 #18
Posted 22 September 2013 - 02:22 AM
Umm trying to run these 4 programs together need some help with my code

Turtle:

shell.run ("orbit")
end
then
shell.run ("orbit2")
end
then
shell.run ("orbit3")
end
then
shell.run ("orbit4")
end

Keep getting error bios :338: [string "orbitfull"] :2: '<eof>' expected

also the programs i'm trying to run are orbit , orbit2 , orbit3 and orbit4
Lyqyd #19
Posted 22 September 2013 - 02:29 AM
Take out all of the ends and thens.
brattus123 #20
Posted 22 September 2013 - 02:49 AM
this code was working fine before what it was ment to do was send the turtle backward 25 blocks then turn right then back 17 blocks turn back 25 turn back 25 and turn again but it's not working anymore help

Orbit:

length = 25
for i=1,length,1 do
if turtle.detect(0) then
  end
turtle.back (25)
  end

Orbit2:

turtle.turnRight (1)
length = 17
for i=1,length,1 do
if turtle.detect () then
  end
turtle.back ()
  end

Orbit3:

turtle.turnRight (1)
length = 25
for i=1,length,1 do
if turtle.detect () then
  end
turtle.back ()
  end

Orbit4:

turtle.turnRight (1)
length = 17
for i=1,length,1 do
if turtle.detect () then
  end
turtle.back ()
  end
turtle.turnRight (1)

Lyqyd #21
Posted 22 September 2013 - 03:22 AM
So, you're essentially just doing this?


for i = 1, 2 do
  for j = 1, 25 do
    turtle.back()
  end
  turtle.turnRight()
  for j = 1, 17 do
    turtle.back()
  end
  turtle.turnRight()
end

None of the turtle functions you're using take any arguments, so those numbers you're passing them are just getting ignored.
brattus123 #22
Posted 22 September 2013 - 03:37 AM
i tried using that code and all it did was travel in a stright line 25 blocks turn in a circle for a while faced the wrong way then went 25 more blocks in a stright line then stopped.

here's the code

Turtle:

for i = 1, 2 do
for j = 1, 25 do
  turtle.back ()
end
turtle.turnRight ()
for j = 1, 17 do
turtle.turnRight ()
end turtle.turnRight ()
end
BigTwisty #23
Posted 22 September 2013 - 08:41 AM
Whenever you have several places in your code that are basically the same, it's a good idea to use a function. Try this:

local function backTurn(length)
  for i = 1,length do
    turtle.back()
  end
  turtle.turnRight()
end

backTurn(25)
backTurn(12)
backTurn(25)
backTurn(12)

Looking at the last 4 lines it is now easier to (a.) see what the turtle is going to do and (b.) make it do something different if you want.

It should be noted that Lyqyd's code should do he same thing if it is copied in correctly.
brattus123 #24
Posted 23 September 2013 - 01:05 AM
with this code which is attempting to combine the rednet code with the orbit code im getting an error saying ("eof") expected on line 10 and i don't know why and also would it be possible to make this code loop until the redstone signal is applied

Turtle:

rednet.open ("left")
while true do
local event, id, msg = os.pullEvent ("rednet_message")
if id == 18 then
if msg == "nograv" then
shell.run ("offorbit")
end
end
end
else
shell.run ("orbit")
BigTwisty #25
Posted 23 September 2013 - 08:20 AM
with this code which is attempting to combine the rednet code with the orbit code im getting an error saying ("eof") expected on line 10 and i don't know why and also would it be possible to make this code loop until the redstone signal is applied

Turtle:

rednet.open ("left")
while true do
local event, id, msg = os.pullEvent ("rednet_message")
if id == 18 then
if msg == "nograv" then
shell.run ("offorbit")
end
end
end
else
shell.run ("orbit")

Proper code indentation would make this syntax error easier to spot.


rednet.open ("left")
while true do
  local event, id, msg = os.pullEvent ("rednet_message")
  if id == 18 then
    if msg == "nograv" then
      shell.run ("offorbit")
    end
  end
end
else
  shell.run ("orbit")

The last 2 lines actually belong right after the other shell.run. Right now they are outside your loop and will never run. Can you see how code indentation makes it easier to read your own code?
brattus123 #26
Posted 23 September 2013 - 11:08 PM
ok now how would i get this program to loop until a redstone signal is applied
BigTwisty #27
Posted 23 September 2013 - 11:23 PM

rednet.open ("left")
repeat
  local event, id, msg = os.pullEvent ("rednet_message")
  if id == 18 then
    if msg == "nograv" then
      shell.run ("offorbit")
    else
      shell.run ("orbit")
    end
  end
until rs.getInput("left")
brattus123 #28
Posted 23 September 2013 - 11:57 PM
instead of a line of redstone being applied directly to the turtle how would i make it stop looping with a rednet signal also i changed the rednet_message to rednet.recieve to stop the turtle from wating indefinatly and now im getting the error rednet :68: Expected number here is the code:


rednet.open ("left")
while true do
local event, id, msg = os.pullEvent (rednet.receive ("10"))
if id == 18 then
if msg == "nograv" then
shell.run ("offorbit")
shell.run ("orbit")
end
end
end
BigTwisty #29
Posted 24 September 2013 - 11:29 PM
instead of a line of redstone being applied directly to the turtle how would i make it stop looping with a rednet signal also i changed the rednet_message to rednet.recieve to stop the turtle from wating indefinatly and now im getting the error rednet :68: Expected number here is the code:


rednet.open ("left")
while true do
local event, id, msg = os.pullEvent (rednet.receive ("10"))
if id == 18 then
if msg == "nograv" then
shell.run ("offorbit")
shell.run ("orbit")
end
end
end

Look at your os.pullEvent line. Read the documentation on os.pullEvent, look at your code again, and have a nice facepalm moment.
plazter #30
Posted 29 September 2013 - 01:33 PM
Not sure why you want shell.run, but as they said, you forget the "os.pullEvent('rednet_message')"

Also try avoiding multi shells :)/> i use functions :)/>
brattus123 #31
Posted 30 September 2013 - 01:14 AM
ok so need to change it back to os.pullEvent ("rednet_message") but what i need it to do is check for a rednet signal if there is one execute the offorbit program and if there is not one execute the orbit program.
BigTwisty #32
Posted 30 September 2013 - 01:27 PM
The answer lies in the tutorials on this very site. Check out the "If then else" tutorial here: http://www.computercraft.info/wiki/Conditional_statements
and the Redstone API documentation here: http://www.computercraft.info/wiki/Redstone_(API). In the Redstone API, check out the rs.getInput(side) function. Figuring it out from there should be simple enough.