#include <Servo.h> // Include the Servo library
Servo myServo; // Create a Servo object
void setup() {
myServo.attach(9); // Attach the Servo control pin to digital pin 9
}
void loop() {
// Sweep the servo from 0 to 180 degrees
for (int pos = 0; pos <= 180; pos += 1) { // Goes from 0 to 180 degrees
myServo.write(pos); // Tell the servo to go to the position in variable 'pos'
delay(15); // Wait 15 milliseconds for the servo to reach the position
}
// Sweep the servo back from 180 to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) { // Goes from 180 to 0 degrees
myServo.write(pos); // Tell the servo to go to the position in variable 'pos'
delay(15); // Wait 15 milliseconds for the servo to reach the position
}
}