Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Momentary Button

Hi, I’m more of a prop builder, and get stuck up on code.

I’m trying to press a button, the servos run a sequence and move to position-only holding that position while the momentary button is still being pressed. The moment the button is released, the servos follow sequence and return to their resting position.

And so of course I’ve got coding problems that I don’t understand
Here is the code I’ve got:

 goto main_loop    # Run the main loop when the script starts (see below).
 # This subroutine returns 1 if the button is pressed, 0 otherwise.
 # To convert the input value (0-1023) to a digital value (0 or 1) representing
 # the state of the button, we make a comparison to an arbitrary threshold (500).
 # This subroutine puts a logical value of 1 or a 0 on the stack, depending
 # on whether the button is pressed or not.
 sub button
   0 get_position 500 less_than
   return
  # This subroutine uses the BUTTON subroutine above to wait for a button press,
 # including a small delay to eliminate noise or bounces on the input.
 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 not pressed
     button
     if
       get_ms over minus 10 greater_than
       if drop return endif
     else
       drop get_ms
     endif
   repeat
 # the servo sequence
 # Sequence 0
 main_loop:
 begin
 if button == closed
   800 4020 8000 8000 8000 8000 frame_1..5 # Frame 0
 delay
   800 4020 3968 8000 8000 8000 frame_1..5
 delay
   800 4020 3968 3968 8000 8000 frame_1..5
 delay
   800 4020 3968 3968 3968 8000 frame_1..5
 delay
   800 4020 3968 3968 3968 3968 frame_1..5
 else
   800 5960 8000 8000 8000 8000 frame_1..5 # Frame 1
 repeat
 sub frame_1..5
   5 servo
   4 servo
   3 servo
   2 servo
   1 servo
   delay
   return

Hello.

If I’m understanding what you want to do, you should be able to use the wait_for_button_closed_10ms and wait_for_button_open_10ms subroutines to accomplish that. Your main_loop would look something like this:

main_loop:
begin

  wait_for_button_closed_10ms  #do nothing until the button is pressed

    800 4020 8000 8000 8000 8000 frame_1..5 # Frame 0
 
    800 4020 3968 8000 8000 8000 frame_1..5

    800 4020 3968 3968 8000 8000 frame_1..5
 
    800 4020 3968 3968 3968 8000 frame_1..5

    800 4020 3968 3968 3968 3968 frame_1..5

  wait_for_button_open_10ms  #hold the servos at their position until the button is released
  
    800 5960 8000 8000 8000 8000 frame_1..5 # Frame 1

 repeat

Please note that I removed the delay commands between each of the calls to frame_1..5 because the delay is already being done inside of the subroutine, and the extra delay was causing errors.

If that is not what you were intending, could you try describing it again in more detail and post a copy of your Maestro settings file? You can save a copy of your settings file from the “File” drop-down menu of the Maestro Control Center while the controller is connected.

Brandon

thank you Brandon, I’ll give that a try