Conditional Loop Question


#1

I’m trying to design a piston that will repeat a certain number of times OR until successful for locking a door.

I keep bouncing back and forth between using the For Loop and Repeat Loop. The problem is I don’t know how to end either one when successful.

For instance, my piston locks a door. After it sends the lock command I plan to have it wait 10 seconds, then evaluate the lock condition. If the lock is locked, no action taken. If the lock is still unlocked or jammed, then I want it to retry locking 5 times. So, unlock, wait a few seconds, lock. Repeat 5 times, OR, if successful, end.

If I use the Repeat Loop it seems it will do it infinitely until it locks, which I don’t want. If I use the For loop, it seems it will do it 5 times even if it locks on the 3rd try for instance.

What obvious solution to this problem am I missing? Thanks in advance!

EDIT: Here’s what I have. No idea if it works or not yet.


#2

Just off the top of my head, you could use a boolean variable that is true when locked. Then you could use that variable as the termination of a Repeat Until loop. The piston will require more logic within the loop to meet your limitations.


#3

why not:

while lock is NOT locked
  with lock
    lock
    wait 10 seconds
  end with
end while

This will keep going as long as the condition is true (not locked) and as soon as it confirms locked, will stop. I use this whenever I want to be sure something completes successfully.


#4


#5

Here’s what I use:

Essentially, this piston fires after (5) minutes of the door being unlocked OR nightly, prior to going to bed for security insurance. For the specific loop question you asked… I use a Repeat Loop - with an ‘until’ condition check. It loops until successful (door is locked) OR until it makes (5) attempts - as set by a ‘MaxLockCycles’ variable. The setting of a maximum number of attempts prevents the piston of looping ‘forever’ under unusual circumstances. The ‘LockCounter’ variable counts the attempted number of cycles. After each completed run, it logs successes and if it requires more than one attempt, it sends SMS notifications to me. If unsuccessful after (5) attempts, it sends out ST notifications to both my wife’s and my phone as well.

I use similar logic for setting thermostat heat and cool setpoints.


#6

I’ve used a While loop in my pistons, but otherwise, mine is the same as yours. I also do the same for shutting things off. In my house, I find that about 5-10% of the time, some switches need 2 or 3 attempts before they finally turn on or off.

One suggestion: When the piston starts executing, you should set your loop counter to 0 (but not in the Define area). I’m not sure the value that it will have when you repeatedly execute this piston.


#7

you can use the ‘break’ command to exit the loop. you have to test some condition/variable, then if true
‘break’

Break is part of "advance statements’