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

Project Red Loop

Started by Durahansolo, 27 June 2015 - 01:38 PM
Durahansolo #1
Posted 27 June 2015 - 03:38 PM
I'm trying to create a program that will verify whether a specific color input is on/off and if its on/off to provide or turn off power to a specific wire. It seems to work fine by itself, but when a loop is added it stops looping after the switch gets turned on/off.

This is what I have:



while true do
sleep(0)
	 if redstone.getBundledInput("left") == 512 then
	 redstone.setBundledOutput("left", 16384, red)
	 print("House 1 Lighting is On.")
	 sleep(2)
	 elseif redstone.getBundledInput("left") == 0
	 redstone.setBundledOutput("left, 0, red)
	 print("House 1 Lighting is Off.")
	 sleep(2)
end
end

also tried this which works until power turns on/off once then it doesn't work


while true do
	 event = os.pullEvent()
if event == "redstone" and rs.getBundledInput("left") == 512 then
redstone.setBundledOutput("left", 16384, red)
print("House 1 Lighting is On.")
sleep(0)
end
if event == "redstone" and rs.getBundledInput("left") == 0 then
redstone.setBundledOutput("left", 0, red)
print("House 1 Lighting is Off.")
end
sleep(0)
end
LBPHacker #2
Posted 27 June 2015 - 10:45 PM
It's been quite a while since I used the bundled functions of the redstone API, but back then rs.getBundledInput would always return all the colours present in the cable. By that I mean that rs.getBundledInput reports all the wires pulled high by other machines connected to the bundled cable to be high and the ones you pull high using rs.setBundledOutput as well.

If that's still how it works these days, I believe pulling the 16384 line high would prevent any of your branches from being taken again as the bundled input will never again be 0 or 512. So if by "stops looping" you mean you never see any of those messages you're printing again, this might be the cause of that.

Also, I've checked the wiki and rs.setBundledOutput doesn't seem to accept more than two parameters (well, it may accept them, but it really just doesn't care if you pass more than two).

EDIT: You don't need the sleep(0)'s in there.
Edited on 27 June 2015 - 08:46 PM
Bomb Bloke #3
Posted 28 June 2015 - 01:01 AM
Once you light your red wire, rs.getBundledInput() subsequently sees it as lit. That means the value of the input is no longer going to be equal to cyan or nothing; it's going to be equal to cyan + red, or it's going to be just red. Hence we use rs.testBundledInput(), which is designed to check whether a certain colour (or certain combination of colours) can be found as "active", ignoring the others.

while true do
	if rs.testBundledInput("left", colours.cyan)
		rs.setBundledOutput("left", colours.red)
		print("House 1 Lighting is On.")
	else
		rs.setBundledOutput("left", 0)
		print("House 1 Lighting is Off.")
	end
	
	os.pullEvent("redstone")
end

Though it strikes me that it'd be easier to simply switch all your red wiring with cyan, since your logic boils down to "make the red state match the cyan state". You could alternatively just pull the two colours out of your bundled cable and link them with a patch of regular dust, it'd have the same effect.
Durahansolo #4
Posted 28 June 2015 - 01:22 AM
I ended up swapping the output to the backside, it seems to work now.