really need help with 4 servos with the arduino

I having a very hard time running 4 servos with the arduino. I can only get 2 to work.

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;  // create servo object to control a servo
Servo myservo4;

int potpin1 = 0;  // analog pin used to connect the potentiometer
int potpin2 = 1;
int potpin3 = 2;  // analog pin used to connect the potentiometer
int potpin4 = 3;
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);  // attaches the servo on pin 10 to the servo object
  
    myservo3.attach(5);  // attaches the servo on pin 5 to the servo object
  myservo4.attach(6);  // attaches the servo on pin 6 to the servo object
}

void loop()
{
  val = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180)
  myservo1.write(val);                  // sets the servo position according to the scaled value
  val = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo2.write(val);
  
  val = analogRead(potpin3);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo3.write(val);
  
  val = analogRead(potpin4);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180)
  myservo4.write(val);                  // sets the servo position according to the scaled value
                    // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

Thats the code i em using, the turbble is that pin 5 and 6 are not sending pwm signals or any signal for that mater (aslo used LED light to test this) every thing else works. 9 and 10 are running 2 of the servos just fine. all 4 inputs are tested and good. Please help me i have been stuck on this for some time now.

I think that you can only run 2 servos using the standard Servo library. You should look into this:

http://www.arduino.cc/playground/Code/MegaServo

I have downloaded this link and it gave me this code

#include <MegaServo.h>

// test sketch for MegaServo library
// this will sweep all servos back and forth once, then position according to voltage on potPin


#define FIRST_SERVO_PIN  22  

MegaServo Servos[MAX_SERVOS] ; // max servos is 32 for mega, 8 for other boards

int pos = 0;      // variable to store the servo position 
int potPin = 0;   // connect a pot to this pin.

void setup()
{
  for( int i =0; i < MAX_SERVOS; i++)
    Servos[i].attach( FIRST_SERVO_PIN +i, 800, 2200);

  sweep(0,180,2); // sweep once    
}

void sweep(int min, int max, int step)
{
  for(pos = min; pos < max; pos += step)  // goes from 0 degrees to 180 degrees    
  {                                  // in steps of 1 degree 
    for( int i =0; i < MAX_SERVOS; i++){ 
      Servos[i].write( pos);     // tell servo to go to position  
    }
    delay(15);                  // waits 15ms for the servo to move 
  } 
  for(pos = max; pos>=min; pos-=step)     // goes from 180 degrees to 0 degrees 
  {                                
    for( int i =0; i < MAX_SERVOS; i++){ 
      Servos[i].write( pos);     // tell servo to go to position  
    }
    delay(15);                  // waits 15ms for the servo to move 
  }   
}

void loop()
{ 
  pos = analogRead(potPin);   // read a value from 0 to 1023
  for( int i =0; i < MAX_SERVOS; i++) 
    Servos[i].write( map(pos, 0,1023,0,180));   
  delay(15);   
}

I have 4 analog inputs (potentiometer) gong in (0,1,2,3,) and 4 servo. I really don’t understand this code it says i can run up to 12 servos with it. I uploaded it to the board as is and go one of the servos to turn one way, so i know im doing something wrong. Im new at this and have a very hard time with code. I still don’t under stand why i cant controll more then 2 servos with the pwm pins. I have make lots of led lights fade in and out.

I cam move my wires around to fit any code that being said dose any one have code that will make this work?

Dose any one have an ez way to help me out with this. Thank you

Just out of curisoity… did you mean to intentionaly reverse the range of numbers for myservo1 (pin 9) and myservo4 (pin 6) using the map func? What happens if you set all the servo’s to the same range’s by saying

 val = map(val, 0, 1023, 0, 179);

instead of:

 val = map(val, 0, 1023, 179, 0);

So your code would look like:

 val = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo1.write(val);                  // sets the servo position according to the scaled value
  val = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo2.write(val);

  val = analogRead(potpin3);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo3.write(val);

  val = analogRead(potpin4);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo4.write(val);                  // sets the servo position according to the scaled value

Maybe that may help? Dunno but may yield diff results at least.

And I dont know if it matters but if indeed you do want the values of myservo1 and myservo4 reversed you may try doing this and seeing if it yields different results:

val = map(val, 0, 1023, 0, 179);
val = map(val, 0, 179, 179, 0);

This just breaks up the math and maybe helps you find an error somewhere…

the numbers have been moved around to get the servos facing the right way. (4in) when i would turn the knob left the servo would go right. so switching the numbers around made the servo turn with the knob.

Ok man, I was a little pissed off at the Mega Servo library at first, but later on I figured out that it was just like the normal library. The library says that it can control 12 servos on one pin (if I understood it correctly, this is what I have been thinking for a while now, so please correct me if I am wrong). Since you dont need that many servos, you can simple do a little switchover from normal library to mega library to allow servos on all pins (a little confusing, but here is some code to help)

untested, but if memory serves me right, this will work and should get you started… the servos are connected to pins 8, 9, 10, 11… potentiometers are Analog 0 - 3

#include <MegaServo.h> 
 
MegaServo servo1; 
MegaServo servo2; 
MegaServo servo3; 
MegaServo servo4; 



 
void setup() 
{ 

  servo1.attach(8); 
    servo1.attach(9); 
      servo1.attach(10); 
        servo1.attach(11); 

} 
 
 
void loop() 
{
  
int A1 = analogRead(0);
int A2 = analogRead(1);
int A3 = analogRead(2);
int A4 = analogRead(3);

int val1 = map(A1, 0, 1023, 0, 180);
int val2 = map(A2, 0, 1023, 0, 180);
int val3 = map(A3, 0, 1023, 0, 180);
int val4 = map(A4, 0, 1023, 0, 180);

servo1.write(val1);
servo2.write(val2);
servo3.write(val3);
servo4.write(val4);
}

ok i have it set up just how you said to and i got the code uploaded but only 1 servo is working out of 4, theres still something wrong with the code.

Although it quite well might be code, it might just be an issue of lack of current. How are the servos powered?

#include <MegaServo.h>

MegaServo servo1;
MegaServo servo2;
MegaServo servo3;
MegaServo servo4;




void setup()
{

  servo1.attach(3);
    servo2.attach(9);
      servo3.attach(10);
        servo4.attach(11);

}


void loop()
{
  
int A1 = analogRead(0);
int A2 = analogRead(1);
int A3 = analogRead(2);
int A4 = analogRead(5);

int val2 = map(A1, 0, 1023, 180, 0);
int val3 = map(A2, 0, 1023, 180, 0);
int val4 = map(A3, 0, 1023, 0, 180);
int val5 = map(A4, 0, 1023, 0, 180);

servo1.write(val2);
servo2.write(val3);
servo3.write(val4);
servo4.write(val5);
}

had to mod the code a bit but now all of them are working but there is a glitch. the analog in for 2 is running 2 servos at once. is like its bleeding over so how. my wires are flawless so i really dont know whats going to, i dont see the problem in the code (but then again i mite just not be seeing it) other idea is the arduino mite have some kind of problem. I just cant fig out how the one input is getting split to run 2 different pins going to the servos. Grrrrr :-[im so so close to having this right!!! when i have this last thing all done i will take some pics and post them so you can see what im doing. and i want to thank you for getting me this far.

The library says that it can control 12 servos on one pin

It does?
Where?

I don’t like using a bloated library to control servos. The best way to control servos is directly with:

#define ANGLE_MIN -90
#define ANGLE_MAX 90
#define PWM_MIN 130
#define PWM_MAX 235


void rotate(int angle, int servo)
{
  analogWrite(servo,map(angle,ANGLE_MIN,ANGLE_MAX,PWM_MIN,PWM_MAX));
}

The servo must be connected to a digital PWM pin in your arduino. Angle and PWM values depends on your servo. To find the PWM value just try a high or lower value if the default does not work well.

Check your arduino for the PWM digital pins, there are almost 4 of them or more = 4 servos at once without troubles ;D

Beware: see http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1249178408/10#10

Well,

Quote:
The library says that it can control 12 servos on one pin

It does?
Where?

“This library allows an Arduino board to control one to twelve RC (hobby) servo motors on any digital pin on a standard Arduino board”

This is the first line in the MegaServo playground page. Perhaps I am misreading, but I think it is saying “12 servos per pin”, not “12 servos total, and each can be placed on whatever pin you want”. Like i said, correct me if I’m wrong.

This library allows an Arduino board to control one to twelve RC (hobby) servo motors on any digital pin on a standard Arduino board

Means that unlike the standard arduino library that can only use pins 9 and 10, the MegaServo library allows the selection of any pin for a servo. I had assumed it was obvious that each servo needs its own pin, as illustrated in the example sketch.

that’s too bad, my bad for the mis-information afap. And there I was being all happy that the arduino can run 12 servos per pin.

afap, the code in post #2 attaches servos on consecutive pins starting from pin 22. Unless you have a mega board this will not work. If you change the define FIRST_SERVO_PIN from 22 to 2, itw ill attach 12 servos starging from pin 2 to pin 14 (analog pin 0).

The code in post #8 is missing the delay at the end of loop. A delay of 15 milliseconds (as you have in your code in post #2) puts much less stress on the system. Without the delay the code is rewriting each channels value thousands of times a second (and turning off and on interrupts for each write) . Because the channel data is only updated every 20 milliseconds the system is less stressed without handling all the unnecessary calls to Servo.write()

Try this:

#include <MegaServo.h>

MegaServo servo1;
MegaServo servo2;
MegaServo servo3;
MegaServo servo4;

void setup()
{
  servo1.attach(3);
  servo2.attach(9);
  servo3.attach(10);
  servo4.attach(11);
}

void loop()
{
  int A1 = analogRead(0);
  int A2 = analogRead(1);
  int A3 = analogRead(2);
  int A4 = analogRead(5);

  int val2 = map(A1, 0, 1023, 180, 0);
  int val3 = map(A2, 0, 1023, 180, 0);
  int val4 = map(A3, 0, 1023, 0, 180);
  int val5 = map(A4, 0, 1023, 0, 180);

  servo1.write(val2);
  servo2.write(val3);
  servo3.write(val4);
  servo4.write(val5);
  delay(15);  // delay 15 milliseconds
}

I don’t like using a bloated library to control servos. The best way to control servos is directly with:

Code:#define ANGLE_MIN -90
#define ANGLE_MAX 90
#define PWM_MIN 130
#define PWM_MAX 235

void rotate(int angle, int servo)
{
analogWrite(servo,map(angle,ANGLE_MIN,ANGLE_MAX,PWM_MIN,PWM_MAX));
}

The servo must be connected to a digital PWM pin in your arduino. Angle and PWM values depends on your servo. To find the PWM value just try a high or lower value if the default does not work well.

Check your arduino for the PWM digital pins, there are almost 4 of them or more = 4 servos at once without troubles

Just in case you missed AWOL’s warning about this suggestion - please do not do this using a standard hobby servo. Using analogWrite to drive a servo can potentially destroy it.

that’s too bad, my bad for the mis-information afap. And there I was being all happy that the arduino can run 12 servos per pin.

Not sure if your comment was completely serious but I have reworded the playground page, is the following clearer:

This library allows an Arduino board to control one to twelve RC (hobby) servo motors on a standard Arduino board or up to 48 servos on an Arduino Mega. Each servo can be attached to any unused digital pin.

It was serious sarcasm, but it’s clearer now. And good catch on the missing delays as well :wink:

Thank you for taking a look at this every one… here is the code i have uploaded edited by mem

#include <MegaServo.h>

MegaServo servo1;
MegaServo servo2;
MegaServo servo3;
MegaServo servo4;

void setup()
{
  servo1.attach(3);
  servo2.attach(9);
  servo3.attach(10);
  servo4.attach(11);
}

void loop()
{
  int A1 = analogRead(0);
  int A2 = analogRead(1);
  int A3 = analogRead(2);
  int A4 = analogRead(5);

  int val2 = map(A1, 0, 1023, 180, 0);
  int val3 = map(A2, 0, 1023, 180, 0);
  int val4 = map(A3, 0, 1023, 0, 180);
  int val5 = map(A4, 0, 1023, 0, 180);

  servo1.write(val2);
  servo2.write(val3);
  servo3.write(val4);
  servo4.write(val5);
  delay(15);  // delay 15 milliseconds
}

unfortunately I still have the same problems. One of the analogs are moving 2 servos. the servo shakes real bad because of getting 2 inputs. with i pull one of the analog inputs out, then the shaking stops but but still 2 servos turn off from the other analog in, just more smooth. and before you say it NONE of the signal wires are even close to touching. its got to be the code. i have moved around and tryed other pins and it always turns out the same problem. Now also please keep in mind that all other servos run fine.

again i really want to thank all of you for taking the time to help me with this, This is not just a project, its to have pan and tilt on my cams for my house. I have a wife and baby and not to long ago to men broke in to some ones house just down the street more me. anyway the tided up the people how lived there and took there time about 30 to 45 mins robing the place. and they got away with it. So im hope have some big cams on each side of my house with servos making them look even more crazy will word off these people. :-[ The cams go to a PC DVR.