One button/2 Servo control

I'm trying to make a single button activate 2 servos (moving each of them from position "0" to "90" and then staying). Then when the button is pushed, moving them from "90" to "0" and rinsing and repeating for each button press.

I have my project up and running as follows: Pull down resistor on the button, software debounce function, 2 tower hobby servo motors (not continuous)

Here is my working (somewhat) code:

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
Servo myservo2;  // create 2nd servo object to control a 2nd servo 

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to

// Variables will change:
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonPushCounter = 0;   // counter for the number of button presses
boolean currentButton = LOW;

void setup() {
  // attach the Servo to pins
   myservo.attach(9);
   myservo2.attach(10);
  // tell servo to go to position in variable 'pos'
  myservo.write(0);
  myservo2.write(0);
  
  delay(15); 
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(buttonPin);
  }
  return current;
}


void loop() {
  // read the pushbutton input pin:
  buttonState = debounce(buttonPin);
  
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
    } 
    else {
    }

    // save the current state as the last state, 
    //for next time through the loop
    lastButtonState = buttonState;
  }
  
  // moves the servo every other button push by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 2 == 0) {
  //move to position 90  
   myservo.write(90); 
   myservo2.write(90); 
   delay(15); 
  } else {
  //OR: move back to position 0  
   myservo.write(0); 
   myservo2.write(0);
   delay(15); 
  } 
}

Everything works as I had hoped except one servo keeps making a little noise after each move. It just vibrates quietly for a second or two and then usually stops.

Anyone have any clue why this is happening based on my code?

Also, does anyone see anything that can be cleaned up in the code or made better? Thank you,
I appreciate all the help I can get as I am completely new to both Arduino and coding.

Thanks,
Chris

Some servo button toggle code that doesn't require a resistor. As to your issue, if you are trying to power the servos from the arduino, that can be a problem. Poor servo grounding can also cause issues.

//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
}

Awesome and thank you I'll look into the ground and give your code a try!

Do you have any recommendations on power for this project? Since I have 2 servos, should I use 4 AA batteries for the arduino and a couple of D batters or 9 volts for the servos?

What would work best and still be somewhat small?

Thanks again,
Chris

Is there a way where we can add this function to this cuurent code?

#include <Servo.h>

Servo myservo;
Servo myservo2;
Servo myservo3;

const int Button1 = 2;   // the number of the pushbutton pin
const int Button2 = 3;   // the number of the pushbutton pin
int pos = 15;

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonPushCounter = 0;   // counter for the number of button presses
boolean currentButton = LOW;

// variables will change:
bool buttonState = 0;         // variable for reading the pushbutton status
bool buttonState2 = 0;

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);

  
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);

  myservo.write(15);
  myservo2.write(15);
  delay(500);

  //myservo3.write(0);
}


boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(buttonPin);
  }
  return current;
}



void loop() { 
  buttonState = digitalRead(Button1);

  if (buttonState == HIGH) {
  	
    pos = 170;
    delay(500);
  } else {
  	pos = 15;
  	delay(500);
  }



  myservo.write(pos);
  myservo2.write(pos);
Serial.println(pos);
}