#include <Servo.h> //servo libary
Servo myservo;//object servo motor
const int servoPin = 9; //servo motor conected on pin 9
int servoPosition;
bool wiperOn = 0;
bool wiperOn2 = 0;
long servoInterval = 250; // 1st speed of wiper
long servoInterval2 = 900; // 2nd speed of wiper (faster)
const int button1Pin = 2; // the button 1 is connected to pin 2
bool button1 = 0;
const int button2Pin = 3; // button 2 is connected to pin 3
bool button2 = 0;
void setup() {
Serial.begin(115200); // not shure about this numbers (copied)
pinMode(button1Pin, INPUT);
myservo.attach(servoPin);
servoHome();
welcome();
Serial.print("OFF ");
}
void loop () {
readbutton1();
readbutton2();
}
void readbutton1() {
button1 = digitalRead(button1Pin);
if (button1) {
delay(50);
if (!wiperOn) {
Serial.print("ON ");
wiperOn = 1;
wiperOn2 = 0;
}
moveWiper();
}
if (wiperOn)
moveWiper();
}
void readbutton2() {
button2 = digitalRead(button2Pin);
if (button2) {
delay(50);
if (!wiperOn2) {
Serial.print("ON2 ");
wiperOn = 0;
wiperOn2 = 1;
}
moveWiper2();
}
if(wiperOn2);
moveWiper2();
}
void moveWiper() {
// this is a blocking routine... no buttons will be read while the servo is moving
myservo.write(180 - servoPosition);
servoPosition = 180 - servoPosition;
delay(servoInterval);
}
void moveWiper2() {
// this is a blocking routine... no buttons will be read while the servo is moving
myservo.write(180 - servoPosition);
servoPosition = 90 - servoPosition;
delay(servoInterval2);
}
void servoHome() {
myservo.write(servoPosition);
}
void welcome() {
Serial.println("Press the RED button to start.");
Serial.println("Press the GREEN button to start.");
}