const int buttonPin = 4; // Pin for the button
const int ledPins[] = {12, 11, 10, 9, 8, 7, 6, 5}; // Pins for the LEDs
const int numLeds = 8; // Number of LEDs
int buttonState = 0; // Variable for reading the button status
void setup() {
// Initialize the LED pins as outputs
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize the button pin as input
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// If the button is pressed
if (buttonState == HIGH) {
// Turn on each LED one by one with a delay
for (int i = 0; i < numLeds; i++) {
if (digitalRead(buttonPin) == LOW) {
// If the button is released, turn off all LEDs and break the loop
for (int j = 0; j < numLeds; j++) {
digitalWrite(ledPins[j], LOW);
}
break;
}
digitalWrite(ledPins[i], HIGH);
delay(200); // Delay for 200 milliseconds
}
} else {
// If the button is not pressed, turn off all LEDs
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
}
}