const int IN1_PIN = 5; // Pin for OUTPUT1 (Digital pin 5)
const int IN2_PIN = 4; // Pin for OUTPUT2 (Digital pin 4)
const int IN3_PIN = 3; // Pin for OUTPUT3 (Digital pin 3)
const int IN4_PIN = 2; // Pin for OUTPUT4 (Digital pin 2)
const int SIG_PIN = 7; // Define signal pin
bool sigPinState = false; // State of SIG_PIN
bool sequenceStarted = false; // State to track if the sequence has started
bool isStartPending = false; // To check if we are waiting for the 5-second
condition to start
unsigned long previousMillis = 0;
unsigned long sequenceStartMillis = 0;
unsigned long signalLowMillis = 0; // Time when signal first went low
const unsigned long debounceDelay = 5000; // 5 seconds debounce delay
enum SequenceState {
IDLE,
IN1_ON,
IN1_OFF,
IN2_ON,
IN3_ON,
IN3_OFF,
IN4_ON,
COMPLETE // New state to track completion of the sequence
};
SequenceState sequenceState = IDLE;
void resetAll() {
// Reset all relays and states
sequenceStarted = false;
sequenceState = IDLE;
isStartPending = false;
digitalWrite(IN1_PIN, HIGH); // Turn off all relays
digitalWrite(IN2_PIN, HIGH);
digitalWrite(IN3_PIN, HIGH);
digitalWrite(IN4_PIN, HIGH);
}
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
resetAll(); // Ensure all relays are off at the start
}
void loop() {
unsigned long currentMillis = millis();
// Read the current signal state
sigPinState = (digitalRead(SIG_PIN) == LOW); // Check if SIG_PIN is LOW
// Handle signal going LOW (request to start sequence)
if (sigPinState && !sequenceStarted && !isStartPending) {
isStartPending = true;
signalLowMillis = currentMillis; // Record the time when signal first goes LOW
}
// 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
}
// Handle the relay sequence when started
if (sequenceStarted) {
switch (sequenceState) {
case IN1_ON:
if (currentMillis - sequenceStartMillis >= 0) {
digitalWrite(IN1_PIN, LOW); // Turn ON IN1
previousMillis = currentMillis;
sequenceState = IN1_OFF;
}
break;
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;
}
}
}