//https://en.wikipedia.org/wiki/Servomotor
#include <Servo.h> //including the servo library
#define SERVO_PIN 3 //define as a constant the SERVO PIN
// global variables
bool positive = 1;
int angle= 0;
Servo Servo1; // declare a new variable of type Servo
void setup() {
pinMode(SERVO_PIN, OUTPUT); //setup the pin as output
Servo1.attach(SERVO_PIN);
}
// we move the servo from 0 to 180 and back, min and max angle
void loop() {
//managing the rotation inversion
if (angle > 180){
positive = 0;
angle = 180;
} else if (angle < 0){
positive = 1;
angle = 0;
}
//Start position of the servo
Servo1.write(angle);
if (positive) angle= angle+10; //increase or decrease the angle
else angle= angle-10;
delay(30); //Choose the right delay: the motor need time to reach the position
}