// Include the Servo library to control the servo motor. #include // Create a servo object named myServo for controlling a servo. Servo myServo; // Attach the servo on pin 9 to the servo object for PWM control. void setup() { myServo.attach(9); } // Move the servo from 0 to 180 degrees void loop() { // Goes from 0 degrees to 180 degrees for(int angle = 0; angle <= 180; angle += 1) { myServo.write(angle); // Tell the servo to go to the position in angle. delay(15); // Wait 15ms between each position to slow down the movement. } // Move the servo from 180 to 0 degrees for(int angle = 180; angle >= 0; angle -= 1) { // Goes from 180 degrees back to 0 degrees myServo.write(angle); // Tell the servo to go to the position in angle. delay(15); // Wait 15ms between each position to slow down the movement. } }