#include <ESP32Servo.h>

#define SERVO_PIN 18   // Servo signal pin
#define CW_BUTTON 4    // Clockwise button
#define CCW_BUTTON 5   // Counterclockwise button

Servo myServo;        // Create servo object
int angle = 90;       // Start at 90° (neutral position)

void setup() {
  myServo.attach(SERVO_PIN, 500, 2500);  // Attach servo with min/max pulse width
  pinMode(CW_BUTTON, INPUT_PULLUP);
  pinMode(CCW_BUTTON, INPUT_PULLUP);
  myServo.write(angle);  // Set servo to initial position
}

void loop() {
  if (digitalRead(CW_BUTTON) == LOW) {  // If CW button is pressed
    if (angle < 180) {  // Limit to 180°
      angle += 5;
      myServo.write(angle);
      delay(100);  // Small delay to avoid fast movement
    }
  }

  if (digitalRead(CCW_BUTTON) == LOW) {  // If CCW button is pressed
    if (angle > 0) {  // Limit to 0°
      angle -= 5;
      myServo.write(angle);
      delay(100);
    }
  }
}
