#include <Servo.h>

#define NUM_SERVOS 16
Servo myServo[NUM_SERVOS];

void setup() 
{
  // Attach pins from the Arduino Mega board to the Servo objects.
  // Starting from pin 22, there happen to be exactly 32 pins on the double row pins.
  for( int i=0; i<NUM_SERVOS; i++)
  {
    myServo[i].attach( i + 1);      // pin 22 up to 53 is 32 pins
  }
}
void loop()
{
  for( int i=0; i<NUM_SERVOS; i++)
  {
    myServo[i].write( 0);            // set to begin position (horn is rotated left)
  }
  delay( 1000);                      // wait to let the viewer get used to it

  for( int a=0; a<3; a++)
  {
    for( int r=0; r<=180; r++)       // move horns to the right
    {
      for( int i=0; i<NUM_SERVOS; i++)
      {
        myServo[i].write( r);
      }
      delay( 6);
    }
    for( int r=180; r>=0; r--)
    {
      for( int i=0; i<NUM_SERVOS; i++)  // move horns to the left
      {
        myServo[i].write( r);
      }
      delay( 6);
    }
  }

for( int a=0; a<6; a++)
  {
    for( int i=0; i<NUM_SERVOS; i++)
    {
      for( int j=0; j<NUM_SERVOS; j++)
      {
        // Calculate distance to active servo
        int d = j - i;
        if( d < 0)
          d = -d;
        if( d > (NUM_SERVOS / 2))
          d = NUM_SERVOS - d;

        int angle = 90 - (10 * d);
        if( angle < 0)
          angle = 0;
        myServo[j].write( angle);
      }
      delay(40);
    }
  }
}
void showPointer( int s)
{
  int pointerA = s % NUM_SERVOS;        // Using the '%' (remainder) for valid number
  int pointerB = (s + 1) % NUM_SERVOS;  // pointer is made with the next servo motor
  int tailA = (s + 16) % NUM_SERVOS;
  int tailB = (s + 17) % NUM_SERVOS;

  // make pointer with servo motor s and s+1.
  myServo[pointerA].write(180-56);
  myServo[pointerB].write(56);

  // make tail with servo motor s+16 and s+17.
  myServo[tailA].write(95);
  myServo[tailB].write(85);

  // Set servos right of pointer
  int n = (NUM_SERVOS / 2) - 2;
  int start = pointerB + 1;
  for( int i=0; i<n; i++)
  {
    int j = (start + i) % NUM_SERVOS;
    myServo[j].write( 2);
  }

  // Set servos left of pointer
  start = tailB + 1;
  for( int i=0; i<n; i++)
  {
    int j = (start + i) % NUM_SERVOS;
    myServo[j].write( 178);
  }
}