#define LED1 2 #define LED2 3 #define LED3 4 #define LED4 5 #define LED5 6 #define LED6 7 #define LED7 8 #define LED8 9 const int ledPins[] = {LED1, LED2, LED3, LED4, LED5, LED6, LED7, LED8}; const int buttonPin = 10; bool ledState = false; bool buttonPressed = false; unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 50; int currentLed = 0; int direction = 1; // 1 = forward, -1 = backward unsigned long previousMillis = 0; const unsigned long interval = 300; // 300ms for each blink void setup() { Serial.begin(115200); Serial.println("System Ready. Waiting for button press..."); for (int i = 0; i < 8; i++) { pinMode(ledPins[i], OUTPUT); digitalWrite(ledPins[i], LOW); } pinMode(buttonPin, INPUT_PULLUP); } void loop() { static bool lastButtonReading = HIGH; bool currentReading = digitalRead(buttonPin); if (currentReading != lastButtonReading) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { if (currentReading == LOW && !buttonPressed) { buttonPressed = true; ledState = !ledState; if (ledState) { Serial.println("Button Pressed: Starting blinking sequence..."); currentLed = 0; direction = 1; } else { Serial.println("Button Pressed: Stopping and turning OFF all LEDs..."); for (int i = 0; i < 8; i++) { digitalWrite(ledPins[i], LOW); } } } if (currentReading == HIGH) { buttonPressed = false; } } lastButtonReading = currentReading; if (ledState) { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // Turn OFF all LEDs first for (int i = 0; i < 8; i++) { digitalWrite(ledPins[i], LOW); } // Turn ON the current LED digitalWrite(ledPins[currentLed], HIGH); Serial.print("Blinking LED "); Serial.println(currentLed + 1); // Move to next LED currentLed += direction; // If we reach the end, reverse direction if (currentLed >= 8) { currentLed = 7; // Stay at last LED direction = -1; // Reverse } else if (currentLed < 0) { currentLed = 0; // Stay at first LED direction = 1; // Forward again } } } }