Turn on led in sequence while holding a button

Guys, how are you?

I have a question in a code using the delay ().

The operation is correct until I put delay () between the lighting of each led.

See the code

const int led1 = 8; // define LED
const int led2 = 9; // define LED
const int led3 = 10; // define LED
const int led4 = 11; // define LED
const int led5 = 12; // define LED
const int button = 2; // define button

int buttonState = 0; // define button = 0

void setup() {
  // put your setup code here, to run once:
  pinMode (led1, OUTPUT); // define led1 
  pinMode (led2, OUTPUT); // define led2 
  pinMode (led3, OUTPUT); // define led3 
  pinMode (led4, OUTPUT); // define led4 
  pinMode (led5, OUTPUT); // define led5 
  pinMode (button, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:

  buttonState = digitalRead (button);

  if (buttonState == LOW) { // if the state of the button is pressed
    digitalWrite (led1, !buttonState); // lights up led1
    digitalWrite (led2, !buttonState); // lights up led2
    digitalWrite (led3, !buttonState); // lights up led3
    digitalWrite (led4, !buttonState); // lights up led4
    digitalWrite (led5, !buttonState); // lights up led5
  
   } else { // if not, turn off all LEDs
    
    digitalWrite (led1, LOW);
    digitalWrite (led2, LOW);
    digitalWrite (led3, LOW);
    digitalWrite (led4, LOW);
    digitalWrite (led5, LOW);
  }
}

If I put delay () between each digitalWrite by turning on the LED, even if I release the button after turning on the first LED, it does not go to the else function to turn off all the LEDs.

Can someone help me?

The idea is that when I press the button, I light 1 led at a time with a pause of 1 second and immediately when I release the button, all the LEDs go out.

You need to understand that if you put a delay() in the loop, the loop will stop at that point and absolutely nothing happens for the duration of the delay. That is what "delay" means. :grinning:

Clearly while nothing is happening, it can not get back to the part of the loop where you check the button, can it?

Study this discussion.

I imagine I need to use millis () but I'm not able to make it work

“. . . but I'm not able to make it work”

Show us your attempt at using millis()

I 'm a newbie and I tried it this way and in other ways, but I can't get it to work

I think that to give the time interval for each LED I need to use millis() after each digitalWrite()

const int led1 = 8; // define LED
const int led2 = 9; // define LED
const int led3 = 10; // define LED
const int led4 = 11; // define LED
const int led5 = 12; // define LED
const int button = 2; // define button

int buttonState = HIGH; // define button = 0

unsigned long startMillis; // define start time
unsigned long currentMillis; // define actual time

const unsigned long period = 1000; // time for "delay"

void setup() {

 startMillis = millis(); // start counting the time

 // put your setup code here, to run once:
 pinMode (led1, OUTPUT); // define led1
 pinMode (led2, OUTPUT); // define led2
 pinMode (led3, OUTPUT); // define led3
 pinMode (led4, OUTPUT); // define led4
 pinMode (led5, OUTPUT); // define led5
 pinMode (button, INPUT_PULLUP);
}

void loop() {
 // put your main code here, to run repeatedly:

 buttonState = digitalRead (button);

 if (currentMillis - startMillis >= period) // test time
 {
   if (buttonState == LOW) { // if the state of the button is pressed
     digitalWrite (led1, !buttonState); // lights up led1
     startMillis = currentMillis;
     if (currentMillis - startMillis >= period) // test time
     {
       if (buttonState == LOW) { // if the state of the button is pressed
         digitalWrite (led2, !buttonState); // lights up led2
         startMillis = currentMillis;
         if (currentMillis - startMillis >= period) // test time
         {
           if (buttonState == LOW) { // if the state of the button is pressed
             digitalWrite (led3, !buttonState); // lights up led3
             startMillis = currentMillis;
             if (currentMillis - startMillis >= period) // test time
             {
               if (buttonState == LOW) { // if the state of the button is pressed
                 digitalWrite (led4, !buttonState); // lights up led4
                 startMillis = currentMillis;
                 if (currentMillis - startMillis >= period) // test time
                 {
                   if (buttonState == LOW) { // if the state of the button is pressed
                     digitalWrite (led5, !buttonState); // lights up led5
                     startMillis = currentMillis;

                   } else { // if not, turn off all LEDs

                     digitalWrite (led1, LOW);
                     digitalWrite (led2, LOW);
                     digitalWrite (led3, LOW);
                     digitalWrite (led4, LOW);
                     digitalWrite (led5, LOW);
                   }
                 }
               }
             }
           }
         }
       }
     }
   }
 }
}

larryd:
“. . . but I'm not able to make it work”

Show us your attempt at using millis()

Do you understand ‘everything’ in this sketch ?

Change this line as needed:
#define LEDon HIGH // Pin---->|----[220R]----GND -->|-- LED

// LEDsOnSwitchPressed.ino
//
//                YY/MM/DD
// Version 1.00   20/06/18      Running code
// 
//
//

#define    PUSHED          LOW     //+5V----INPUT_PULLUP(20k)----Input Pin----[N.O. Switch]----GND
#define    LEDon           HIGH    //Pin---->|----[220R]----GND               -->|--  LED
#define    LEDoff          !LEDon

const byte button          = 2;

const byte led1            = 8;
const byte led2            = 9;
const byte led3            = 10;
const byte led4            = 11;
const byte led5            = 12;
const byte heartbeatLED    = 13;

byte mState                = 0;    //states 0-5 are valid
byte lastState;

//timing stuff
const unsigned long period = 1000; //time for "delay"

unsigned long commonMillis;
unsigned long currentMillis;
unsigned long switchMillis;
unsigned long heartbeatMillis;

//**********************************************************************************
void setup()
{
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (led4, OUTPUT);
  pinMode (led5, OUTPUT);

  pinMode (heartbeatLED, OUTPUT);

  pinMode (button, INPUT_PULLUP);
  lastState = digitalRead(button);

} //END of setup()


//**********************************************************************************
void loop()
{
  currentMillis = millis();

  //********************************
  //to help show if there is any blocking code, this LED toggles every 500ms
  if (currentMillis - heartbeatMillis >= 500)
  {
    //start this TIMER
    heartbeatMillis = currentMillis;

    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //********************************
  checkSwitches();


  //********************************
  stateMachine();

  //********************************
  // Other none blocking code goes here
  //********************************

} //END of loop()

//**********************************************************************************
void stateMachine()
{
  switch (mState)
  {
    //*************
    case 0:
      //do nothing

      break;

    //*************
    case 1:
      digitalWrite (led1, LEDon); 

      //start the TIMER
      commonMillis = currentMillis;

      //next state
      mState = 2;

      break;

    //*************
    case 2:
      if (currentMillis - commonMillis >= period)
      {
        digitalWrite (led2, LEDon); 

        //restart the TIMER
        commonMillis = currentMillis;

        //next state
        mState = 3;
      }

      break;

    //*************
    case 3:
      if (currentMillis - commonMillis >= period)
      {
        digitalWrite (led3, LEDon); 

        //restart the TIMER
        commonMillis = currentMillis;

        //next state
        mState = 4;
      }

      break;

    //*************
    case 4:
      if (currentMillis - commonMillis >= period)
      {
        digitalWrite (led4, LEDon); 

        //restart the TIMER
        commonMillis = currentMillis;

        //next state
        mState = 5;
      }

      break;

    //*************
    case 5:
      if (currentMillis - commonMillis >= period)
      {
        digitalWrite (led5, LEDon); 
      }

      break;

  } //END switch/case

} //END of stateMachine()


//**********************************************************************************
void checkSwitches()
{
  //*************************************
  //is it time to check the switch ?
  if (currentMillis - switchMillis < 50)
  {
    //not time to read the switch
    return;
  }

  //restart the TIMER
  switchMillis = currentMillis;

  //*************************************
  //was there a change in switch state ?
  byte currentState = digitalRead(button);
  if (lastState == currentState)
  {
    //there was no switch change
    return;
  }

  //update to the current state
  lastState = currentState;

  //*************************************
  //did the switch go closed ?
  if (currentState == PUSHED)
  {
    mState = 1;
  }

  //the switch went open
  else
  {
    mState = 0;

    digitalWrite (led1, LEDoff);
    digitalWrite (led2, LEDoff);
    digitalWrite (led3, LEDoff);
    digitalWrite (led4, LEDoff);
    digitalWrite (led5, LEDoff);
  }
  
} //END of checkSwtiches()

//**********************************************************************************

W A R N I N G

"Guys, how are you?"

There are gals here too.

Sorry for guys ... I meant people!
I believe that there are many women here who are able to do many things like anyone else.

No, I don't understand the whole code.
Switch, case, break - I haven't seen in my study yet, and another parts of this sketch.

I've been studying Arduino for 16 days.

My kit has arrived 06/03 and every day after my job, I study alone with blog materials, youtube videos, forums.

But thanks for the sketch, I will study what they means.

If you need explanation, ask for help with what you do not understand.

Have fun.

Hi people, I got it ...

const int led1 = 8; // define pino led 1
const int led2 = 9; // define pino led 2
const int led3 = 10; // define pino led 3
const int led4 = 11; // define pino led 4
const int led5 = 12; // define pino led 5

const int botao = 2; // define pino botao
int estadoBotao;

unsigned long tempoAnterior;
unsigned long tempoAtual;
unsigned long momento;


void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(botao, INPUT_PULLUP);
}

void loop() {
  estadoBotao = digitalRead(botao);

  if (millis() - momento > 10) {
    if (estadoBotao == LOW) {
      digitalWrite(led1, HIGH);
    }
    if (millis() - momento > 990) {
      if (estadoBotao == LOW) {
        digitalWrite(led2, HIGH);
      }
    }
    if (millis() - momento > 1990) {
      if (estadoBotao == LOW) {
        digitalWrite(led3, HIGH);
      }
    }
    if (millis() - momento > 2990) {
      if (estadoBotao == LOW) {
        digitalWrite(led4, HIGH);
      }
    }
    if (millis() - momento > 3990) {
      if (estadoBotao == LOW) {
        digitalWrite(led5, HIGH);
      }
    }
  } if (estadoBotao == HIGH) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);

    momento = millis();
  }
}

Now I will increment some things, because project, did not over

Hi people, how are you?

I'm having trouble getting the buzzer to ring for 500 milliseconds and pause 500 milliseconds

const int led1 = 9; // define pin led 1
const int led2 = 10; // define pin led 2
const int led3 = 11; // define pin led 3
const int led4 = 12; // define pin led 4
const int led5 = 13; // define pin led 5
const int buzzer = 4; // define pin buzzer
const int timeVermelho = 5; // define red squad
const int timeVerde = 6; // define pin green squad

const int botao = 2; // define pin button 1
int estadoBotao; // variable that keeps the button state 1
const int botao2 = 3; // define pin button 2
int estadoBotao2; // variable that keeps the button state 2

unsigned long momento; // variable to receive millis from green squad
unsigned long momento2; // variable to receive millis from red squad
unsigned long momento3; // variable to receive millis from buzzer

int frequenciaAciona; // variable sound when triggering
int frequenciaFinal; // variable confirms activation

int taAceso1; // variable to store if led1 is on
int taAceso2; // variable to store if led2 is on
int taAceso3; // variable to store if led3 is on
int taAceso4; // variable to store if led4 is on
int taAceso5; // variable to store if led5 is on


void setup() {
 frequenciaAciona = 2000; // trigger tone
 frequenciaFinal = 1000; // tone confirms

 pinMode(led1, OUTPUT); // define led as output
 pinMode(led2, OUTPUT); // define led as output
 pinMode(led3, OUTPUT); // define led as output
 pinMode(led4, OUTPUT); // define led as output
 pinMode(led5, OUTPUT); // define led as output
 pinMode(timeVerde, OUTPUT); // define green squad led as output
 pinMode(timeVermelho, OUTPUT); // define red squad led as output
 pinMode(botao, INPUT_PULLUP); // sets button 1 to HIGH
 pinMode(botao2, INPUT_PULLUP); // sets button 2 to HIGH
 pinMode(buzzer, OUTPUT); // define buzzer as output
}

void loop() {

 // green button behavior
 estadoBotao = digitalRead(botao); // reads the state that button 1 is in: pressed (LOW) or released (HIGH)
 taAceso1 = digitalRead(led1);
 taAceso2 = digitalRead(led2);
 taAceso3 = digitalRead(led3);
 taAceso4 = digitalRead(led4);
 taAceso5 = digitalRead(led5);

 if ( (millis() - momento) > 0) { // if the elapsed time is greater than 0 seconds
   if (estadoBotao == LOW) { // if button 1 is pressed
     digitalWrite(led1, HIGH); // turn on the led 1

     if (taAceso1 == HIGH) { // if led1 is on, activates the buzzer
       tone(buzzer, frequenciaAciona);
       
     }
   }
 }
 if ( (millis() - momento) > 1000) { // if the elapsed time is greater than 1 seconds
   if (estadoBotao == LOW) { // if button 1 is pressed
     digitalWrite(led2, HIGH); // turn on the led 2

     if (taAceso2 == HIGH) { // if led2 is on, activates the buzzer
       tone(buzzer, frequenciaAciona);
     }
   }
 }
 if ( (millis() - momento) > 2000) { // f the elapsed time is greater than 2 seconds
   if (estadoBotao == LOW) { // if button 1 is pressed
     digitalWrite(led3, HIGH); // turn on the led 3

     if (taAceso3 == HIGH) { // if led3 is on, activates the buzzer
       tone(buzzer, frequenciaAciona);
     }
   }
 }
 if ( (millis() - momento) > 3000) { // f the elapsed time is greater than 3 seconds
   if (estadoBotao == LOW) { // if button 1 is pressed
     digitalWrite(led4, HIGH); // turn on the led 4

     if (taAceso4 == HIGH) { // if led4 is on, activates the buzzer
       tone(buzzer, frequenciaAciona);
     }
   }
 }
 if ( (millis() - momento) > 4000) { // f the elapsed time is greater than 4 seconds
   if (estadoBotao == LOW) { // if button 1 is pressed
     digitalWrite(led5, HIGH); // turn on the led 5
     digitalWrite(timeVerde, HIGH); // turn on the led green
     digitalWrite(timeVermelho, LOW); // turn off the led red

     if (taAceso5 == HIGH) { // if led5 is on, activates the buzzer 
       tone(buzzer, frequenciaAciona);        
   }
 }
} if (estadoBotao == HIGH) { // if button 1 is released
 digitalWrite(led1, LOW); // turn off led  1
 digitalWrite(led2, LOW); // turn off led  2
 digitalWrite(led3, LOW); // turn off led  3
 digitalWrite(led4, LOW); // turn off led  4
 digitalWrite(led5, LOW); // turn off led  5
 noTone(buzzer);

 momento = millis(); // updates the moment variable to the current time
}

// red button behavior
estadoBotao2 = digitalRead(botao2); // reads the state that button 2 is in: pressed (LOW) or released (HIGH)

if ( (millis() - momento2) > 0) { // if the elapsed time is greater than 0 seconds
 if (estadoBotao2 == LOW) { // if button 2 is pressed
   digitalWrite(led5, HIGH); // turn on the led 5

   if (taAceso5 == HIGH) {
       tone(buzzer, frequenciaAciona);        
   }
 }
 if ( (millis() - momento2) > 1000) { // if the elapsed time is greater than 1 seconds
   if (estadoBotao2 == LOW) { // if button 2 is pressed
     digitalWrite(led4, HIGH); // turn on the led 4

     if (taAceso4 == HIGH) {
       tone(buzzer, frequenciaAciona);
     }
   }
 }
 if ( (millis() - momento2) > 2000) { // if the elapsed time is greater than 2 seconds
   if (estadoBotao2 == LOW) { // if button 2 is pressed
     digitalWrite(led3, HIGH); // turn on the led 3

     if (taAceso3 == HIGH) {
       tone(buzzer, frequenciaAciona);
     }
   }
 }
 if ( (millis() - momento2) > 3000) { // if the elapsed time is greater than 3 seconds
   if (estadoBotao2 == LOW) { // if button 2 is pressed
     digitalWrite(led2, HIGH); // turn on the led 2

     if (taAceso2 == HIGH) {
       tone(buzzer, frequenciaAciona);
     }
   }
 }
 if ( (millis() - momento2) > 4000) { // if the elapsed time is greater than 4 seconds
   if (estadoBotao2 == LOW) { // if button 2 is pressed
     digitalWrite(led1, HIGH); // turn on the led 1
     digitalWrite(timeVermelho, HIGH); // turn on the led red
     digitalWrite(timeVerde, LOW); // turn off the led gree

     if (taAceso1 == HIGH) {
       tone(buzzer, frequenciaAciona);
     }
   }

 } if (estadoBotao2 == HIGH) { // if button 1 is released
   digitalWrite(led1, LOW); // turn off led  1
   digitalWrite(led2, LOW); // turn off led  2
   digitalWrite(led3, LOW); // turn off led  3
   digitalWrite(led4, LOW); // turn off led  4
   digitalWrite(led5, LOW); // turn off led  5

   momento2 = millis(); // updates the moment variable to the current time
 }
}
}

In this part of the code

if (taAceso1 == HIGH) { // if led1 is on, activates the buzzer
       tone(buzzer, frequenciaAciona);
       
     }

Can someone help me?

Tell us when this statement will go true:

if ( (millis() - momento2) > 0)

larryd:
Tell us when this statement will go true:

if ( (millis() - momento2) > 0)

Now that you speak, I thought.

Never.

But it works by immediately turning on led1 when I press the button.

Even changing this subtraction to >10 for example, it works normally

Have a look at -

Using millis() for timing beginner’s guide

several things at the same time