simple program for controlling two servos at once

Good evening, thanks for taking the time to have a look at my issue! I've put together what I think is a simple program to control two micro servos which control two cat ears for a toy project. I'm using a 5v arduino pro mini, a button attached to pin 2 with a 5k pull-down resistor.
I'm powering it with a 9v battery. These servos are tiny but I seem to be having trouble with them. I'm pretty sure the board and power supply has enough juice to get what i want because i tried this test program: http://www.robotoid.com/appnotes/arduino-operating-two-servos.html and they move ok, it's just when i try and bring a button into the mix that i have problems.

What I'd like to happen is the following:

Button is pressed

light comes on

Power is given to servos

one servo rotates clockwise(to 180), the other anti-clockwise( to 0)

They stay there for a moment

They are both returned to 90 (central position)

power is removed.

light goes off

What actually happens is when I press the button the light comes on, one servo moves around, then they go off. The second time I press the button the light comes on but doesn't turn off, it just hangs with the servos powered (buzzing).

I'm trying to work out if it's my code or something to do with the servos i'm using.

Many thanks - see the code below.

/*
Cat ears
 
 Turns on and off two servos connected to digital button

  */
#include <Servo.h>

Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo
// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

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

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH); 
   servoLeft.attach(10);  // Set left servo to digital pin 10
      servoRight.attach(9);  // Set right servo to digital pin 9
  earsback();             // put ears back
  delay(2000);           // Wait 2000 milliseconds (2 seconds)
  reset();    //put ears forwards
  delay(2000);    //delay to let it get there
  servoLeft.detach();    //detach servo
  servoRight.detach();   //detach servo 
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

// Motion routines for putting ears back and reset
void earsback() {
  servoLeft.write(0);
  servoRight.write(180);
}

void reset() {
  servoLeft.write(90);
  servoRight.write(90);
}

I'd move this

   servoLeft.attach(10);  // Set left servo to digital pin 10
      servoRight.attach(9);  // Set right servo to digital pin 9

to the setup function and I would not detach.

The reason is that you are unlikely to want to change any of the setup while it is running and you only need to initialise one. Sometimes this is an implicit assumption in setup code in libraries, not sure if this is the case here.

You should probably also call your reset() function from the setup so the servos start earsback() from a known position.

Probably no need to use the servo detach as that only stopps the control signal to the servo and not the power supply to the servo motor and control board. You will need larger batterys to power the servos for any reasonable time. Below is the typical servo power setup.

Thank you for the helpful comments, i'll try some of these alternatives when I get home. The reason i'm attaching and detaching the servos is because they buzz and creep when attached, and i'd like to leave the program running from a battery from a while and i don't want there to sit there "creeping" and eating power. I need to power both the arduino and the servos form the same battery because of space restrictions but I can try an alternative circuit.

Many thanks!

As long as there is only a trivial load on the servos, detaching shouldn't cause them to move any. Just understand that without a signal, the servo will not try to hold it's position, and it will take little effort to move them from the last position they were set to.

I'm also doubtful that detaching them will have any significant impact on power consumption, but it shouldn't hurt it any.

Thanks for the advice, perhaps something else is wrong then as the servos do buzz and kind of wander around when they are attached but no signal is being given by the program. Allowing them to move manually while detached is not really a problem for the application.

Thanks for the advice, perhaps something else is wrong then as the servos do buzz and kind of wander around when they are attached but no signal is being given by the program.

If you are using the Servo library, there is a signal being sent to the servo every 20 milliseconds, or less, unless your application is so overwhelming the interrupt system that that can't happen.

Have you seen and followed the advice in the two links in my signature ?

Duane B

perhaps something else is wrong then as the servos do buzz and kind of wander around when they are attached but no signal is being given by the program.

The buzz and wandering is not normal for the servos. Below is some simple servo test code you can use with each servo to see if the issue is fixed or continues.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

Thanks for everyone's suggestions! It was a power supply issue all along, as suggested. It looks like the arduino can only control one servo at a time, so i can control each ear independently by attaching and detaching each one in turn. I'd love to set up two power supplies, but i only have a space a little smaller than a 9V battery to power both the servos and the arduino.

I understand that i need to provide 9v for the arduino pro mini (5v) and 5v to each servo, so i can't hook them up indirectly to the same battery pack.

So i think i'm not going to be able to power both in the space available to move both ears at once, unless anyone can recommend some really tiny servos (there is virtually no load on these servos) or a battery option that will fit inside the project.

I understand that i need to provide 9v for the arduino pro mini

Why not provide it with something closer to 5V, then you won't waste 4v@ xmA in heat?