Controlling 30 servos..

Hello!

I'm new here ...
I have a small project in which I need control up to 30 servos using
an application, through the serial port (USB).

I have a Mega Arduino, I know that you can control up to 48 servos, but do not know
how to make an optimal code for that .. can control up to 6 servos (by PWM) without
problem using the instruction "case" to get the handle of the servo
and then the value of the position servo ..
But he liked to take it more simply, get a string with 30
identifiers over the respective values ??of position ..

Any ideas'?

Thanks,
Rafael.

can control up to 6 servos (by PWM) without problem

No, you can't. Servos are not controlled using PWM. They are controlled using PPM.

I have a Mega Arduino, I know that you can control up to 48 servos, but do not know
how to make an optimal code for that

Optimal code depends on what you want to do. Arrays will almost certainly come into play.

But he liked to take it more simply, get a string with 30
identifiers over the respective values ??of position

He who? If you want speed, you almost certainly don't want a string of anything. An array of bytes, now, that's a different story.

What kind of servos are you using? The standard 0 to 180 type? If so, you can send a start of data marker (a byte whose value is greater than 180, and less than 255), the 30 serial positions, and and end of data marker (a byte whose value is greater than 180, and less than 255, and different from the start marker).

On the Arduino, read the data from the serial port, discarding data until the start marker arrives. Keep reading, storing and counting the values until the end marker arrives. If you received 30 values, use them. If not, the packet was corrupt, and should be discarded.

Hi PaulS,

I'm using standards servos, 0 to 180. I just need to control 30 servos, by USB. The speed is not to important...

I have this code working with 6 servos, "a" to "f" is the identifier servo and "0" to "180" is the position value.
But I don't know how to "scale" this to up 30 servos..

#include <Servo.h>
#define SERVO_2_PIN 2
#define SERVO_3_PIN 3
#define SERVO_4_PIN 4
#define SERVO_5_PIN 5
#define SERVO_6_PIN 6
#define SERVO_7_PIN 7

Servo g_servo2,g_servo3,g_servo4,g_servo5,g_servo6,g_servo7;

void setup()
{
pinMode(SERVO_2_PIN, OUTPUT);
pinMode(SERVO_3_PIN, OUTPUT);
pinMode(SERVO_4_PIN, OUTPUT);
pinMode(SERVO_5_PIN, OUTPUT);
pinMode(SERVO_6_PIN, OUTPUT);
pinMode(SERVO_7_PIN, OUTPUT);
g_servo2.attach(SERVO_2_PIN);
g_servo3.attach(SERVO_3_PIN);
g_servo4.attach(SERVO_4_PIN);
g_servo5.attach(SERVO_5_PIN);
g_servo6.attach(SERVO_6_PIN);
g_servo7.attach(SERVO_7_PIN);
Serial.begin(9600);
}

void loop()
{
static int val = 0;

if (Serial.available())
{
char ch = Serial.read();

switch(ch)
{
case '0'...'9':
val = val * 10 + ch - '0';
break;
case 'a':
g_servo2.write(val);
val = 0;
break;
case 'b':
g_servo3.write(val);
val = 0;
break;
case 'c':
g_servo4.write(val);
val = 0;
break;
case 'd':
g_servo5.write(val);
val = 0;
break;
case 'e':
g_servo6.write(val);
val = 0;
break;
case 'f':
g_servo7.write(val);

val = 0;
break;
}
}
//Servo::refresh(); // Should try to call this every 20 ms to insure servo stays set
}

Thank you!
Rafael.

If you are going to purchase 30 servos, you might want to consider getting a seperate servo controller to be controlled by your arduino. An ssc-32 ($40) can control 32 servos.

I am using the Mega to control 18 servos. Here is some of the code:

  const int LEG_COUNT = 6;
  const int servos_per_leg = 3;

  enum leg_id { R1, R2, R3, L3, L2, L1 };

  // Servo pins.
  const int servo_pins[][servos_per_leg] = {{42, 43, 44},  // R1
										    {34, 35, 37},  // R2
										    {26, 27, 28},  // R3
										    {22, 23, 24},  // L3
										    {30, 32, 33},  // L2
										    {38, 39, 40}}; // L1

      // create servo objects to control servos.
      for (int i = 0; i < (LEG_COUNT); i++)
      {
        Servo sx;       // New servo object.
        servos[i].coxa = sx; // Add to array.

        Servo sy;       // New servo object.
        servos[i].femur = sy; // Add to array.

        Servo sz;       // New servo object.
        servos[i].tibia = sz; // Add to array.

        servos[i].coxa.attach(servo_pins[i][x]); // Attach servo to pin.
        servos[i].femur.attach(servo_pins[i][y]); // Attach servo to pin.
        servos[i].tibia.attach(servo_pins[i][z]); // Attach servo to pin.
      }

I know this is an old post but, just in case dbiltcliffe observes this reply, I must ask:

Would you post the rest of the code your sample came from? I would love to see it.

Thanx

I bought the Lynxmotion SSC-32 board. How should I connect the SSC-32 board to the Arduino board? Should I use Arduino Mega board instead of Arduino Uno board?

At 136 posts, you should know that taking a question onto an unrelated post is not a good idea. You should also know that posting questions about hardware without posting a link to the hardware wastes everybody's time.

jeffmorris:
I bought the Lynxmotion SSC-32 board. How should I connect the SSC-32 board to the Arduino board? Should I use Arduino Mega board instead of Arduino Uno board?

I'd go ahead and get the mega as the price difference between the mega and uno is now only a couple of $. You connect the arduino tx and rx to the ssc-32 TTL rx and tx, and connect the grounds between the two boards. make sure both are set for the same baud rate.