Two buttons to move one servo step by step

Hello!

I have searched and tried modifying example scripts with no success… I am a beginner in programming…

I would like to move one servo clockwise with button 1 and anti clockwise with a push on button 2.

The goal is to have 10 defined positions that button 1 & 2 steps though. One step at the time at either way. When I reach the end I want the servo to stop until the other button is pushed.

To explain more easy - I want to change gears on a bicycle with + and - buttons.

Please help!

Hello.

I would suggest looking at the “Using multiple buttons or switches to control servos” example script and using it as a starting point. The main BEGIN/REPEAT block would probably not need to be changed other than removing the button_c if sequence_c endif line of code. If you take out the rest of the code for button c, you are left with four subroutines: button_a, button_b, sequence_a, and sequence_b. Then, you would need to change the sequence_a and sequence_b subroutines to do what you want when their respective buttons are pushed.

There are probably a lot of ways to get 10 discrete positions and have two buttons step through them one at a time. One way to do it would be to check the position of the servo with the get_position command and add or subtract a particular value from it (depending on which button was pushed). For example, sequence_a might look like this:

sub sequence_a
  2 get_position       #check the position of servo 2
  448 plus             #increase the position
  2 servo              #send servo 2 to the new position
  150 delay
return

The amount you increase or decrease the position per button push would depend on the min and max settings for that servo in the “Channel Settings” tab of the Maestro Control Center. In the example I gave above, the 448 quarter microseconds value was based on the default 992us and 2000us min and max values respectively (e.g. (2000-992)*4/9). Also, please note that since the min and max settings limit the position of the servo, pressing button A when the servo is already at a position of 2000us would not cause a change. The same goes for button B if the servo is already at 992us.

If you try to make these changes and run into problems, you can post the code you have so far, and I would be glad to take a look.

Brandon

Thanks a lot, it works great!

I added some extra movement to overcome backlash. Now it´s time to go mechanical!

    # When the script is not doing anything else,
    # this loop will listen for button presses.  When a button
    # is pressed it runs the corresponding sequence.
    begin
      button_a if sequence_a endif
      button_b if sequence_b endif
    repeat
     
    # These subroutines each return 1 if the corresponding
    # button is pressed, and return 0 otherwise.
    # Currently button_a is assigned to channel 0, 
    # button_b is assigned to channel 1, and
    # button_c is assigned to channel 2.
    # These channels must be configured as Inputs in the
    # Channel Settings tab.
    sub button_a
      4 get_position 500 less_than
      return
     
    sub button_b
      5 get_position 500 less_than
      return
     
     
    # These subroutines each perform an arbitrary sequence
    # of servo movements.  You should change these to fit
    # your application.
    sub sequence_a
      1 get_position       #check the position of servo 2
      548 plus             #increase the position
      1 servo              #send servo 2 to the new position
      200 delay
      1 get_position       
      -100 plus             #increase the position
      1 servo              #send servo 2 to the new position
      10 delay
    return
       
    sub sequence_b
      1 get_position       #check the position of servo 2
      -548 plus             #increase the position
      1 servo              #send servo 2 to the new position
      150 delay
      1 get_position       
      100 plus             #increase the position
      1 servo              #send servo 2 to the new position
      10 delay
    return

I am glad you were able to get it working how you want it to! Thank you for letting us know and sharing your code.

If you are willing to share more about your project when it is complete, you might consider posting about it in the “Share your projects” section of the forum. I am sure other members would also be interested.

Brandon