#define SERVO_PIN 2 // Define the pin connected to the servo motor
void setup() {
pinMode(SERVO_PIN, OUTPUT); // Set the servo pin as an output
}
void servoRotate(int angle) {
int pulseWidth = map(angle, 0, 180, 500, 2400); // Map the angle to pulse width
// Generate PWM signal
digitalWrite(SERVO_PIN, HIGH); // Set the pin high
delayMicroseconds(pulseWidth); // Wait for the pulse width
digitalWrite(SERVO_PIN, LOW); // Set the pin low
delay(20 - (pulseWidth / 1000)); // Wait for the remaining period of the 20ms cycle
}
void loop() {
if(1==0){
for(int i=0;i<=180;i++){servoRotate(i);delay(0);}
for(int i=180;i>=0;i--){servoRotate(i);delay(0);}
}
}