const int relayPins[] = {2, 3, 4, 5, 6}; // Pins where the relays are connected
const int buttonPin = 7; // Pin where the button is connected
bool buttonState = LOW; // Current state of the button
bool lastButtonState = LOW; // Previous state of the button
unsigned long lastDebounceTime = 0; // Last time the button state was toggled
unsigned long debounceDelay = 50; // Debounce time in milliseconds
void setup() {
// Initialize the relay pins as outputs
for (int i = 0; i < 5; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW); // Ensure relays are off initially
}
// Initialize the button pin as an input with an internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int reading = digitalRead(buttonPin);
// Check if the button state has changed
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
// If the button state has been stable for the debounce delay, toggle the button state
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
}
}
// If the button is pressed, blink the relays
if (buttonState == LOW) { // Since using INPUT_PULLUP, LOW means the button is pressed
for (int i = 0; i < 5; i++) {
digitalWrite(relayPins[i], HIGH); // Turn the relay on
}
delay(500); // Wait for 500 milliseconds
for (int i = 0; i < 5; i++) {
digitalWrite(relayPins[i], LOW); // Turn the relay off
}
delay(500); // Wait for 500 milliseconds
} else {
// Ensure all relays are turned off when the button is not pressed
for (int i = 0; i < 5; i++) {
digitalWrite(relayPins[i], LOW);
}
}
lastButtonState = reading; // Save the current button state
}