Control 12 Servos using Arduino Mega

Hi!

I'm a newbie and am currently doing my first ever Hexapod ROBOT with 18servo motors controlled using Arduino Mega ADK. I already control 6 servos using 4 pins from arduino (pins 8,9,10,11). However, when i tried controlling the other 6 servos with 4 other pins, something went wrong. it doesnt word at all. It's seems it's my codes fault. Here's my code:

 

 

 

#include <Servo.h>

 

Servo servoa;             // Define a servo

Servo servob;       

Servo servoc;

Servo servod;

Servo servoe;

Servo servof;

Servo servog;

Servo servoh;

int i,j,k;

int a=90;

int b=90;

int c=90;

int d=90;

int e=90;

int f=90;

int g=90;

int h=90;

 

void setup() { 

  servoa.attach(10);      // RightA(4,6)

  servob.attach(9);       // LeftA(2)

  servoc.attach(8);       // RightB(4,6)

  servod.attach(7);       // LeftB(2)

  servoe.attach(6);       // LeftA(1,3)

  servof.attach(5);       // RightA(5)

  servog.attach(4);       // LeftB(1,3)

  servoh.attach(3);       // RightB(5)

  servoa.write(a);

  servob.write(b);

  servoc.write(c);

  servod.write(d);

  servoe.write(e);

  servof.write(f);

  servog.write(g);

  servoh.write(h);

 

  } 

 

void loop()  {           // Loop through motion tests

delay(5000);

for(i=1;i<=30;i++){

a++;

b--;

c++;

d--;

servoa.write(a); 

servob.write(b); 

servoc.write(c); 

servod.write(d);

delay(50);

}

for(i=1;i<=30;i++){

c--;

d++;

servoc.write(c); 

servod.write(d); 

delay(25);

}

for(i=1;i<=30;i++){

a--;

b++;

e++;

f--;

g++;

h--;

servoa.write(a); 

servob.write(b); 

servoe.write(e); 

servof.write(f);

servog.write(g); 

servoh.write(h);

delay(75);

}

for(i=1;i<=30;i++){

g--;

h++;

servog.write(g); 

servoh.write(h);

delay(25);

}

for(i=1;i<=30;i++){

e--;

f++;

servoe.write(e); 

servof.write(f);

delay(25);

}

}

 

 

Can somebody please tell what's wrong with this? Any help would be much appreciated. Thank you!

Some code for you to try

Hi Weisheil, I managed to get 15 servos working on my Arduino Mega ADK and thought I’d share a snippet of the code with you here to see the approach I took.

#include <Servo.h>

#define number_of_servos 15
int servoPins[] = {23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 48};
Servo servos[number_of_servos];
int servoPosMax[] = {120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120};
int servoPosMin[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int servoPosSafeStart[] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};

void initServos() {
  int i;
  for (i=0; i<number_of_servos; i++)
  {
    servos[i].attach(servoPins[i]); // servo setup
    servos[i].write(servoPosSafeStart[i]);
    Serial.print(i, DEC);
    Serial.print(", “);
  }
  Serial.println(” ");
}

void setServo(int num, int deg) {
  deg = constrain(deg, servoPosMin[num], servoPosMax[num]);
  servos[num].write(deg);
}

void setup() {
  initServos();
}
void loop() {
  // your code here, sample should move all through 180deg and back
  int j;
  int i;
  int pos;
  for (j=0; j<360; j += 20) {
    if (j <= 180)
      pos = j;
    else
      pos = 360 - j;
    
    for (i=0; i<number_of_servos; i++) {
      setServo(i,pos);
      delay(50);
    }
    
  }
}

This compiles in Arduino v1.0.1 but was not tested on an Arduino (the functions work fine but I haven’t tested the sample code in the loop).

Hope this is of some use to you.

Simulator and PC fault

Hi Sir Oddbot,

Your work really help a lot with the problems other than this. After checking Arduino 0022, it made me realize that Arduino 1.0.1 is way updated than 0022 and it’s not its fault for not recognizing the program properly. I tried loading the program direcly to the Arduino board but it doesnt work. Then I tried again in Arduino Simulator using other PC and failed. Load it directly to board and presto it works just fine. 

Now, I’m working on using 12 servos controlled by 8 pins. I’m using a powersupply with 5V (45A max). Setup works fine when 10servos are connected but more than that, the robot malfunctions or the desired position is not reached instead it goes directly to its minimum or maximum position. Also, when I connect the one pin to a servo, the setup’s fine only until 7 pair, with 8 pins to 8 servo, it malfunctions. Btw, I am using SG5010 servo motor.

Controlling

Hi markcra,

I’ll try it. I believe this involves PC to board interface?

Serial.print(i, DEC);

    Serial.print(”, “);
  }
  Serial.println(” ");"

But, I still dont know how to control my robot directly from PC. Do I have to use another program for this?

Thanks!

Oops

The Serial calls were throw-backs to some debugging I was doing. Without setting the baudrate for the Serial communications those calls won’t do anything.

My code is supposed to be stand-alone and just moves all the servos, control would be implemented in the loop() function by checking some inputs or communications.

I’ve not tried computer control but may take a look at it for one of my projects. I’ll post back here when I get a chance.

Thanks!

I somehow get the gist of it. I removed “serial” related codes and it verifies all right. I’ll gonna work on my programming later and will try to implement it in an array. Right now, I’m having trouble with the supply. Thanks for the info! Hope to get some more later :smiley: