#include <ESP32Servo.h>
const int servoPin = 15; // GPIO pin connected to the servo
Servo servo; // Create a Servo object
int currentPos = 90; // Current servo position, start at 90 (middle position)
void setup() {
Serial.begin(115200); // Start the Serial Monitor at 115200 baud rate
servo.attach(servoPin, 500, 2400); // Attach the servo on the pin and set the pulse width range
servo.write(currentPos); // Set initial position
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read(); // Read the command from the Serial Monitor
if (command == 'L') { // Left command
currentPos = max(0, currentPos - 90); // Decrease position by 90 degrees but not below 0
servo.write(currentPos); // Set the servo position
delay(1000); // Wait for the servo to hold the position for 1 second
currentPos = 90; // Reset to normal position
servo.write(currentPos); // Set the servo back to 90 degrees
delay(15); // Wait for the servo to reach the position
} else if (command == 'R') { // Right command
currentPos = min(180, currentPos + 90); // Increase position by 90 degrees but not above 180
servo.write(currentPos); // Set the servo position
delay(1000); // Wait for the servo to hold the position for 1 second
currentPos = 90; // Reset to normal position
servo.write(currentPos); // Set the servo back to 90 degrees
delay(15); // Wait for the servo to reach the position
}
}
}