/*
         +----------| USB |--------+
         | D13/SCK        MISO/D12 |
         | 3.3V           MOSI/D11~|
         | Vref             SS/D10~|
         | A0                   D9~|
         | A1       NANO        D8 |
         | A2                   D7 |
         | A3                   D6~|
         | A4/SDA               D5~|
         | A5/SCL               D4 |
         | A6              INT1/D3~|
         | A7              INT0/D2 |
         | 5V                  GND |
         | RST                 RST |
         | GND   5V  DO GND    TX1 |
         | Vin   DI  SCK  RST  RX1 |
         +-------------------------+
*/

#define   srvnum 8
#include <Servo.h>
Servo srv[srvnum]; // create servo array of objects to control servo array

int val[srvnum];  // variable array to read value from analog pot pin
int srvpin[srvnum] = {9, 10, 11, 12, 2, 3, 4, 5};  // servo pins
int pot[srvnum]; // array of potentiometers
int potpin[srvnum] = {0, 1, 2, 3, 4, 5, 6, 7};  // pot pins

void setup() {
  for (int i = 0; i < srvnum; i++)
  {
    srv[i].attach(srvpin[i]); // attach servo to servo object
  }
  angles();
}

void loop() {
  for (int i = 0; i < srvnum; i++)
  {
    val[i] = analogRead(potpin[i]);  // pot 0 to 1023
    val[i] = map(val[i], 0, 1023, 0, 180);  // scale pot (0 to 1023) to servo (0 and 180)
    srv[i].write(val[i]);  // set servo position
  }
  delay(15);  // wait for servo to settle
}

void angles()
{
  int states = 8;
  int angle[srvnum] = {168, 145, 123, 101, 89, 66, 45, 22};
  for (int i = 0; i < states; i++)
  {
    srv[i].write(angle[i]);
    // delay(100);
  }
  for (int i = (states - 1); i >= 0; i--)
  {

    srv[i].write(angle[i]);
    // delay(100);
  }
  delay(500);
}