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

creating a loop with BundledInput

Started by Fognar777, 27 June 2015 - 09:55 PM
Fognar777 #1
Posted 27 June 2015 - 11:55 PM
I am trying to create a security system program that runs whenever my hidden front door is closed. So heres my code:

While
redstone.getBundledInput(“bottom”, colors.cyan)  == true do
  repeat
   until  redstone.getBundledInput(“bottom”, colors.lightgrey)  == true
	redstone.setBundledOutput(“bottom”, colors.grey)
	sleep(20)
end
so my door closing program ends by setting cyan to true and I can use redprobe to see that its getting to the console that I am using for my security program but when I run my security program instead of it creating a infinite loop checking for a signal on lightgrey(which is my pressure plates) it ends immediately with no error code like its not looping at all? what am I doing wrong?
jerimo #2
Posted 28 June 2015 - 01:18 AM
I believe you don't need the while do in Your statement since you already have the repeat in there?

Edit: just checked and yeah its
repeat
Code here
Until statement[\code]

And not [code]while statement do repeat until statement[\code]
Edited on 27 June 2015 - 11:21 PM
Bomb Bloke #3
Posted 28 June 2015 - 01:23 AM
For starters you're typing "while" with a capital "W". It needs to be lowercase, as Lua is case-sensitive. And be careful not to allow those "smart quotes". Also, it's either "colours.lightGrey", or "colors.lightGray"; there is no "colors.lightgrey".

Assuming most of those typos didn't make it into your actual script file, consider what will happen if you run it while the cyan input is off: Lua will check the condition of your "while" loop, see that it resolves to "false", and skip past the whole thing. There's no more code after that, and so the script ends.

It appears you were going for something along these lines:

while true do
	if rs.testBundledInput("bottom", colours.combine(colours.cyan, colours.lightGrey)) then
		rs.setBundledOutput("bottom", colours.grey)
		sleep(20)
		rs.setBundledOutput("bottom", 0)
	end
	
	os.pullEvent("redstone")  -- Yield until a redstone state change occurs.
end
Edited on 27 June 2015 - 11:24 PM
Fognar777 #4
Posted 28 June 2015 - 02:01 PM
Thanks for all the suggestions guys I ending up figuring out that I needed to do testBundled not getBundled to get a boolean value. So this is my revised code:
 
[color=#660066]while[/color]
[color=#000000] redstone[/color][color=#666600].test[/color][color=#000000]BundledInput[/color][color=#666600](“[/color][color=#000000]bottom[/color][color=#666600]”,[/color][color=#000000] colors[/color][color=#666600].[/color][color=#000000]cyan[/color][color=#666600])[/color][color=#000000]  [/color][color=#666600]==[/color][color=#000000] [/color][color=#000088]true[/color][color=#000000] [/color][color=#000088]do[/color]
[color=#000000] while
  [/color][color=#000000] redstone[/color][color=#666600].test[/color][color=#000000]BundledInput[/color][color=#666600](“[/color][color=#000000]bottom[/color][color=#666600]”,[/color][color=#000000] 256[/color][color=#666600])[/color][color=#000000]  ~[/color][color=#666600]=[/color][color=#000000] [/color][color=#000088]true
[/color]   do
   sleep(.1)
    end
[color=#000000]  redstone[/color][color=#666600].[/color][color=#000000]setBundledOutput[/color][color=#666600](“[/color][color=#000000]bottom[/color][color=#666600]”,[/color][color=#000000] 128[/color][color=#666600])[/color]
[color=#000000]  sleep[/color][color=#666600]([/color][color=#006666]20[/color][color=#666600])
  redstone.setBundledOutput(“bottom”, 0)
end
[/color][color=#000088]

this works except for the problem of it ending and not restarting on its own when the cyan is off. I was considering just putting this code into startup and just putting a reboot command in the end and forcing it to loop that way but your suggestion should work as well Bomb bloke.
HPWebcamAble #5
Posted 28 June 2015 - 06:28 PM
this works except for the problem of it ending and not restarting on its own when the cyan is off


while true do

  while redstone.testBundledInput(“bottom”, colors.cyan) == true do
    while redstone.testBundledInput(“bottom”, 256) ~= true do
      sleep(.1)
    end
    redstone.setBundledOutput(“bottom”, 128)
    sleep(20)
    redstone.setBundledOutput(“bottom”, 0)
  end

end