// Include the ESP32Servo library #include // Define pin connections const int servoPin = D3; // Pin connected to the servo const int buttonPin1 = D0; // Pin connected to the first button const int buttonPin2 = D10; // Pin connected to the second button // Create a Servo object Servo myServo; // Initial servo angle and increment for button press int angle = 90; // Initial angle of the servo int increment = 3; // Amount to increase/decrease the angle when a button is pressed // Variables for debouncing unsigned long lastPressTime = 0; // Time when a button was last pressed const unsigned long debounceDelay = 50; // Debounce delay to prevent multiple detections // Setup configuration void setup() { pinMode(buttonPin1, INPUT_PULLUP); // Initialize the first button pin as an input with internal pull-up resistor pinMode(buttonPin2, INPUT_PULLUP); // Initialize the second button pin as an input with internal pull-up resistor myServo.attach(servoPin); // Attach the servo on the servoPin to the Servo object myServo.write(angle); // Initialize the servo to its initial position } // Main program loop void loop() { // Check for button press with debouncing if (millis() - lastPressTime > debounceDelay) { // If the first button is pressed, move the servo in one direction if (digitalRead(buttonPin1) == LOW) { if(angle < 180) { // Check if the angle is less than 180 degrees angle += increment; // Increase the angle myServo.write(angle); // Move the servo to the new angle } lastPressTime = millis(); // Update the lastPressTime } // If the second button is pressed, move the servo in the opposite direction if (digitalRead(buttonPin2) == LOW) { if(angle > 0) { // Check if the angle is greater than 0 degrees angle -= increment; // Decrease the angle myServo.write(angle); // Move the servo to the new angle } lastPressTime = millis(); // Update the lastPressTime } } }