#include <Servo.h>
Servo servo1; // create servo object to control a servo
int position = 0; // variable to store the servo position
void setup() {
servo1.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (position = 0; position <= 180; position += 1) { // rotate from 0 degrees to 180 degrees
// in steps of 1 degree
servo1.write(position); // tell servo to go to position in variable 'pos'
delay(18); // waits Xms for the servo to reach the position
}
for (position = 180; position >= 0; position -= 1) { // rotate from 180 degrees to 0 degrees
servo1.write(position); // tell servo to go to position in variable 'pos'
delay(18); // waits Xms for the servo to reach the position
}
}
//reference https://maker.pro/arduino/projects/servo-motor-on-virtual-arduino-online-simulator