#include <Servo.h>
const byte servoPin = 3;
unsigned int defUs = 1472; // sets initial pulse width (default angle) for servo (beteen 544 and 2400)
unsigned int usStep = 10;
const unsigned int minUs = 544;
const unsigned int maxUs = 2400;
const unsigned int speed = 1; // step μs (1 slow to 100 fast)
bool dir;
unsigned int pos;
Servo servo; // create servo object
void setup() {
servo.attach(servoPin); // attaches the servo on pin 3 to the servo object
pos = defUs; // initial position
}
void loop() {
servo.writeMicroseconds(pos);
if (dir) {
if (pos <= maxUs) {
pos += speed;
} else {
dir = false;
}
} else {
if (pos >= minUs) {
pos -= speed;
} else {
dir = true;
}
}
delay(15); // loop speed determines the maximum servo speed limit
}