// Example code for controlling a servo
// First include the servo library,
// this must be installed in the computer via Arduino IDE
#include <Servo.h>
// Define the pin that is used for sending data (i.e. the
// position) to the servo, and define a variable for the
// angle of the servo.
int servoPin = 8;
int servoAngle = 0;
// Declare a servo object. In this example it is called
// flagServo, but can be called anything
Servo flagServo;
void setup() {
// Connect the servo object to the servo pin using attach
flagServo.attach(servoPin);
// Tell the servo to turn to the angle (servoAngle) using
// write
flagServo.write(servoAngle);
}
void loop() {
// Use a if statement to check the angle that is set for
// the servo, if it is 0 the new angle should be 90, if
// it is 90 it should be 180, and if it is 180 it should
// be 0.
if (servoAngle == 0) {
servoAngle = 90;
} else if (servoAngle == 90) {
servoAngle = 180;
} else if (servoAngle == 180) {
servoAngle = 0;
}
// Tell the servo to go to the new angle
flagServo.write(servoAngle);
// Wait one second before checking the new angle and
// moving the servo again
delay(1000);
}