Please help for my child's school project

how to turn off the leds one by one with one push button and also use the millis code for 5 seconds. I am very confused.

[code]
int led_1 = 2;
int led_2 = 3;
int led_3 = 4;
int led_4 = 5;
int led_5 = 6;
int led_6 = 7;
int led_7 = 8;
int led_8 = 9;
int led_9 = 10;
int led_10 = 11;

const int buttonPin = A0;
int programState = 0;
int buttonState;
long buttonMillis = 0;
const long intervalButton = 3000;   
long ledMillis = 0;
const long intervalLed = 5000;   

void setup() {
  pinMode(led_1, OUTPUT);
  pinMode(led_2, OUTPUT);
  pinMode(led_3, OUTPUT);
  pinMode(led_4, OUTPUT);
  pinMode(led_5, OUTPUT);
  pinMode(led_6, OUTPUT);
  pinMode(led_7, OUTPUT);
  pinMode(led_8, OUTPUT);
  pinMode(led_9, OUTPUT);
  pinMode(led_10, OUTPUT);
  
  pinMode(buttonPin, INPUT);      
  
}

void loop()
{
  unsigned long currentMillis = millis();
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == LOW && programState == 0) {
    buttonMillis = currentMillis;
    programState = 1;
  }
  else if (programState == 1 && buttonState == HIGH) {
        programState = 0; //reset
  }
  if(currentMillis - buttonMillis > intervalButton && programState == 1) {
    programState = 1;
    ledMillis = currentMillis;

    digitalWrite(ledPin, HIGH);
  }

  if(currentMillis - ledMillis > intervalLed && programState == 2) {
    programState = 0;

    digitalWrite(ledPin, LOW);
  }
}
[/code]

Welcome to the forum

Start with the basics

You need to detect when the button becomes pressed rather than when it is pressed. See the StateChangeDetection example in the IDE

That will give you a count of the number of button presses

Put the LED pin numbers in an array and use the number of button presses as the index to the array so that you know which LED to turn off

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.