#include <Servo.h>
Servo wiperServo;
int servoPin = 9;
int timerDuration = 60000; // 60 seconds
unsigned long startTime = 0;
bool timerRunning = false;
#define start_Button 2
void setup() {
wiperServo.attach(servoPin);
wiperServo.write(0); // Set the initial position to 0 degrees
pinMode(start_Button, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int start_Button_state = digitalRead(start_Button);
if (start_Button_state == LOW && !timerRunning) {
// Start the timer when the button is pressed
startTime = millis();
timerRunning = true;
}
// Calculate the elapsed time since the timer started
unsigned long elapsedTime = millis() - startTime;
// If the timer is still running
if (timerRunning && elapsedTime < timerDuration) {
// Calculate the servo position based on elapsed time
int servoPosition = map(elapsedTime, 0, timerDuration, 0, 90);
// Move the servo smoothly to the calculated position
for (int pos = 0; pos <= servoPosition; pos++) {
wiperServo.write(pos);
delay(10); // Adjust this delay for smoother motion
}
Serial.println(servoPosition);
} else {
// Timer has ended, move the servo to the final position (90 degrees)
wiperServo.write(90);
// Reset the timer
timerRunning = false;
}
}