#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int ledPin = 13; // Pin for the LED
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(ledPin, OUTPUT); // set the LED pin as an output
}
void loop() {
// Sweep from 0 degrees to 180 degrees
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(ledPin, HIGH); // Turn on the LED when servo is ON
// Sweep from 180 degrees to 0 degrees
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(ledPin, LOW); // Turn off the LED when servo is OFF
}