const int relay1Pin = 4;
const int relay2Pin = 5;
const int limitSwitch1Pin = 7;
const int limitSwitch2Pin = 8;
const int pushButtonPin = 2;
bool isCycleRunning = false;
void setup() {
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(limitSwitch1Pin, INPUT);
pinMode(limitSwitch2Pin, INPUT);
pinMode(pushButtonPin, INPUT_PULLUP);
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
}
void loop() {
int buttonState = digitalRead(pushButtonPin);
if (buttonState == LOW && !isCycleRunning) {
// Button is pressed and cycle is not already running
isCycleRunning = true;
digitalWrite(relay1Pin, HIGH);
}
if (isCycleRunning) {
if (digitalRead(limitSwitch1Pin) == LOW) {
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH);
} else if (digitalRead(limitSwitch2Pin) == LOW) {
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
}
}
if (isCycleRunning && buttonState == HIGH) {
// Button is released, stop the cycle
isCycleRunning = false;
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
}
}