const int NUM_LEDS = 9;
const int LED_PINS[NUM_LEDS] = {13, 12, 11, 10, 9, 8, 7, 6, 5};
const int BTN_PIN = 4;
int count = 0;
int oldBtnState = HIGH;
void checkButton() {
int btnState = digitalRead(BTN_PIN);
if (oldBtnState != btnState) {
oldBtnState = btnState;
if (btnState == LOW) {
Serial.println("Button pressed");
count++;
if (count == NUM_LEDS + 1) {
count = 0;
// turn all LEDs off
for (int i = 0; i <= NUM_LEDS; i++) {
digitalWrite(LED_PINS[i], LOW);
}
}
} else {
Serial.println("Button released");
}
delay(20); // debounce
}
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(LED_PINS[i], OUTPUT);
}
pinMode(BTN_PIN, INPUT_PULLUP);
}
void loop() {
checkButton();
if (count > 0) {
digitalWrite(LED_PINS[count - 1], HIGH);
}
}