servo question!

Hi All!

I'm just starting to play around with the Arduino, hoping to use it for robotics purposes, making a walking servo robot.

I installed the servo library file, but when I try a simple code to test it out:

#include <Servo.h>

Servo myservo;

void setup() {
myservo.attach(2) // attaches a servo connected to pin 2
}

void loop() {
myservo.write(90); // position the servo angle at 90 degrees
}

.....Anyway, when I try that i get an error message saying:

error: redefinition of 'void setup()'

Any thanks would be greatly appreciated

Gavin

Make sure you have a semi colon at the end of your line within the setup function:

myservo.attach(2);  // attaches a servo connected to pin 2

uh, thanks...forgot that...

but that still doesn't help with the error message....

any other ideas??

Thanks

Hmmm... it works for me when I add the semi colon to the code you posted. You sure that's the exact code you are trying to compile?

yes...I'll try on another computer if I can...see if that helps!

Thanks for all your help!

Im having trouble with most of the servo codes i have. they dont seem to be doing what they are sposed to..

my mini servos do not respond to any of them, and my futaba S3003 taken to their extent and the program just keeps trying to push them beyond that instead of making it go back and forth like the program says..

the code in question:

/*
 * Servo Move Simple
 * -----------------
 * Move an R/C servo back and forth (0 to 180 degrees) using
 * delayMicroseconds() for pulse and delay() for time between pulses.
 *
 * Created 18 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com>
 * http://todbot.com/
 *
 * Adapted from Daniel @
 * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1160470155/0
 * Rotates servo through 180 degrees, using "servoPulse" function  
 * adapted from "Temporary Servo Function"  by Tom Igoe and Jeff Gray
 */
 
 
int servoPin = 7;            // R/C  Servo connected to digital pin
int myAngle;                 // angle of the servo (roughly in degrees) 0-180
int pulseWidth;              // function variable
 
void servoPulse(int servoPin, int myAngle) {
  pulseWidth = (myAngle * 9) + 700;  // converts angle to microseconds
  digitalWrite(servoPin, HIGH);       // set servo high
  delayMicroseconds(pulseWidth);      // wait a very small amount 
  digitalWrite(servoPin, LOW);        // set servo low
  Serial.print("pulseWidth: "); Serial.println(pulseWidth);
  delay(20);                          // refresh cycle of typical servos (20 ms)
}

void setup() {
  pinMode(servoPin, OUTPUT);          // set servoPin pin as output
  Serial.begin(19200);
}

void loop() {
  // cycle through every angle (rotate the servo 180 slowly)
  for (myAngle=0; myAngle<=180; myAngle++) {  
    servoPulse(servoPin, myAngle);
  }
  delay(1000);
}

as soon as it is uploaded to the Arduino it moves the servo to max position (clockwise 100%) and tries to push it further. it doesn't move in the other direction at all. why is it doing this?

My mini servos do not seem to do anything when they are connected. they are E_Sky micro servos 8g

Both servo types work perfectly in the radio kit i have (Futaba R114F Rx, and Futaba Skysport ss3 Tx) without problems.

ok scrap that it seems to be working ok now :-? (the code above at least.. will test more)

It seems that they do not like runing i\under usb power and only cooperate on battery :S

It seems that they do not like runing i\under usb power and only cooperate on battery :S

are your servos powered from the +9v pin or +5v pin on your arduino? the 9v pin only carries power when you're using an external power-source (not USB).

It seems that they do not like runing i\under usb power and only cooperate on battery :S

are your servos powered from the +9v pin or +5v pin on your arduino? the 9v pin only carries power when you're using an external power-source (not USB).

The 5V pin naturally ( i don't see a 9V pin, what one is that?) :slight_smile:

The 9V pin is the 2nd (from left to right) orange colored pin at the bottom of the image below.

If I am correct, servo's are normally powered by 4-6V, so you might destroy it at 9V.

If your servo's don't work, then they are probably drawing too much current, at least more than Arduino can provide. In this case, use an external power supply to power the servo's and just use the Arduino for the signal.

Good luck!

The 9V pin is the 2nd (from left to right) orange colored pin at the bottom of the image below.

that 9v pin is a Vin on my board this is the power bit of it (Reset, 3v3, +5V Gnd, Gnd, Vin)

so the usb isnt enough to drive it? also im getting the rx and tx lights on low wen its on battery power, they just sit there slightly on (in all sketches) but they remain off on USB power (unles being used)

that 9v pin is a Vin on my board this is the power bit of it (Reset, 3v3, +5V Gnd, Gnd, Vin)

Huh???? My board has the same layout (without checking) as the board shown in that image I posted. Do you have a NG or Diecemila, they might differ...

that 9v pin is a Vin on my board this is the power bit of it (Reset, 3v3, +5V Gnd, Gnd, Vin)

Huh???? My board has the same layout (without checking) as the board shown in that image I posted. Do you have a NG or Diecemila, they might differ...

Mine is this one: http://mcukits.com/?p=9 Diecemila compatable..

that 9v pin is a Vin on my board this is the power bit of it (Reset, 3v3, +5V Gnd, Gnd, Vin)

Huh???? My board has the same layout (without checking) as the board shown in that image I posted. Do you have a NG or Diecemila, they might differ...

Mine is this one: http://mcukits.com/?p=9 Diecemila compatable..

The pin marked Vin (next to the Gnd pin) is the 9 volt input pin. But as pointed out above, this is too much for most servos.

The pin marked Vin (next to the Gnd pin) is the 9 volt input pin. But as pointed out above, this is too much for most servos.

yea it would be a bit too much.. i need to sort my joystick pots out with new wires so i can make a x and y control axis need to make a trip to the components shop too for some resistors and such

One of the Deicemila changes was to add two additional pins to the "power" connector; 3.3V and RESET, to the LEFT of existing power pins. So a shield that is "only" NG-aware still works attached to a diecemila, it just doesn't have connections to the two new power pins...

Hi
I am trying to drive 8 servos, unfortunately I cant get the code to work. Does anyone see the mistake? ( Only tested it with 2 servos so far)

#include <ServoTimer2.h>    // the servo library



ServoTimer2 servo1;            
ServoTimer2 servo2; 

////////////////////Read RC
int servoPin1 = 5;          //PD5
int servoPin2 = 6;        //PD6
int servoPin3 = 7;      //PD7
int servoPin4 = 11;    //PB3
int servoPin5 = 12;    //PB4
int servoPin6 = 13;    //PB5
int servoPin7 = 14;    //PC0 ???
int servoPin8 = 21;    //PC6  ???
           

int pulseWidth1 = 900;
int pulseWidth2 = 2500;

void setup() {
  
  servo1.attach(servoPin1);     // attach a pin to the servos 
  servo2.attach(servoPin2); 
  
  
}
////////////////////////////////////////////////////////////

void loop()
{ 

  if(pulseWidth1<2500){
        servo1.write(pulseWidth1++);
        servo2.write(pulseWidth2--);
  }
  else{
    servo1.write(pulseWidth1--);
    servo2.write(pulseWidth2++);
  }
  
        
        
 
   
    delay(2000);
}

In that sketch, the pulse values will stop changing when servo1 reaches 2500 (it will then toggle between 2499 and 2500) try this

int direction = 1;

void loop()
{
  if(pulseWidth1 >2500){
       direction = -1;
  }
  else if pulseWidth1 < 900{
       direction = 1;
  }
  servo1.write(pulseWidth1 =  pulseWidth1 + direction);
  servo2.write(pulseWidth2 = pulseWidth2 - direction );
}

also, I think servoPin8 should be pin 19, not 21