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

rednet message

Started by Brian87, 22 February 2014 - 10:16 AM
Brian87 #1
Posted 22 February 2014 - 11:16 AM
Hello!
I'm trying to run a program on a pc on another pc using rednet.

If I type start on the "master computer", the other computer will run a program called light which basically randomly creates redstone signals to redstone lamps.But I also want the pc to run startup if i type stop on the "master computer" and again wait until i write start. But it just wont work. :(/>

This is the code I've written:

"master pc":

while true do
print("start?")
input = read()
if input == "start" then
  rednet.open("right")
  rednet.broadcast("light")
  rednet.close("right")
  if input == "stop" then
   rednet.open("right")
   rednet.broadcast("startup")
   rednet.close("right")
  end
end
end

on the other computer I have two programs:


--startup
rednet.open()
message = rednet.receive()
rednet.receive()
shell.run(message)

and

--light
while true do
local side = {"right", "left", "top", "bottom", "front", "back"}
local random_side = side[math.random(1, #side)]
rs.setOutput(random_side, true)
sleep(math.random(0.5, 1))
rs.setOutput(random_side, false)
sleep(math.random(0, 0.5))
message = rednet.receive()
rednet.receive()
if message == "stop" then
shell.run(message)
end
CometWolf #2
Posted 22 February 2014 - 11:20 AM
Your startup program will end after it has performed one command, because it dosen't loop. Just throw in a loop after you open rednet. Also, change the shell.run(message) at the end of light to just "return" so it just ends the program. and you appear to be missing an end there aswell?
Edited on 22 February 2014 - 10:22 AM
Bomb Bloke #3
Posted 22 February 2014 - 11:31 AM
The "master"'s script first checks to see if "input" is "start", and only if that's true will it check to see if "input" is "stop" (which will always be false, given that we've already established that it's "start" in order to get to that point).

Try it like this:

while true do
  print("start?")
  input = read()
  if input == "start" then
    rednet.open("right")
    rednet.broadcast("light")
    rednet.close("right")
  elseif input == "stop" then
    rednet.open("right")
    rednet.broadcast("startup")
    rednet.close("right")
  end
end
Brian87 #4
Posted 22 February 2014 - 01:27 PM
It still doesn't seem to work :(/>
I'm starting startup on the computer, then I write "start" on the "master computer" and check the other computer and it says "no such program". I'm trying to start the light program manually nothing happens, I think it could be something with the light-program waiting for a receive before it does anything.

I've changed the code:

-- master computer
while true do
  print("start?")
  input = read()
  if input == "start" then
	rednet.open("right")
	rednet.broadcast("light")
	rednet.close("right")
  elseif input == "stop" then
	rednet.open("right")
	rednet.broadcast("startup")
	rednet.close("right")
  end
end

the other pc:

-- startup
rednet.open("top")
while true do
  message = rednet.receive()
  shell.run(message)
end
-- light
while true do
  local side = {"right", "left", "top", "bottom", "front", "back"}
  local random_side = side[math.random(1, #side)]
  rs.setOutput(random_side, true)
  sleep(math.random(0.5, 1))
  rs.setOutput(random_side, false)
  sleep(math.random(0, 0.5))
  message = rednet.receive()
  rednet.receive()
  if message == "stop" then
	return
  end
end
Bomb Bloke #5
Posted 22 February 2014 - 02:06 PM
Try "id, message = rednet.receive()" instead of "message = rednet.receive()".
Brian87 #6
Posted 22 February 2014 - 02:16 PM
That worked better :)/>
But it wont loop, I want it to randomly send redstone signal until it gets a message
Edited on 22 February 2014 - 01:22 PM
Bomb Bloke #7
Posted 22 February 2014 - 08:31 PM
Change this chunk:

  sleep(math.random(0.5, 1))
  rs.setOutput(random_side, false)
  sleep(math.random(0, 0.5))
  message = rednet.receive()
  rednet.receive()
  if message == "stop" then
        return
  end

… to just:

  id, message = rednet.receive(math.random(0.5, 1))
  rs.setOutput(random_side, false)
  if message == "stop" then return end
  id, message = rednet.receive(math.random(0, 0.5))
  if message == "stop" then return end

Passing a number to rednet.receive() causes it to wait the specified number of seconds (much the same as "sleep"). If that time passes and nothing comes in, then id/message get set to nil and the script carries onwards.

It'll react to random rednet traffic, but you're after a random effect anyway so that shouldn't matter.
Edited on 22 February 2014 - 07:35 PM
Brian87 #8
Posted 23 February 2014 - 04:35 AM
thank you! It works perfectly :)/>
Brian87 #9
Posted 23 February 2014 - 04:50 AM
By the way, how can I make it randomly choose for example to set output to one side, or two sides at once, or three sides at once etc. ?

Let's say I use this code:

while true do
  local side = {"right", "left", "top", "bottom", "front", "back"}
  id, message = rednet.receive(1)
end

how can I make it select for example "right" and "top" then pause for one second, and then for example "bottom", "left" and "right" etc ?
Edited on 23 February 2014 - 04:20 AM
Bomb Bloke #10
Posted 23 February 2014 - 05:41 AM
Make "random_side" a table, then make a "for" loop that goes from 1 to math.random(#side). With each iteration, add a random entry from "side" to the new table, table.remove() that same entry from "side", then enable the relevant redstone output.

When it comes time to disable the lamps, loop from 1 to #random_side and turn all entries off.
Brian87 #11
Posted 23 February 2014 - 05:51 AM
okay, thank you very much :)/>