Jump to content




Rednet code question.


  • You cannot reply to this topic
31 replies to this topic

#1 brattus123

  • Members
  • 17 posts

Posted 16 September 2013 - 04: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

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 16 September 2013 - 11:39 AM

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.

#3 plazter

  • Members
  • 134 posts

Posted 16 September 2013 - 07: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 :)/&gt;</sideinhere>

Edited by plazter, 16 September 2013 - 07:11 PM.


#4 brattus123

  • Members
  • 17 posts

Posted 17 September 2013 - 09:53 PM

sorry lyqyd was not sure what i was doing in terms of posting thanks for the help

#5 brattus123

  • Members
  • 17 posts

Posted 17 September 2013 - 10: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

#6 campicus

  • Members
  • 164 posts

Posted 17 September 2013 - 10:24 PM

View Postbrattus123, on 17 September 2013 - 10:02 PM, said:

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)

#7 brattus123

  • Members
  • 17 posts

Posted 21 September 2013 - 10: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


#8 BigTwisty

  • Members
  • 106 posts

Posted 21 September 2013 - 10: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.

#9 brattus123

  • Members
  • 17 posts

Posted 21 September 2013 - 11:10 PM

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


#10 BigTwisty

  • Members
  • 106 posts

Posted 21 September 2013 - 11:25 PM

View Postbrattus123, on 21 September 2013 - 11:10 PM, said:

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.

#11 brattus123

  • Members
  • 17 posts

Posted 21 September 2013 - 11:39 PM

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


#12 BigTwisty

  • Members
  • 106 posts

Posted 21 September 2013 - 11:44 PM

View Postbrattus123, on 21 September 2013 - 11:39 PM, said:

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?

#13 brattus123

  • Members
  • 17 posts

Posted 21 September 2013 - 11:47 PM

i just need to type the id command into the computer to find it's rednet id right

#14 BigTwisty

  • Members
  • 106 posts

Posted 21 September 2013 - 11:52 PM

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.

#15 brattus123

  • Members
  • 17 posts

Posted 21 September 2013 - 11:54 PM

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

#16 BigTwisty

  • Members
  • 106 posts

Posted 21 September 2013 - 11:59 PM

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!

#17 brattus123

  • Members
  • 17 posts

Posted 22 September 2013 - 12:03 AM

thank you for the help i will look that up

#18 brattus123

  • Members
  • 17 posts

Posted 22 September 2013 - 01: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

#19 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 September 2013 - 01:29 AM

Take out all of the ends and thens.

#20 brattus123

  • Members
  • 17 posts

Posted 22 September 2013 - 01: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)







1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users