#include <Servo.h>
Servo servoControl;
int servoPosition;
void setup() {
servoControl.attach(9);//setup PWM pin 9 on the arduino board to control servo motor
// put your setup code here, to run once:
}
void loop() {
for (servoPosition = 0; servoPosition <= 180; servoPosition += 20) //rotate the servo from 0-180 degrees in increment of 20 degree
{
servoControl.write(servoPosition); //Writes a value to the servo thus control a shaft position between 0-180 degrees as per the loop values.
delay(500); //delays the rotate by 500ms.
}
for (servoPosition = 180; servoPosition >= 0; servoPosition -= 20) //rotates the servo back from 180-0 degrees in decrement of 20 degree
{
servoControl.write(servoPosition); //Writes a value to the servo thus controlling a shaft position between 180-0 degrees as per the loop values.
delay(500); //delays the rotate by 500ms.
}
// put your main code here, to run repeatedly:
}