Servo with push button

Hello community!
I am new to pololu and I am sure this topic has been covered already somewhere. But i am attempting to: push a button and control 1 continuious servo (have it rotate a couple times to a position) stop wait about 5 secs and rotate back to the start position (i understand i am not using the proper lingo i’m just trying to be as clear as possible).

I tried using sample scripts and tried my own editing of scripts and i can’t get anything to work properly.

Any help would greatly be appreciated.

Hello.

What part of the behavior are you having problems getting to work properly? Could you post the script you have so far as well as a description of what it does?

By the way, continuous rotation servos sacrifice the ability to do closed-loop position control for the ability to turn continuously, so you will likely not have very precise control over when the servo stops. For example, you could have it rotate for a set amount of time, but if the load on the servo varies or is different when turning in the other direction, it might not end up in the same position it started in. If you need precise stopping positions, you might consider adding some kind of limit switches.

Brandon

I’ve gotten it board to register my switch and the servo will move after a button push. But not I cant get the servo to go through a sequence. (move, stop, move, stop). this is the full script below:

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 250 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
 
# a sequence of positions on servo 1.

 # 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
main_loop:
begin
  wait_for_button_press		  # call subroutine before executing sequence
  500 0 1000 0 0 0 frame # Frame 0
  500 3982 frame # Frame 1
  500 7970 frame # Frame 2
  500 7384 frame # Frame 3
  500 6240 frame # Frame 4
  500 5067 frame # Frame 5
  500 3968 frame # Frame 6
  500 5918 frame # Frame 7
repeat 
sub frame
  wait_for_button_press
  1 servo
  return

It looks like you probably used the sequencer to create your servo movements, converted it to a script, and tried to combine it with the example code for handling a button. I suspects something got lost during this process; in particular, it looks like your frame subroutine only uses one value from the stack (with the 1 servo line), but you are sending it 6 units with the first call in the loop and 2 arguments with each subsequent call. This results in values being added to the stack that never get used, which will quickly build up to the point where it will overflow and trigger an error.

Since you are using continuous rotation servos, to do what you described you will probably want to do something like this:

begin
    wait_for_button_press	  #wait for button before starting your loop
    7000 1 servo        #start rotating servo
    500 delay           #wait for half of a second for servo to rotate
    6000 1 servo        #stop servo
    5000 delay          #delay for 5 seconds before moving the servo the other direction
    5000 1 servo        #rotate servo back
    500 delay           #wait for servo to rotate
    6000 1 servo	    #stop servo
repeat

This BEGIN/REPEAT loop will wait for the button to be pushed, rotate the servo in one direction for half a second, pause for 5 seconds, then rotate back in the other direction for half a second. To stop a continuous rotation servo, you can send it a neutral position set target (e.g. 1500us or 6000 quarter microseconds). Please note that the actual neutral position for your continuous rotation servos might vary slightly; they typically have a potentiometer available somewhere on the case for making adjustments to this value.

Brandon