#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
}
}
esp:D0
esp:D1
esp:D2
esp:D3
esp:D4
esp:D5
esp:D6
esp:D7
esp:D8
esp:D9
esp:D10
esp:3V3
esp:GND
esp:5V
servo1:GND
servo1:V+
servo1:PWM
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
r4:1
r4:2