21 posts
Posted 13 September 2012 - 01:07 AM
Hi there,
Okay i'm probably dumb but i'm trying to make that work for like 30 minutes and since the codes is soooo simple i can't believe i ask help for that. But i'm really not that good on lua. so… here is the thingy. With friend we actually make a little village for us to live and i would love to set-up a nice system for automatique lights. Problem is that i have set 1 computer "master" that detect redstone input from a light sensor of RedPower 2.
But the code don't seem to work. Ok i did not make the master programs for now since i was testing with broadcast and that still not work.
rednet.open("top")
message = rednet.receive()
if message == "on" then
print "test"
end
See… that pretty simple right? For now i was testing. print should be replace by rs.setOutput on any side to light up redstone lamp. But for some reason when i send message from my master it need 2 send for receiver to receive it and when it receive it it do nothing and just exit the programs.
So i'm pretty sure its a dumb error but if someone could explain me the error and how to solve it. I try to learn better :)/>/>
Thx you!
8543 posts
Posted 13 September 2012 - 01:11 AM
rednet.receive() returns two values, the ID of the computer that sent the message, and the message. You can simply change your rednet.receive() line:
local id, message = rednet.receive()
And then message will contain the message that was received!
21 posts
Posted 13 September 2012 - 01:17 AM
thx for your quick answer!
Now the code is
rednet.open("top")
local id, message = rednet.receive()
if message == "on" then
print "test"
end
And it actually print the test message (hurray!) but there still a problem. When i first broadcast message the receiving computer do nothing. So i go back to master, broadcast again (using lua) and not the receiver work and print the message. Is there any reason for that?
8543 posts
Posted 13 September 2012 - 01:33 AM
Was the receiving program running before the signal was broadcast the first time?
21 posts
Posted 13 September 2012 - 01:48 AM
yep, absolutely. And for some reason this is not the first time it do that. I have another programs that do that with rednet. And they are all on startup so they alway run and wait for information.
First time it look like the broadcast go no where and second time boom it receive it.
On another note i know that the programs work from top to bottom. is that possible to send the programs back to a specific like? Kind of a loop or something. Cause… Since it receive "on" message i would love the master to send back "off" message after a certain amount of time (when night end). But i don't want to have the programs reboot or it gonna turn-off the redstone signal. Or should i call back the programs with shell.run?
Again, Thx for your help!
8543 posts
Posted 13 September 2012 - 03:12 AM
You'd want to use a while loop. Something like:
rednet.open("top")
while true do
local id, message = rednet.receive()
if message == "on" then
rs.setOutput("back", true)
elseif message == "off" then
rs.setOutput("back", false)
end
end
21 posts
Posted 13 September 2012 - 03:31 AM
The programs client work pretty nicely with your help. So while true do is a loop, so programs don't end as it was. Good to know.
Still i can't figure out why all my broadcast don't "hit" the client. Every single time i have to send command on the master twice (or more. one time i did rednet.broadcast("off") 3 time in the lua interpreter and it finally close the light on the third time.
don't really understand why since they are pretty close.
8543 posts
Posted 13 September 2012 - 06:31 AM
I'm not sure what would cause that–I've never had problems with rednet signals getting 'lost', and I've worked relatively extensively with rednet.
21 posts
Posted 13 September 2012 - 11:44 PM
well… Since i can't find the problem i did find another way to solve it. But not sure if its the good way to do it.
Master code
rednet.open("top")
while true do
if rs.getInput("right") then
rednet.broadcast("on")
sleep(10)
else
rednet.broadcast("off")
sleep(10)
end
end
So if there is a redstone signal input it send "on" string every 10 seconde. And if there is not it send "off".
But do that is bad or create lot of lag or something? If yes i gonna have to find another way.
3790 posts
Location
Lincoln, Nebraska
Posted 13 September 2012 - 11:50 PM
No, sleeping every 10 seconds would not cause lag. If it did, then you have a horrible connection :)/>/>
This would not cause too many problems, unless working on a large server, where your rednet broadcasts may interfere with someone elses, or theirs interfering with yours. Otherwise, this is good! Nice simple setup to start with!
8543 posts
Posted 14 September 2012 - 12:57 AM
It would be better to use this, though:
rednet.open("top")
while true do
os.pullEvent("redstone")
if rs.getInput("right") then
rednet.broadcast("on")
else
rednet.broadcast("off")
end
end
This will wait for a change in the redstone state, then check the input side and broadcast the appropriate packet.
21 posts
Posted 14 September 2012 - 02:13 AM
Well, what is the notable change? I mean what do it do?
FYI i did finaly set the full system on the village. Master is up in the sky and the lamp post work like a charm. They turn on around 8pm. And since its broadcast system its expendable in the array of the master gps who is actually maxed by the hight in the sky.
8543 posts
Posted 14 September 2012 - 02:17 AM
The change is that instead of uselessly broadcasting the same thing every ten seconds, it only broadcasts when the input changes.
21 posts
Posted 14 September 2012 - 03:39 AM
yeah, but as i said if the broadcast is sent one time only the receive don't work. I always have to send it at least 2-3 time for receiver to actually receive the message.
I really don't understand why since i already try that on ssp and that don't do that problem but i had to adapt the code to make sure it work. In fact i did try that solution before with lever in my test area but every single time i sent the redstone signal the receiving compute did not catch it before my 2-3 activation of the lever.
that why i did use the 10 second loop. To be honest i'm not the most happy with that solution but i don't really have any other choice since the system don't behave :)/>/>