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

Problem With Rednetmessages

Started by jeroentjuh99, 16 August 2013 - 01:49 PM
jeroentjuh99 #1
Posted 16 August 2013 - 03:49 PM
Hi,
I am trying to create an music-system using a computer, a turtle and rednet.
I made some code, but it doesn't seem to work:

computer:
Spoiler

function clear()
term.clear()
term.setCursorPos( 1, 1 )
end

function disc()
print("witch disc do you want to hear? (1~16)")
local m = read()
end

function broadcast()
term.clear()
rednet.open("back")
rednet.broadcast(m)
end

function done()
message = rednet.receive()
if message == done then
sleep(240)
shell.run(startup)
end
end

while true do
clear()
disc()
broadcast()
done()
end

turtle:
Spoiler


function receive()
rednet.open("right")
local msg = rednet.receive()
term.write(msg)
end

function select()
if msg == "1" then
turtle.select(1)
elseif msg == "2" then
turtle.select(2)
elseif msg == "3" then
turtle.select(3)
elseif msg == "4" then
turtle.select(4)
elseif msg == "5" then
turtle.select(5)
elseif msg == "6" then
turtle.select(6)
elseif msg == "7" then
turtle.select(7)
elseif msg == "8" then
turtle.select(8)
elseif msg == "9" then
turtle.select(9)
elseif msg == "10" then
turtle.select(10)
elseif msg == "11" then
turtle.select(11)
elseif msg == "12" then
turtle.select(12)
elseif msg == "13" then
turtle.select(13)
elseif msg == "14" then
turtle.select(14)
elseif msg == "15" then
turtle.select(15)
else
turtle.select(16)
end

turtle.dropUp()
redstone.setOutput("top", true)
sleep(1)
redstone.setOutput("top", false)
end

function done()
turtle.suckUp()
rednet.send(6, "done")
end

while true do
turtle.select(1)
receive()
select()
done()
sleep(2)
shell.run("startup")
end

What I am trying to do is the following:
When you enter a number (1~16) on the computer, it sends it to the turtle. The turtle should read the message, and select the right slot.
It currently only selects slot 16, because the message send is 6.0. It doesn't matter what you type into the computer, the received message is always 6.0.

What is wrong?
Thank you for your time :)/>
Grim Reaper #2
Posted 16 August 2013 - 04:13 PM
rednet.receive returns several parameters, not just the message being sent.

http://www.computercraft.info/wiki/Rednet.receive
You should change the line:

local msg = rednet.receive()
to:

local senderID, msg, distanceFromSender = rednet.receive()
because rednet.receive returns the channel that the message was sent on (which should be the id of the sender if the sender used rednet.send), the message sent, and the distance from the sending computer.
jeroentjuh99 #3
Posted 16 August 2013 - 05:24 PM
Alright, changed it. I'll try it tomorrow!
Pinkishu #4
Posted 16 August 2013 - 06:30 PM
Also if you want to keep the code that way, you'll probably have to remove the local from the rednet.receive line

Or at least I think the way it currently is, the select function wouldn't even know about the msg received?
Grim Reaper #5
Posted 17 August 2013 - 02:52 AM
Also if you want to keep the code that way, you'll probably have to remove the local from the rednet.receive line

Or at least I think the way it currently is, the select function wouldn't even know about the msg received?

You're certainly right! :)/>
reububble #6
Posted 17 August 2013 - 05:36 AM
Sometimes I get these float errors and the best thing to do is just floor it. I don't mean go really fast and be a hoon, I mean use the math.floor(x) to round off the number
jeroentjuh99 #7
Posted 17 August 2013 - 05:54 AM
I'll try it without local then.

EDIT: thanks! It works now! Now I have to do some tweaking, but I found some useful stuff around here.
Edited on 17 August 2013 - 04:16 AM
jeroentjuh99 #8
Posted 17 August 2013 - 10:00 AM
I changed function receive() to this:

function receive()
if not rednet.isOpen("right") then
rednet.open("right")
end
senderID, msg, distanceFromSender = rednet.receive()
term.clear()
term.setCursorPos(1,1)
term.write(msg)
end
because I noticed every time I loaded my savegame, the turtles modem was off-line. It still is. How do I change that?
It's weird that I have to terminate the startup program and reboot the turtle every time I load the savegame :/
Grim Reaper #9
Posted 17 August 2013 - 02:27 PM
That is rather strange. My only suggestion would be to have the startup file open the modem for the turtle, run your script, then close the modem.

Something like this:

rednet.open ("right")
shell.run ("your_program")
rednet.close ("right")
immibis #10
Posted 17 August 2013 - 09:24 PM
I changed function receive() to this:

function receive()
if not rednet.isOpen("right") then
rednet.open("right")
end
senderID, msg, distanceFromSender = rednet.receive()
term.clear()
term.setCursorPos(1,1)
term.write(msg)
end
because I noticed every time I loaded my savegame, the turtles modem was off-line. It still is. How do I change that?
It's weird that I have to terminate the startup program and reboot the turtle every time I load the savegame :/

Computers reboot when you reload the savegame - there's nothing you can do about that.
I'll bet your modem also goes off-line if you reboot the turtle. You should fix that by opening the modem in your program.
jeroentjuh99 #11
Posted 18 August 2013 - 05:06 AM
I changed function receive() to this:

function receive()
if not rednet.isOpen("right") then
rednet.open("right")
end
senderID, msg, distanceFromSender = rednet.receive()
term.clear()
term.setCursorPos(1,1)
term.write(msg)
end
because I noticed every time I loaded my savegame, the turtles modem was off-line. It still is. How do I change that?
It's weird that I have to terminate the startup program and reboot the turtle every time I load the savegame :/

Computers reboot when you reload the savegame - there's nothing you can do about that.
I'll bet your modem also goes off-line if you reboot the turtle. You should fix that by opening the modem in your program.
It is getting opened at the start of the program. The first thing it does when the turtle (re)boot is checking if the modem is open, and if not, it opens it.
The weird thing in that is couldn't be opened.

But somehow it managed to fix itself. It starts online now.

Edit: It did not fix itself. I still get troubles getting the modem on. Any ideas?
Edited on 18 August 2013 - 05:53 AM