// Define the pins for the 7 LEDs and the button
#define LED_COUNT 7
int ledPins[LED_COUNT] = {2, 4, 5, 18, 19, 21, 22};
#define BUTTON_PIN 15
// Variable to track button state
bool buttonPressed = false;
void setup() {
// Initialize the LED pins as OUTPUT
for (int i = 0; i < LED_COUNT; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Ensure LEDs are off initially
}
// Initialize the button pin as INPUT with pull-up resistor
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Attach an interrupt to the button pin
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), onButtonPress, FALLING);
}
void loop() {
// Blink LEDs one by one
for (int i = 0; i < LED_COUNT; i++) {
digitalWrite(ledPins[i], HIGH); // Turn LED on
delay(600); // Wait
digitalWrite(ledPins[i], LOW); // Turn LED off
delay(600); // Wait
// Check if the button is pressed
if (buttonPressed) {
handleButtonPress();
return;
}
}
// Blink all LEDs together
for (int i = 0; i < 3; i++) { // Blink 3 times for demonstration
for (int j = 0; j < LED_COUNT; j++) {
digitalWrite(ledPins[j], HIGH);
}
delay(1000);
for (int j = 0; j < LED_COUNT; j++) {
digitalWrite(ledPins[j], LOW);
}
delay(1000);
// Check if the button is pressed
if (buttonPressed) {
handleButtonPress();
return;
}
}
}
void onButtonPress() {
buttonPressed = true;
}
void handleButtonPress() {
// Turn off all LEDs
for (int i = 0; i < LED_COUNT; i++) {
digitalWrite(ledPins[i], LOW);
}
// Reset the button state
buttonPressed = false;
// Wait for the button to be released to avoid multiple triggers
while (digitalRead(BUTTON_PIN) == LOW);
}