Controlling Multiple Servos

Hi I’m new to Arduino. I have three servos that I’ve connected to PWM inputs on my Arduino Mega ADK. I modified the example servo sweep program to the one below. This program only moves 1 of the 3 servos. I was wondering what’s going wrong?

Thanks for any help/guidance!!

#include <Servo.h>

Servo x;
Servo y;
Servo z;

int pos_x = 0;
int pos_y = 0;
int pos_z = 0;

void setup()
{
x.attach(9);
y.attach(10);
z.attach(11);
}

void loop()
{

for(pos_x = 0; pos_x < 180; pos_x = 100)
{
x.write(pos_x);
delay(1);
}

{
delay(10);
}

for(pos_y = 0; pos_y < 180; pos_y = 100)
{
y.write(pos_y);
delay(1);
}

{
delay(10);
}

for(pos_z = 0; pos_z < 180; pos_z = 100)
{
z.write(pos_z);
delay(1);
}

}

I have three servos that I've connected to PWM inputs on my Arduino Mega ADK.

You don't have any PWM inputs on the Mega. Servos don't need to be connected to PWM outputs. They can be connected to any digital pin.

  {
    delay(10);
  }

What are the curly braces for?

This program only moves 1 of the 3 servos.

You don't suppose that it's important that we know which one?
How are the servos powered?

Thanks for your help Paul!

The

{
delay(10);
}

was just a copying mistake, so no need to worry there.

Only servo x moves.

The servos are just powered via the 5v from the Arduino.

These are the servos:

I also used this adapter kit:

Do you think the servos are under powered? Do I need to connect a DC adapter?

Only servo x moves.

So, comment everything having to do with Servo x. See if that makes Servo y move.

Do you think the servos are under powered? Do I need to connect a DC adapter?

Yes and yes.

for(pos_x = 0; pos_x < 180; pos_x = 100)

Let’s look at that in detail

Set pos_x to 0
While pos_x is smaller than 180
Set pos_x to 100

How many different values of pos_x will there be ?
What will cause the for loop to end ?

Paul: Commenting everything does move servo y. So I guess I need a DC adapter. The servos I’m using max out at 400mA and accept 5V, so I guess I need a 5V/400mA DC adapter?

UKHeliBob: There will only be 1 value for pos_x. In the case of:

for(pos_x = 0; pos_x < 180; pos_x = 100)

does the loop just end once pos_x=100 on the servo?

does the loop just end once pos_x=100 on the servo?

No. The loop keeps going while pos_x is less than 180 so it never stops and pos_x remains at 100.

The servos I'm using max out at 400mA and accept 5V, so I guess I need a 5V/400mA DC adapter?

For each servo, yes. Or one that can provide 1.2A to power all 3 servos.