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

Wireless Redstone Pulse loop then exit loop

Started by tmoflash, 03 June 2016 - 04:27 AM
tmoflash #1
Posted 03 June 2016 - 06:27 AM
I have a network of computers and I can get them talking and I can also get a loop going using redstone, however, I cannot get the loop to exit. It either runs loop continuously or only runs through one time.


----THIS IS TRANSMITTING COMPUTER-----
######Transmitter#####
rednet.open("back")
x = tmoflash
while x ~= "exit" do
term.clear()
term.setCursorPos(1,1)
print("#####################")
print("										  ")
print("	 Lamp Control			   ")
print("										  ")
print("    1 = on  2 = off			    ")
print("	 exit to quit				    ")
print("#####################")
x = io.read()
  if x == "1" then
   rednet.send(861, "on")
  end
  if x == "2" then
   rednet.send(861, "off")
  end
end

----THIS IS RECEIVER COMPUTER-----
######Receiver#####
rednet.open("back")
x = "0"
id, message = rednet.receive(10)
if id == 855 and message == "on" then
	    repeat
	    rs.setOutput("left", true)
	   sleep(1)
	   rs.setOutput("left", false)
	   sleep(1)
	   until x == "2"
else
	   if id == 855 and message == "off" then
	   rs.setOutput("left", false)
	   end
end
Bomb Bloke #2
Posted 03 June 2016 - 08:00 AM
Nothing within your "repeat" loop attempts to check the content of further messages.

You could add another "rednet.receive()" in there, but there's a bit of a problem in that the use of the "sleep()" function may still cause you to miss messages. Anything received while sleeping is going to be discarded.

See, the idea is that both sleep() and rednet.receive() operate by pulling events, for which they make use of the os.pullEvent() function. sleep() starts a timer, and pulls events from the front of the event queue until it finds a matching timer event. rednet.receive() operates in a similar manner, but it's more interested in rednet_message events.

So sleep() / rednet.receive() don't work well together (as each function pulls and discards events from the queue that the other function might want). The solution is to avoid the both of them, and instead manage the events yourself - starting the needed timers, and so on.

For eg:

######Receiver#####
rednet.open("back")

local myTimer

--# "active" tracks whether the redstone should pulse.
--# "curState" tracks whether the redstone should be specifically off or on.
local active, curState = false, false

while true do
	local event, par1, par2 = os.pullEvent()
	
	if event == "rednet_message" and par1 == 855 then
		if par2 == "on" then
			active, curState = true, true
			rs.setOutput("left", true)
			myTimer = os.startTimer(1)
		
		elseif par2 == "off" then
			active = false
			rs.setOutput("left", false)
		
		end
	
	elseif event == "timer" and par1 == myTimer and active then
		curState = not curState
		rs.setOutput("left", curState)
		myTimer = os.startTimer(1)
	
	end
end
tmoflash #3
Posted 03 June 2016 - 02:11 PM
This worked perfect, thank you.