//นาย กิจเกษม ผิวอ่อน ปวส.2/1 เลขที่26
#include<ESP32Servo.h>
Servo servo1, servo2, servo3, servo4, servo5, servo6, servo7, servo8;
int a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0;
int mode = 1;
int sw1 = 0, locksw1 = 0, sw2 = 0, sw3 = 0;
void setup() {
  Serial.begin(115200);
  pinMode(15, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  servo1.attach(13, 500, 2400);
  servo2.attach(12, 500, 2400);
  servo3.attach(14, 500, 2400);
  servo4.attach(27, 500, 2400);
  servo5.attach(26, 500, 2400);
  servo6.attach(25, 500, 2400);
  servo7.attach(33, 500, 2400);
  servo8.attach(32, 500, 2400);
}

void loop() {
  sw1 = digitalRead(15);
  sw2 = digitalRead(2);
  sw3 = digitalRead(4);
  if ((sw1 == 0) && (locksw1 == 0)) {
    mode++;
    locksw1 = 1;
    if (mode > 8) {
      mode = 1;
    }
  }
  if ((sw1 == 1) && (locksw1 == 1)) {
    locksw1 = 0;
  }
  if (sw2 == 0) {
    if ((mode == 1) && (a1 < 180)) {
      a1++;
    }
    if ((mode == 2) && (a2 < 180)) {
      a2++;
    }
    if ((mode == 3) && (a3 < 180)) {
      a3++;
    }
    if ((mode == 4) && (a4 < 180)) {
      a4++;
    }
    if ((mode == 5) && (a5 < 180)) {
      a5++;
    }
    if ((mode == 6) && (a6 < 180)) {
      a6++;
    }
    if ((mode == 7) && (a7 < 180)) {
      a7++;
    }
    if ((mode == 8) && (a8 < 180)) {
      a8++;
  }
  }
  if (sw3 == 0) {
    if ((mode == 1) && (a1 > 0)) {
      a1--;
    }
    if ((mode == 2) && (a2 > 0)) {
      a2--;
    }
    if ((mode == 3) && (a3 > 0)) {
      a3--;
    }
    if ((mode == 4) && (a4 > 0)) {
      a4--;
    }
    if ((mode == 5) && (a5 > 0)) {
      a5--;
    }
    if ((mode == 6) && (a6 > 0)) {
      a6--;
    }
    if ((mode == 7) && (a7 > 0)) {
      a7--;
    }
    if ((mode == 8)&& (a8 > 0)) {
      a8--;
    }
  }
  servo1.write(a1);
  servo2.write(a2);
  servo3.write(a3);
  servo4.write(a4);
  servo5.write(a5);
  servo6.write(a6);
  servo7.write(a7);
  servo8.write(a8);
  Serial.print(mode);
  Serial.print("\t");
  Serial.print(a1);
  Serial.print("\t");
  Serial.print(a2);
  Serial.print("\t");
  Serial.print(a3);
  Serial.print("\t");
  Serial.print(a4);
  Serial.print("\t");
  Serial.print(a5);
  Serial.print("\t");
  Serial.print(a6);
  Serial.print("\t");
  Serial.print(a7);
  Serial.print("\t");
  Serial.println(a8);

  delay(50);
}