#include <Servo.h>
Servo Wiper1;
Servo Wiper2;
int Activator_pin = 2;
int Wiper1_pin = 3;
int Wiper2_pin = 5;
const unsigned int restAngle = 20; //resting position of wipers
const unsigned int maxAngle = 160; //maximum position of wipers
unsigned int angle = restAngle; //current position of wipers
int deltaAngle = 1; //change angle by this, each pass through the code; starts positive if restAngle<maxAngle, negative if opposite
bool runningFlag = false; //start off at rest
int speed = 2; //increase to slow the wipers, decrease to speed them up
void setup() {
pinMode(Activator_pin, INPUT_PULLUP); //wired pin to button to GND, means input will be low when button pressed
Wiper1.write(restAngle); //Write BEFORE attach, to set initial angle appropriately
Wiper2.write(restAngle);
Wiper1.attach(Wiper1_pin);
Wiper2.attach(Wiper2_pin);
}
void loop() {
if (digitalRead(Activator_pin) == 0) runningFlag = true; //inverted due to low-active button
else if (angle <= restAngle) runningFlag = false; //we only turn off the wiper at minimum
if (runningFlag) {
angle = angle + deltaAngle;
if (angle >= maxAngle || angle <= restAngle) deltaAngle = deltaAngle * -1; //turn around at endpoints
Wiper1.write(angle);
Wiper2.write(angle);
delay(speed); //speed constant could be modified, for example a high/low selector
}
}