Fixed-term LOOP ?


Good evening to all of you,
I am looking to make a LOOP of a determined duration and 1) EXIT this loop as soon as a button has been activated or 2) light a red led if this button has not been activated during the determined time ... and continue the rest of the program.

This would be the principle of arming an Alarm.
(The program does not loop. This is a single check of an action to be performed within a specified time.)

Start of the program by supplying the Picaxe
So We have 20 seconds to push a button. (System arming)
1) If the button is activated during this period, we exit the 20 second loop (we do not wait for the end of the 20 seconds) and a green LED is lit.
2) If this 20 second period has elapsed, the button has not been activated: A red LED lights up.
And we continue, outside this timed loop, until the end of the program.

I think the BUTTON instruction might be the best one, but I don't understand how to calibrate exactly how long I want.
Would another method be better?
Already, Thanks to the Team.


Senior Member

I'd just use a simple "counting" loop with a Pause to set the time delay. That could also toggle (flash) a LED to indicate that some response is required. Using symbols for a variable (e.g b2), the required PICaxe pins, and a constant (e.g. pressed = 1), it might be :
for counter = 0 to 100
    if inputbutton = pressed then {goto} armed
    toggle LED     ; Optional
    pause 200      ; loop for 20 seconds / 100  = 200ms
next              ; then "fall through"
    High REDLED
;  ......   rest of program
;....... etc.
Cheers, Alan.


Hi AllyCat,
It's indeed a good idea to use the Goto instruction to get out of a For/Next loop, which I had for a moment thought of using...
In fact, I often avoid using this GoTo instruction but it seems to be a must to get out of a loop different from a Do/Loop which has an Exit instruction!
Unless I am mistaken, I think that a GoTo instruction does not load the "operational memory". ("Pile opérationnelle" in french)
Thank you for the quick response to this particularly active and high quality forum.
Take care of yourself


The EXIT command with work with both the For Next and the Do Loop structures.

However you cannot select where program execution continues from as can be done using GOTO.
With EXIT, the program resumes immediately after the NEXT or LOOP instruction.