Problem Trying to Make 2 Servos Sweep Only Once

Hi,
I just got my Arduino Uno last week and have been playing around with it. For my project, I need to make two servos sweep from 0 to 180 degrees and back, once (by pushing a button). I wired everything up and found good code and modified it so that it seems to work, however after one button push, the servo keeps rotating. If I press the button again, at the exact right time (just before the servos start the second sweep) the servos stop, but otherwise, they keep going. Below is the code. Any thoughts would be much appreciated! Thanks!

#include <Servo.h>

Servo myservo; // make a new servo object
Servo myservo2; // make a new servo object number 2

int buttonPin = 2;  // the input pin where the
                    // pushbuttonPin is connected
int buttonState = 0;      // val will be used to store the state
                          // of the input pin 
int old_buttonState = 0;  // this variable stores the previous
int pos = 0;
int delayVal = 18;  // Sort of the "speed" of the servo

void setup() { 
  pinMode(buttonPin, INPUT); // and buttonPin is an input 
  myservo.attach(9);   // declare the output pin for the servo
  myservo2.attach(10);
  myservo.write(180);         // Set the initial position of the servo
  myservo2.write(180);
} 

void loop(){ 
  buttonState = digitalRead(buttonPin); // read input value and store it 


  // check if there was a transition 
  if ((buttonState == HIGH) && (old_buttonState == LOW)) { 

    for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
    {                                  // in steps of 1 degree 
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      myservo2.write(pos);
      delay(delayVal);                       // waits 15ms for the servo to reach the position 
    } 
    for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
    {                                
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      myservo2.write(pos);
      delay(delayVal);                       // waits 15ms for the servo to reach the position 
    }
  }
  old_buttonState = buttonState; // val is now old, let's store it  
}/code]

Hi, the code you have posted works ok for me, servo's travel 180 degrees then return and stop, pressing the button while the servo's are traveling has no effect for me, so i ask how you have connected the button?

I have 10k resistor pulling up pin two and a button connected to 0v and the other side connected to pin 2 so when the button is pressed it pulls pin 2 down

Code looks OK - it does assume a pull-down resistor wired to the button pin - do you have this?

I think I figured it out; I had a pretty wimpy resistor connected to it. I swapped it out to a 10K and it works as expected now :D. Thanks!

One last question, if you don't mind... would it be possible to rotate the servo from 0 to 180 and then hold it there until the button is pushed again? Thanks again for the help!

glad you got it working, yes anything is possible if you put your mind to it. If you don't need slow motion servo's do away with the for() and just set pos to 180 for one button state and 0 for another state

Simple servo toggle setup that does not require a resistor in the button setup.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}