// Define the pins connected to the solenoid relays
const int solenoidPins[] = {7, 8, 9, 10, 11, 12};
const int numSolenoids = 6;
// Define the pin for the control switch
const int controlSwitchPin = A4;
// Define the timing parameters in milliseconds
const long onTime = 5000; // 5 seconds
const long offTime = 10000; // 10 seconds
void setup() {
// Set all solenoid pins as OUTPUTs
for (int i = 0; i < numSolenoids; i++) {
pinMode(solenoidPins[i], OUTPUT);
digitalWrite(solenoidPins[i], LOW); // Ensure all solenoids are initially off
}
// Set the switch pin as an INPUT with the internal pull-up resistor
pinMode(controlSwitchPin, INPUT_PULLUP);
}
void loop() {
// Read the state of the switch. The INPUT_PULLUP configuration means it's
// HIGH when the switch is open and LOW when it's closed (connected to ground).
if (digitalRead(controlSwitchPin) == HIGH) {
// If the switch is HIGH, run the solenoid control sequence
for (int i = 0; i < numSolenoids; i++) {
digitalWrite(solenoidPins[i], HIGH);
delay(onTime);
digitalWrite(solenoidPins[i], LOW);
// Check the switch state again before the next delay to ensure
// an immediate stop if the switch is toggled during the on-time
if (digitalRead(controlSwitchPin) == LOW) {
break; // Exit the for loop immediately
}
delay(offTime);
// Check the switch state one more time to handle a toggle during the off-time
if (digitalRead(controlSwitchPin) == LOW) {
break; // Exit the for loop
}
}
} else {
// If the switch is LOW, turn off all solenoids and do nothing
for (int i = 0; i < numSolenoids; i++) {
digitalWrite(solenoidPins[i], LOW);
}
}
}