#include <ESP32Servo.h>
#define SERVO_PIN 3 // Servo motor pin
#define BUTTON_PIN 6 // Button pin
Servo servo;
bool direction = false; // false = left, true = right
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // Pull-up resistor
servo.attach(SERVO_PIN);
servo.write(90); // Initial servo position
Serial.begin(115200); // Initialize serial monitor
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) { // If the button is pressed
direction = !direction; // Toggle direction
if (direction) {
servo.write(180); // Move to the right
Serial.println("RIGHT");
} else {
servo.write(0); // Move to the left
Serial.println("LEFT");
}
delay(300); // Small delay to prevent bouncing
while (digitalRead(BUTTON_PIN) == LOW); // Wait for the button to be released
}
}