//已有使用ESP32Servo的library
#include<ESP32Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
int pos=0;
void setup() {
  servo1.attach(15);
  servo2.attach(2);
  servo3.attach(4);
  pinMode(5,INPUT);
  pinMode(18,INPUT);
  pinMode(19,INPUT);
}
int function1(){
  for (int i=0 ; i<=180 ;i++){
    servo1.write(i);
    servo2.write(i);
    servo3.write(i);
    delay(5);
  }
  for (int i=180 ; i>=90 ;i--){
    servo1.write(i);
    servo2.write(i);
    servo3.write(i);
    delay(5);
  }
  return 0;
}
int function2(){
  for(int i=0;i<=50;i++){
    servo1.write(i);
    delay(5);
  }
  for(int i=0;i<100;i++){
    servo2.write(i);
    delay(5);
  }
  for(int i=0;i<150;i++){
    servo3.write(i);
    delay(5);
  }
  return 0;
}
int function3(){
  servo1.write(90);
  servo2.write(90);
  servo3.write(90);
  delay(200);
  for(int r=0;r<3;r++){
    for(int i=90;i>=60;i--){
      servo1.write(i);
      servo2.write(i);
      servo3.write(i);
      delay(10);
    }
    for(int i=60;i<=90;i++){
      servo1.write(i);
      servo2.write(i);
      servo3.write(i);
      delay(10);
    }
    for(int i=90;i<=120;i++){
      servo1.write(i);
      servo2.write(i);
      servo3.write(i);
      delay(5);
    }
    for(int i=120;i>=90;i--){
      servo1.write(i);
      servo2.write(i);
      servo3.write(i);
      delay(5);
    }
  }
  return 0;
}
void loop() {
  servo1.write(pos);
  servo2.write(pos);
  servo3.write(pos);
  delay(500);
  if(digitalRead(5)==HIGH){
    function1();
    delay(500);
    return;
  }
  
  if(digitalRead(18)==HIGH){
    function2();
    delay(500);
    return;
  }
  
  if(digitalRead(19)==HIGH){
    function3();
    delay(500);
    return;
  }
}