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

How do I use rednet to make a two way opening door?

Started by chucky_egg, 27 February 2013 - 07:25 AM
chucky_egg #1
Posted 27 February 2013 - 08:25 AM
title: How do I use rednet to make a two way opening door?
Hi, I wanna use rednet so that i can send a message to a computer operating a redstone signal, which when it receives the phrase "open" it shall turn off the redstone and open the door. I want this to be done from two computers, so that on either - i can send a signal to the computer and it shall turn off the redstone signal. I guess that i just put the same program on each computer but what program is that?


thanks
Lyqyd #2
Posted 27 February 2013 - 08:48 AM
Split into new topic.
SuicidalSTDz #3
Posted 27 February 2013 - 09:27 AM
This will be the computer receiving the rednet_message:


local open = "word that will trigger the door to open"
local close = "word that will trigger the door to close"
local side = "side the door is on, touching the computer"
while true do
  local message, senderId = os.pullEvent("rednet_message")
  if message == open then
	rs.setOutput(side,true)
  elseif message == close then
	rs.setOutput(side,false)
  end
end

This will be the computer sending the rednet_message:


local id = "the receiving computer's ID"
local open = "what word will trigger the door to open"
local close = "what word will trigger the door to close"
while true do
  local state = read()
  if state == open then
	rednet.send(id,open)
  elseif state == close then
	rednet.send(id,close)
  end
end

The open and close variables MUST be defined the same, if open is "Test" then open on the receiving side must be "Test". Same goes for close. This should do exactly what you want as stated in the OP.

EDIT: You can do the easy part and code this to turn on the modem ;)/>
chucky_egg #4
Posted 27 February 2013 - 10:00 AM
Awesome, thanks bro! Just one more question. How do I know what the computers ID is? I can turn the routers and stuff on ill just edit the start up. Also, should I put these under a specific code name? Or just ask the shell to run that at startup. So like I put that into the startup as like
rednet.open(side)
shell.run(program name)
Or should I do something else? For the receiving computer of course.
Edit: does this also leave the door open for a few seconds? Or would I have to alter that?
SuicidalSTDz #5
Posted 27 February 2013 - 10:32 AM
Awesome, thanks bro! Just one more question. How do I know what the computers ID is? I can turn the routers and stuff on ill just edit the start up. Also, should I put these under a specific code name? Or just ask the shell to run that at startup. So like I put that into the startup as like
rednet.open(side)
shell.run(program name)
Or should I do something else? For the receiving computer of course.
Edit: does this also leave the door open for a few seconds? Or would I have to alter that?

To check the computer's id, simply type:

id

Or use this within your code if you want to find out the ID that way(Sometimes useful, but not so much in this situation)

id = os.getComputerID

I would name the receiving computer program as "startup" so it runs whenever the computer restarts. The other program may be named whatever.

If you want the door to stay open for an amount of time, we will ditch the close variable and use this instead for the recieving side:

local open = "word that will trigger the door to open"
local delay = --A number for the delay in seconds
local side = "side the door is on, touching the computer"
while true do
  local message, senderId = os.pullEvent("rednet_message")
  if message == open then
	rs.setOutput(side,true)
	sleep(delay)
	rs.setOutput(side,false)
  end
end

You can have an infinate number of computers relaying the message to the recieving end, however, the ID variable MUST be the same for the message to go through. Of course if two computers sent the message at the same time, that could raise errors. I suggest sticking with two-four computers that relay the rednet_message to the reciever for less of a chance to send two messages at once.
remiX #6
Posted 27 February 2013 - 05:59 PM
This will be the computer receiving the rednet_message:


local open = "word that will trigger the door to open"
local close = "word that will trigger the door to close"
local side = "side the door is on, touching the computer"
while true do
  local message, senderId = os.pullEvent("rednet_message")
  if message == open then
	rs.setOutput(side,true)
  elseif message == close then
	rs.setOutput(side,false)
  end
end

Won't work

local message, senderId = os.pullEvent("rednet_message")
is completely wrong…

Spoiler
local event, senderId, message = os.pullEvent("rednet_message")
SuicidalSTDz #7
Posted 27 February 2013 - 06:04 PM
This will be the computer receiving the rednet_message:


local open = "word that will trigger the door to open"
local close = "word that will trigger the door to close"
local side = "side the door is on, touching the computer"
while true do
  local message, senderId = os.pullEvent("rednet_message")
  if message == open then
	rs.setOutput(side,true)
  elseif message == close then
	rs.setOutput(side,false)
  end
end

Won't work

local message, senderId = os.pullEvent("rednet_message")
is completely wrong…

Spoiler
local event, senderId, message = os.pullEvent("rednet_message")
Forgot the event again… Wait, it was there a second ago. Mustv'e taken it out for some reason? Oh well.
remiX #8
Posted 27 February 2013 - 06:19 PM
Forgot the event again… Wait, it was there a second ago. Mustv'e taken it out for some reason? Oh well.

Haha :P/> you also swapped message and senderid