Hi, i have built a simple project that moves a servo.
The idea is that when I press the switch it moves the servo to a position, waits fot 4 sec, then moves it to another position, and waits for 40 sec.
this cycle is repeated five times and than it should wait for an other press on the switch again and do it all again.
Everything works as it should except I can't get it to start again with the second press.
Can anyone give me a hint what's wrong in my code?
#include <Servo.h>
Servo myservo;
const int switchPin = 2; // set switch input pin
int switchState = HIGH; // initial state of switch
int lastSwitchState = HIGH; // last state of switch
unsigned long lastDebounceTime = 0; // last time switch state changed
unsigned long debounceDelay = 50; // debounce delay in milliseconds
void setup() {
myservo.attach(3); // attach servo to pin 3
pinMode(switchPin, INPUT_PULLUP); // set switch pin as input with pull-up resistor
myservo.write(53); // move servo to 53 degrees
}
void loop() {
switchState = digitalRead(switchPin); // read switch state
if(switchState != lastSwitchState) { // check if switch state has changed
lastDebounceTime = millis(); // reset debounce timer
}
if((millis() - lastDebounceTime) > debounceDelay) { // check if debounce delay has passed
if(switchState == LOW) { // check if switch is pressed
for(int i=0; i<5; i++) { // repeat the cycle 5 times
myservo.write(23); // move servo to 23 degrees
delay(4000); // wait for 4 seconds
myservo.write(53); // move servo to 53 degrees
delay(40000); // wait for 40 seconds
}
myservo.detach(); // stop the servo
}
}
lastSwitchState = switchState; // update last switch state
}