control servos by an array; how to write the sw for controlling the lib

Hi,

I want to use the arduino with several servos. I read, that the servo lib can handle 8 (or 12?) servos at a time.
My question: how can I program this?

Example:
servo_position[8]; // here I want to store actual values(positions) of each individual servo
servo_pin[8]; //this is the pin the individual servo is connected to

What I usually find is:

SoftwareServo myservo
myservo.attach(2); //pin 2

but how to make it “variable” to enable servo-updates in a way like this:

for(i=0; i< max_servo_count; i++){
myservo.write(servoposition*); // how to make this right?!?*
}
Thanks a lot,
Barny

What I usually find is:

SoftwareServo myservo
myservo.attach(2); //pin 2

Why are you using SoftwareServo, instead of Servo?

You can create an array of Servo objects:

Servo myServos[8]; //Now we have 8 servos

Then attach them to consecutive pins:

for(int i=0; i<8; i++)
{
   myServos[i].attach(i+2);
}

Then, move all 8 servos at once:

for(int pos = 0; pos<180; pos++)
{
   for(int s=0; s<8; s++)
   {
      myServos[s].write(pos);
   }
}
1 Like