Make 3 servos move like a wave I am having some trouble programming 3 servos to move like a wave.  So, for instance, all my servos start at 90 degrees. I want my first servo to move and when it reaches 135 degrees I want my second servo to move, with the first, then when it reaches 135 degrees I want my third servo to move, with the first and the second. Then go backwards. Does that make sense?

Here is what I have so far.

#include &lt;Servo.h&gt;

//Create servo object to control a servo
Servo left;
Servo middle;
Servo right;

//Variables to store the servo position
int post = 180;
int post2 = 90;
int back = 90;
int forward = 135;

int firstTriggerAngle = 135;
int secondTriggerAngle = 180;

//Delay
int mid = 4;

void setup() 
{
  Serial.begin(9600);

  //Attaches the servos on the pins of the arduino to the servo object
  left.attach(4);
  middle.attach(3);
  right.attach(2);

  //Starting position
  left.write(back);
  middle.write(back);
  right.write(back);
}

void loop()
{
  for (int i=back; i&gt;=forward; i+=1)
  {
    left.write(i);
    if( i &gt; firstTriggerAngle)
    {
      post = map(i, 90, 135, 135, 90);
      middle.write(post);
    }
    if(post &gt; secondTriggerAngle)
    {
      post2 = map(post, 90, 135, 135, 90);  // depends on the angles you use
      right.write(post2);
    }
    delay(mid);
 }
}
The easiest way is to define how many degrees you want to move totally .

In your case it is 180 steps (from 90 to 180, which is 90 steps, and 180 to 90, which is another 90 steps).

Than use a for loop with a 180 step-counter and make 3 simple formulas to map the 180 possible degrees to the wanted servo position.

So the first formula will look like (step 0..90 -> move from 90 to 180, step 90..180-> move 180 to 0).

int calcServo1(int step)
{
    if (step &lt; 90)
    {
        return 90 + step;
    }
    else
    {
        return 270 - step; // 180 - (step - 90);
    }
}


For the second it should be:


step is 0..45: first iteration: 90, otherwise 135..90
step is 45..135: 90..180
step is 135..180: 180..135


The code will be something like:

int calcServoPos2(int iteration, int step)
{ 
    if (step &lt; 45)
    {
        if (iteration == 0)
        {
            return 90;
        }
        else
        {
            return 135 - step;
        }
   else if (step &lt; 135)
   {
         return step + 45;
   }
   else
   {
       return step - 45; //  step - 180 + 135
   }   
}


Servo 3 I will leave up to you.

From your main loop you call the positions:

 int iteration = 0;

 while (loop)
 {
      for (int step = 0; step &lt; 180; step++)
      {
         int servo1pos = CalcServo1(iteration, step);
         servo1.write(servo1pos);

         int servo2pos = CalcServo2(iteration, step);
         servo2.write(servo2pos);

         int servo3pos = CalcServo2(iteration, step);
         servo3.write(servo3pos);

         delay(mid);
     }
     iteration++;
  }