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

Rednet Send/Recieve Issues

Started by mjmiller814, 24 May 2014 - 05:22 PM
mjmiller814 #1
Posted 24 May 2014 - 07:22 PM
I've been racking my head all morning on this, can't figure it out.
Here's my design idea:

I have a Power Terminal, which acts as my primary control center for engines and all other machines
With a single computer, everything was working fine.
In order to output redstone signals to all my machines though, I needed to have my Power Terminal communicate to other computers, which then output the redstone signal.

For example, Power Terminal sends a message to Computer One to turn on power for engines, Computer One to turn on power to furnances, etc

Here's what I got:

Power Terminal:

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

local function turnOn(direction)
	    rs.setOutput(direction, true)
end

local function turnOff(direction)
	    rs.setOutput(direction, false)
end

local function setPos(y,x)
	    term.setCursorPos(y,x)
end

local function powerDown()
	    rs.setOutput("top", false)
	    rs.setOutput("bottom", false)
	    rs.setOutput("back", false)
	    rs.setOutput("front", false)
	    rs.setOutput("left", false)
	    rs.setOutput("right", false)
end

local function powerCheck(direction)
	    if rs.getOutput(direction) then
			    print("The status of the machine is: On")
	    else
			    print("The status of the machine is: Off")
	    end
end

while true do

rednet.open("top")

	    clear()
	    print("Welcome to the Power Terminal!")
	    setPos(1,3)
	    print("What machine would you like to control?")
	    setPos(1,5)
	    print("1 - Furnace")
	    print("2 -")
	    print("3 -")
	    print("4 -")
	    print("5 -")
	    print("6 -")
	    print("7 -")
	    print("8 -")
	    print("9 - Engines")
	    print("0 - Power Down")

	    local event, command = os.pullEvent("char")

	    if command == "1" then
			    clear()
			    powerCheck("top")
			    setPos(1,3)
			    print("1 - On")
			    print("0 - Off")

			    local event, option = os.pullEvent("char")

					    if option == "1" then
							    turnOn("top")
							    clear()
							    print("Furnace has been turned on")
							    sleep(2)
					    elseif option == "0" then
							    turnOff("top")
							    clear()
							    print("Furnace has been turned off")
							    sleep(2)
					    end
	    end

	    if command == "2" then
			    clear()
			    print("No device is assigned to 2")
			    sleep(2)
	    end

	    if command == "3" then
			    clear()
			    print("No device is assigned to 3")
			    sleep(2)
	    end

	    if command == "4" then
			    clear()
			    print("No device is assigned to 4")
			    sleep(2)
	    end

	    if command == "5" then
			    clear()
			    print("No device is assigned to 5")
			    sleep(2)
	    end

	    if command == "6" then
			    clear()
			    print("No device is assigned to 6")
			    sleep(2)
	    end

	    if command == "7" then
			    clear()
			    print("No device is assigned to 7")
			    sleep(2)
	    end

	    if command == "8" then
			    clear()
			    print("No device is assigned to 8")
			    sleep(2)
	    end

		 if command == "9" then
			    clear()
			    setPos(1,3)
			    print("1 - On")
			    print("0 - Off")

			    local event, option = os.pullEvent("char")

					    if option == "1" then
							    rednet.send(11, "1")
							    print("The engine  has been turned on")
							    sleep(2)
					    elseif option == "0" then
							    rednet.send(11, "0")
							    print("The engine has been turned off")
							    sleep(2)
					    end
	    end

	    if command =="0" then
			    clear()
			    powerDown()
			    print("All machines have been powered down")
			    sleep(2)
	    end
end

Computer One:

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

local function turnOn(direction)
	    rs.setOutput(direction, true)
end

local function turnOff(direction)
	    rs.setOutput(direction, false)
end

local function setPos(y,x)
	    term.setCursorPos(y,x)
end

local function powerDown()
	    rs.setOutput("top", false)
	    rs.setOutput("bottom", false)
	    rs.setOutput("back", false)
	    rs.setOutput("front", false)
	    rs.setOutput("left", false)
	    rs.setOutput("right", false)
end

local function powerCheck(direction)
	    if rs.getOutput(direction) then
			    print("The status of the machine is: On")
	    else
			    print("The status of the machine is: Off")
	    end
end

while true do
	    rednet.open("top")
	    rednet.receive()

	    local id, command = rednet.receive()

	    if id == 10 and command == "1" then
					    rs.setOutput("right", true)
	    elseif id == 10 and command == "0" then
					    rs.setOutput("right", false)
	    end
end

For the Power Terminal, the code I'm focusing on is the "Command 9" section, for the engines. "Command 1" still exists because I was testing the code with a single computer.
So here's the problem…the code above 'works' - sort of. When I got to turn the Engines on, I have to execute the command twice, before it'll turn on. Same with turning off.

Any advice would be greatly appreciated.
CCJJSax #2
Posted 25 May 2014 - 02:20 AM
I'm not the most familiar with rednet, but I'm pretty sure your issue is with computer one.

I think you have rednet.receive() in there twice, when you only need one.

while true do
		    rednet.open("top")
            -- rednet.receive() -- I think if you take this one out, you will be golden.
		    local id, command = rednet.receive()
		    if id == 10 and command == "1" then
										    rs.setOutput("right", true)
		    elseif id == 10 and command == "0" then
										    rs.setOutput("right", false)
		    end
end