#include <Servo.h>
Servo myservo; // create servo object to control a servo
bool increase; // = true; //flag to keep track of direction of sweep
int numberSweep(int interval, int start, int end) {
static unsigned long previousMillis = 0; //keep track of time
unsigned long currentMillis = millis(); //get current time
if (currentMillis - previousMillis >= interval) { //check if interval has passed
previousMillis = currentMillis; //update previous time
static int currentValue = start; //keep track of current value
//check if current value has reached end
if (currentValue >= end) {
increase = false;
}
//check if current value has reached start
else if (currentValue <= start) {
increase = true;
}
//increment or decrement current value based on flag
if (increase) {
currentValue++;
}
else {
currentValue--;
}
return currentValue;
}
}
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(115200);
}
void loop() {
int number = numberSweep(1, 600, 2400);
myservo.writeMicroseconds(number);
Serial.println(number);
}