int isLoop = 1;
unsigned long previousMillis = 0; // Stores the last time the action was updated
const long interval = 3000;
int reset = 0;
void setup() {
pinMode(7, OUTPUT); // Configure pin 7 as an Output
pinMode(8, OUTPUT); // Configure pin 8 as an Output
pinMode(2, INPUT_PULLUP); //green start/pause
pinMode(3, INPUT_PULLUP); //red stop
pinMode(4, INPUT_PULLUP); //yellow button reset
digitalWrite(7, LOW); // Initialize pin 7 as Low
digitalWrite(8, LOW); // Initialize pin 7 as Low
}
void loop() {
if (digitalRead(2) == LOW) {
while (isLoop) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Save the last update time
// Toggle the actuator direction
if (digitalRead(7) == LOW) {
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
// Serial.print("retract");
} else {
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
// Serial.print("extend");
}
}
// if(digitalRead(4) == LOW){
// isLoop = 0;
// digitalWrite(7, HIGH);
// digitalWrite(8, LOW);
// // delay(3000);
// //break; // Exit the loop as soon as button 4 is pressed and the state is set
// }
// Check button 3 state without delay
if (digitalRead(3) == LOW) {
isLoop = 0; // Exit loop immediately
digitalWrite(7, LOW); // Stop the actuator
digitalWrite(8, LOW);
reset = 1;
break; // Exit the while loop
}
}
}
if(reset && digitalRead(4) == LOW){
isLoop = 0;
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
reset = 0;
// delay(3000);
//break; // Exit the loop as soon as button 4 is pressed and the state is set
}
isLoop = 1; // Reset isLoop for the next cycle
// Ensuring actuators are stopped before leaving the loop
// digitalWrite(7, LOW);
// digitalWrite(8, LOW);
}