Updated Code Relay2
Updated Code Relay2
enum SequenceState {
IDLE,
IN1_ON,
IN1_OFF,
IN2_ON,
IN3_ON,
IN3_OFF,
IN4_ON,
COMPLETE // New state to track completion of the sequence
};
void resetAll() {
// Reset all relays and states
sequenceStarted = false;
sequenceState = IDLE;
isStartPending = false;
void setup() {
pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);
pinMode(IN3_PIN, OUTPUT);
pinMode(IN4_PIN, OUTPUT);
pinMode(SIG_PIN, INPUT_PULLUP); // Set SIG_PIN as input with pull-up resistor
void loop() {
unsigned long currentMillis = millis();
// Start the sequence if signal has been LOW for more than 5 seconds
if (isStartPending && (currentMillis - signalLowMillis >= debounceDelay)) {
isStartPending = false;
sequenceStarted = true;
sequenceStartMillis = currentMillis;
sequenceState = IN1_ON; // Start the sequence
}
case IN1_OFF:
if (currentMillis - previousMillis >= 5000) { // Wait 5 seconds
digitalWrite(IN1_PIN, HIGH); // Turn OFF IN1
sequenceState = IN2_ON;
}
break;
case IN2_ON:
digitalWrite(IN2_PIN, LOW); // Turn ON IN2
sequenceState = IN3_ON;
previousMillis = currentMillis;
break;
case IN3_ON:
if (currentMillis - previousMillis >= 0) {
digitalWrite(IN3_PIN, LOW); // Turn ON IN3
previousMillis = currentMillis;
sequenceState = IN3_OFF;
}
break;
case IN3_OFF:
if (currentMillis - previousMillis >= 5000) { // Wait 5 seconds
digitalWrite(IN3_PIN, HIGH); // Turn OFF IN3
previousMillis = currentMillis;
sequenceState = IN4_ON;
}
break;
case IN4_ON:
if (currentMillis - previousMillis >= 10000) { // Wait 10 seconds
digitalWrite(IN4_PIN, LOW); // Turn ON IN4
sequenceState = COMPLETE; // Mark the sequence as complete
}
break;
case COMPLETE:
// The sequence has finished. Stay here until a new signal starts.
resetAll(); // After completion, reset everything for the next cycle.
break;
default:
break;
}
}
}