#include <ESP32Servo.h>
Servo myservo; // create servo object to control a servo
int button1Pin = 12; // pin for button 1
int button2Pin = 14; // pin for button 2
int button1State = 0; // variable to store the state of button 1
int button2State = 0; // variable to store the state of button 2
int angle = 0; // variable to store the servo angle
void setup() {
Serial.begin(9600); // initialize serial communication at 9600 bits per second
myservo.attach(18); // attaches the servo on GPIO pin 2 to the servo object
myservo.write(angle);
pinMode(button1Pin, INPUT_PULLUP); // configure button 1 as an input with internal pull-up resistor enabled
pinMode(button2Pin, INPUT_PULLUP); // configure button 2 as an input with internal pull-up resistor enabled
}
void loop() {
button1State = digitalRead(button1Pin); // read the state of button 1
button2State = digitalRead(button2Pin); // read the state of button 2
if (button1State == LOW) { // if button 1 is pressed
angle = 90; // set the angle to 90 degrees
myservo.write(angle); // set the servo to the new angle
Serial.println("Button 1 pressed"); // print a message to the Serial Monitor
while(digitalRead(button1Pin) == LOW){} // wait for button 1 to be released
}
else if (button2State == LOW) { // if button 2 is pressed
angle = 0; // set the angle to 0 degrees
myservo.write(angle); // set the servo to the new angle
Serial.println("Button 2 pressed"); // print a message to the Serial Monitor
while(digitalRead(button2Pin) == LOW){} // wait for button 2 to be released
}
delay(20); // add a small delay to prevent the ESP32 from flooding the Serial Monitor with messages
}