const int servoPin = A1; // Pin A1 connected to the servo signal wire // Function to write an angle to the servo motor void writeServoAngle(int angle) { // Convert angle (0-180) to microseconds (1000-2000) int pulseWidth = map(angle, 0, 180, 1000, 2000); // Send the PWM signal digitalWrite(servoPin, HIGH); delayMicroseconds(pulseWidth); // HIGH pulse duration digitalWrite(servoPin, LOW); delay(20 - (pulseWidth / 1000)); // Rest of the 20ms period } void setup() { pinMode(servoPin, OUTPUT); } void loop() { // Sweep the servo from 0 to 180 degrees for (int angle = 0; angle <= 180; angle += 10) { writeServoAngle(angle); delay(500); // Wait for servo to reach position } // Sweep the servo from 180 to 0 degrees for (int angle = 180; angle >= 0; angle -= 10) { writeServoAngle(angle); delay(500); // Wait for servo to reach position }