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

Pololu Forum

Needing help adding a button

I’ve been using the example script for using a button and it has been working great to go to the next step. Now I want to add a button so that when I push this second button the controller will do the previous step instead of the next step like the first button is doing. Can someone help me with this please?

Hello.

I moved your post to the “Servo controllers and servos” category of the forum since it is specific to the Maestro.

Unfortunately, adding the multiple button feature you described would not be a trivial modification to the “Using a button or switch to control servos” example from the “Example scripts” section of the Maestro user’s guide. There are a few ways you could write that kind of script. How many positions are you switching between? Have you tried writing a script that does what you described? If so, could you post what you have here?

Brandon

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
 
# An example of how to use wait_for_button_press is shown below:
 
# Uses WAIT_FOR_BUTTON_PRESS to allow a user to step through
# a sequence of positions on servo 1.
main_loop:
begin
  2000 frame
  2000 frame
  2000 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  9800 frame
  2000 frame
  2000 frame
  2000 frame
repeat
 
sub frame
  wait_for_button_press
  1 servo
  return

This is what I have at the moment.

You may be wondering why some many 9800 are in a row and then why so many 2000 are in a row. The reason for this is because there are only two positions the servo needs two be in during the cycle and the button/switch gets pushed 21 times each cycle. It is critical that it stays in rhythm, three 2000’s, fifteen 9800’s, three 2000’s, and then repeat all over again. This is all working great, but now I’m wanting to update the script so that at any point in the cycle if a second button is pushed it will do the previous action.

Hello.

As I mentioned before, it would not be trivial to have that particular script “reverse” like you are describing because of the way it is set up.

With only two positions for the servo, but 21 button states, it is probably easier to write a new script from scratch that essentially uses a dummy servo channel as a state variable. To do that, you can choose a servo channel you are not using (e.g. channel 5) and use the target position of that channel as a reference (reading it with the GET_POSITION command and updating it with the SERVO command).

If you are interested in this method, I would encourage you to give it a try. Since you are new to Maestro scripting, I recommend starting slow; for example, writing a script that just uses two buttons (without debouncing), and increases or decreases the dummy servo’s position by 1 depending on which button is pressed. If you try this and run into problems, you can post what you have so far along with your question and I would be happy to help.

Brandon