const int relayPin = 10; // Connect the IN pin of the relay module to D9
const int switchPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // Connect switches to digital pins D2 to D9
const int numSwitches = sizeof(switchPins) / sizeof(switchPins[0]);
int numSwitchesToActivate = 4; // Set the number of switches you want to activate
bool relayNormallyOpen = true; // Set to true for NO relay, false for NC relay
unsigned long relayOnTime = 5000; // 5 seconds
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, relayNormallyOpen ? LOW : HIGH);
for (int i = 0; i < numSwitches; i++) {
pinMode(switchPins[i], INPUT_PULLUP);
}
}
void loop() {
int numActivatedSwitches = 0;
for (int i = 0; i < numSwitches; i++) {
if (digitalRead(switchPins[i]) == LOW) {
numActivatedSwitches++;
}
}
if (numActivatedSwitches == numSwitchesToActivate) {
activateRelay();
}
}
void activateRelay() {
digitalWrite(relayPin, relayNormallyOpen ? HIGH : LOW);
delay(relayOnTime);
digitalWrite(relayPin, relayNormallyOpen ? LOW : HIGH);
}