#include <Servo.h>
class Sweeper
{
Servo servo; // the servo
int pos; // current servo position
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
public:
Sweeper(int interval)
{
updateInterval = interval;
increment = 1;
}
void Attach(int pin)
{
servo.attach(pin);
}
void Detach()
{
servo.detach();
}
void Update()
{
if ((millis() - lastUpdate) > updateInterval)
{
lastUpdate = millis();
pos +=increment;
servo.write(pos);
if ((pos <= 0) || (pos >= 180))
{
increment = -increment; // Reverse direction
}
}
}
};
Sweeper sweeper1(15);
Sweeper sweeper2(25);
void setup(){
Serial.begin(9600);
sweeper1.Attach(2);
sweeper2.Attach(3);
}
void loop(){
sweeper1.Update();
sweeper2.Update();
}