int state = 0; // 0=Stopped, 1=Extended, 2=Retracted
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
// SAFE START: Both set to HIGH so the actuator/relays are OFF
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
}
void loop() {
// Check if button is pressed
if (digitalRead(2) == LOW) {
state++;
if (state > 2) state = 1; // Cycle: Extend -> Retract -> Extend...
if (state == 1) {
digitalWrite(4, LOW); // Activate Relay 1 (Extend)
digitalWrite(5, HIGH); // Relay 2 at rest (GND)
}
else if (state == 2) {
digitalWrite(4, HIGH); // Relay 1 at rest (GND)
digitalWrite(5, LOW); // Activate Relay 2 (Retract)
}
delay(400); // Small delay to prevent bouncing
while(digitalRead(2) == LOW); // Wait until the user releases the button
}
}