#include <Servo.h>
#include <ezButton.h>

#define BUTTON_PIN 21 // pin GPIO21 connected to the button's pin
#define SERVO_PIN 26  // pin GPIO26 connected to the servo motor's pin

ezButton button(BUTTON_PIN);  // create ezButton object that attach to pin 21
Servo servo;                  // create servo object to control a servo motor

int angle = 0;  // the current angle of the servo motor

void setup() {
  Serial.begin(115200); // Initialize serial
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  servo.attach(SERVO_PIN);    // attaches pin 26 to the servo object

  servo.write(angle); // set angle 0° for servo motor
}

void loop() {
  button.loop();  // MUST call loop() function first

  if (button.isPressed())
    {
      // change angle of servo motor
      if (angle == 0)
        angle = 90;
      else if (angle == 90)
        angle = 0;

      // Control servo motor according to angle
      Serial.print("The button is pressed => rotate servo to ");
      Serial.print(angle);
      Serial.println("°");
      servo.write(angle);
    }
}