const int buttonPins[] = {2, 3, 4, 5};
const int relayPin = 8;
const int relayActivateTime = 4000; // 4 seconds
unsigned long lastActivationTime = 0;
int lastPressedButton = -1;
void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
for (int i = 0; i < 4; ++i) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
}
void loop() {
for (int i = 0; i < 4; ++i) {
if (digitalRead(buttonPins[i]) == LOW) {
// Button is pressed
if (i != lastPressedButton) {
Serial.print("Button ");
Serial.println(i);
// Button is different from the last one pressed
activateRelay();
lastPressedButton = i;
}
}
}
}
void activateRelay() {
digitalWrite(relayPin, HIGH);
delay(relayActivateTime);
digitalWrite(relayPin, LOW);
lastActivationTime = millis();
lastPressedButton = -1;
}