LEDs control with button

Hi everybody
I have this very simple code
I am trying to control the LEDs 1 2 3 4 with the button
But I'm having some problems finding a solution
All I want is when the button is pressed I want the LEDs to flash gradually according to the selected time and I am still holding the button, when I take my finger off the button I want the process to stop immediately
for example
The code shows the following process:
When you press the button, LED 1 lights up, and after a second, LED 2 lights up, and after another second, LED 3 lights up, then LED 4 lights up, and in the end, all LEDs are extinguished.
This process occurs immediately after pressing the button
I want this process:
When the button is pressed without removing the finger from it, the LED 1 lights up, then after a second the LED 2 lights up. When the finger is lifted from the button, everything stops, or when you continue to press the button, the process occurs without stopping until all the LEDs are turned on and then all of them turn off.

int Button=2;
int LED1=3;
int LED2=4;
int LED3=5;
int LED4=6;
void setup() {
pinMode(Button, INPUT_PULLUP); 
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);  
}
void loop() {
     if (digitalRead(Button) == LOW){
     digitalWrite(LED1, HIGH);
     delay(1000);
     digitalWrite(LED2, HIGH);
     delay(1000);
     digitalWrite(LED3, HIGH);
     delay(1000);
     digitalWrite(LED4, HIGH);
     delay(3000);
     digitalWrite(LED1, LOW);
     digitalWrite(LED2, LOW);
     digitalWrite(LED3, LOW);
     digitalWrite(LED4, LOW);
     }
     }

Have you looked into using millis()?

Did you take a look at File|Examples|02.Digitsl|BlinkWithoutDelay?

Did you happen to search the site on how to do multiple things at the same time?

You cannot use long delays for this. While the delay() function is executing, the code is not reading the button and will not notice of it has been released.

I suggest you should not use delay() for more than 100ms.

Is it a school assignment?

In fact, I am a beginner in this field and do not know all the terminology of Arduino

Hello
It seems to be a school assigment, isn´t it?
Take some time and search in the forum.
Last month we have had a quite similar request.
grafik

Have a nice day and enjoy programming in C++ and learning.
Дайте миру шанс!

I've thrown out a few terms that you can do some research on.

Also you may find this thing,


to be useful.

Oh almost forgot, are you using an ESP32? then use the built in OS freeRTOS for multitasking.

Yes, it is a school assignment
I don't want the code to be too complicated.. I want it to be simple and easy to memorize if possible

Try this simple example as one of (n+1) possibele solutions.

constexpr byte ButtonPin{2};
constexpr byte LedPins[] {3,4,5,6};
constexpr unsigned int sizeofLedPins {sizeof(LedPins)};
unsigned long previousMillis {0};
constexpr unsigned long interval {1000};
void setup() {
  pinMode(ButtonPin, INPUT_PULLUP);
  for (auto LedPin : LedPins) pinMode(LedPin, OUTPUT);
}
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval && !digitalRead(ButtonPin)) {
    previousMillis = currentMillis;
    static int pin = 0;
    if (pin != sizeofLedPins) {
      digitalWrite (LedPins[pin++], HIGH);
    } else {
      for (auto LedPin : LedPins) digitalWrite (LedPin, LOW);
      pin = 0;
    }
  }
}

Have a nice day and enjoy programming in C++ and learning.
Errors and omissions excepted.
Дайте миру шанс!

That would explain it. :grin:

Always the question - do you propose to learn anything?

That involves careful consideration of the problem.

You can actually do it without using the "millis()" approach; you just need to check the button on a regular basis, say every second.

So take your first code - it starts by checking the button. OK, if the button is pressed, turn the first LED on and delay one second.

Now check the button again. If the button is pressed, you go on to the next LED. Then check the button again. Continue this until you get to the three second part. Break this up into three button checks and one second delays. This means that it will always respond to the button within one second which while not "instantly" will be visually effective.

You will end up with several nested "if" statements, each only being entered if the test was passed from the previous one. The statements to turn off the LEDs will be outside the "if" statements.

You need to use "auto format" - Ctrl-T - in the IDE so the loops appear correctly nested. I see you did post the code correctly otherwise. :+1:

Thank you everyone for your cooperation and solidarity in an effort to provide assistance.. In any case, the benefit will prevail

Yes, exactly.. This is exactly what I want. I do not want to use the "millis()" because my students are very novices and do not formulate complex code.
Unfortunately, their teacher is also a beginner in the world of Arduino, but ignorance and seeking knowledge is not a defect

In fact, I could not formulate my question correctly, because I did not understand much in Arduino terms, but after searching and looking at some of the codes, I discovered that all I wanted was to stop the void loop as soon as the finger was lifted from the button, and if the button was kept pressed, the void loop will complete

What do you mean “stop the void loop”?

"my students"

OK, so you are saying you are the teacher? :face_with_raised_eyebrow:

So let us see your present approach to the problem (i.e., your current code). :thinking:

Otherwise I may be forced to write it for you. :grin:

I am a general science teacher. As for the Arduino lessons, I volunteer only for my students in their spare time. I am still learning about programming.
That's it :grin:

I mean, everything after the void loop working, meaning the Arduino doesn't do anything

If you can help, I will be grateful to you and at the same time gain knowledge

Maybe you meant to say, I want to do things the proper way.

:thinking:

Another term for you:

State Machine

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