Adding a switch to start a counted loop with a Micro Maestr

Below is my script to move 2 servos through a routine 5 times started by pressing a switch. It works with the counter and no switch and it works with the switch and no counter. When I put the 2 together (switch and counter) I get an error 0x0080. Any help to remedy this problem would be great

wait_for_button_press

 5 # start with a 5 on the stack 

begin

dup # copy the number on the stack - the copy will be consumed by WHILE

while # jump to the end if the count reaches 0

# Move servo 0 180 deg and back (lift filter and set it back down)

 500 delay
 100 0 servo # 2.00 ms
 500 delay
 9000 0 servo
 500 delay

# rotate servo 1  deg and back 3 times (twist filter back and forth)

 1000 1 servo
 500 delay
 5000 1 servo
 500 delay
 1000 1 servo
 500 delay
 5000 1 servo 
 500 delay
 1000 1 servo
 500 delay
 5000 1 servo
 500 delay
 1 minus # subtract 1 from the number of times remaining
repeat
 
# Returns 1 if the button is pressed, 0 otherwise. 
sub button
2 get_position 500 less_than 
return 

# Waits for a button press, with debouncing.     
# (Requires the BUTTON subroutine.) 
sub wait_for_button_press 
wait_for_button_open_10ms 
wait_for_button_closed_10ms 
return 
# wait for the button to be NOT pressed for at least 10 ms    
sub wait_for_button_open_10ms  
get_ms # put the current time on the stack 
begin 
# reset the time on the stack if it is pressed 
button 
if 
drop get_ms 
else 
get_ms over minus 10 greater_than 
if drop return endif 
endif 
repeat 
# wait for the button to be pressed for at least 10 ms 
sub wait_for_button_closed_10ms 
get_ms 
begin 
# reset the time on the stack if it is pressed 
button 
if 
get_ms over minus 10 greater_than 
if drop return endif 
else 
drop get_ms 
endif 
repeat 

Hello.

It looks like after the counter reaches zero and your script exits the while loop, it triggers a stack overflow error because it is continuing on to the “button” subroutine. One way to fix this is to use a nested BEGIN/REPEAT loop to direct the script to return where you want it to. Alternatively, if you want your script to stop running after the counter reaches zero, you could put a QUIT command after your REPEAT command. The GOTO command could also be used to redirect your script to the appropriate location once the counter reaches zero.

If you try one of these solutions and still run into problems, you can post your updated code here, and I would be happy to offer more help.

-Brandon