Pages: [1]   Go Down
Author Topic: simple program for controlling two servos at once  (Read 1336 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Code:
/*
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);
}
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1268
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd move this

Code:
   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.
« Last Edit: July 22, 2012, 08:49:46 pm by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

London
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


London
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 610
Posts: 49016
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Duane B
Logged


0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

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

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

London
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26193
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: