I built a small Hexapod that runs on 12 9g micro servos, all of that is running on an Arduino MEGA.
The code is using MegaServo library so that I can run them smoothly. I tested out all the servos individually on a prototype shield. It all worked fine. But as soon as I tried to get them to work at the same time they all started doing their own thing independent of the code. Note that I connected the server to a separate power supply that is connected to a arduino troulgh a common GND.
Here's the code
#include <MegaServo.h>
#define NBR_SERVOS 12 // the number of servos, up to 48 for Mega, 12 for other boards
#define FIRST_SERVO_PIN 2
MegaServo Servos[NBR_SERVOS] ; // max servos is 48 for mega, 12 for other boards
//B1=1, B2=2 ,B3=3 ,B4=4 ,B5=5 ,B6=6 ,A1=7 ,A2=8 ,A3=9 ,A4=10 ,A5=11 ,A6=12
int pos = 0; // variable to store the servo position
int i;
int po[12]={20,20,20,20,20,20,90,90,90,90,90,90};
int f=0;
void Fow(){
for(i=0;i<3;i++)
Servos[i].write(po[i]+f); //1-3B up
for(i=7;i<10;i++)
Servos[i].write(po[i]+f); //1-3A Fov
for(i=10;i<12;i++)
Servos[i].write(po[i]-f); //4-6A back
for(i=0;i<3;i++)
Servos[i].write(po[i]-f); //1-3B down
for(i=4;i<7;i++)
Servos[i].write(po[i]+f); //4-6B up
for(i=7;i<10;i++)
Servos[i].write(po[i]+f); //4-6A Fov
for(i=10;i<12;i++)
Servos[i].write(po[i]-f); //1-3A back
if(f==20)
f=0;
else
f++;
}
void setup(){
for( i =0; i < NBR_SERVOS; i++)
Servos[i].attach( FIRST_SERVO_PIN +i, 500, 2400);
for( i =0; i < NBR_SERVOS; i++)
Servos[i].write(po[i]);
}
void loop(){
for(i=0;i<6;i++)
Servos[i].write(po[i]+80); //stnad
}
Note: void Fow() is not used right now
Thank you for your time