#define SERVO_PIN D9 // Define the pin for the servo
void setup() {
pinMode(SERVO_PIN, OUTPUT); // Set the servo pin as an output
}
void loop() {
// Sweep from 0 to 180 degrees
for (int angle = 0; angle <= 180; angle++) {
int pulseWidth = map(angle, 0, 180, 500, 2400); // Map angle to pulse width (in microseconds)
digitalWrite(SERVO_PIN, HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(SERVO_PIN, LOW);
delay(20); // Wait for the servo to reach the position
}
// Sweep back from 180 to 0 degrees
for (int angle = 180; angle >= 0; angle--) {
int pulseWidth = map(angle, 0, 180, 500, 2400); // Map angle to pulse width
digitalWrite(SERVO_PIN, HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(SERVO_PIN, LOW);
delay(20); // Wait for the servo to reach the position
}
}