LED SEQUENCE USING MILLIS TRIGGERED BY A BUTTON (NO DELAYS)

Hi there I want to use a very simple circuit just 4 leds and a button,
so the thing is that i want to turn them on and of in a sequence using millis() function
but i don´t know how to do it, the idea goes like so:

if (button_pressed)
{
1: (led_one, HIGH);
2: 200 milliseconds later (led_one, LOW) and (led_two, HIGH);
3: 200 milliseconds later (led_two, LOW) and (led_three, HIGH);
4: 200 milliseconds later (led_three, LOW) and (led_four, HIGH);
5: 200 milliseconds later (led_four,LOW);
}

I only want to initiate the led sequence if i press the button
so if i don´t press the button nothing happens.
Also i would like to use the pwm capabilities so i could adjust the brightness of each led instead of "HIGH or LOW" so it would be something like this:

if (button_pressed)
{
1: (led_one, 255);
2: 200 milliseconds later analogWrite(led_one, 0); and analogWrite(led_two, 255);
3: 200 milliseconds later analogWrite(led_two, 0); and analogWrite(led_three, 255);
4: 200 milliseconds later analogWrite(led_three, 0); and analogWrite(led_four, 255);
5: 200 milliseconds later analogWrite(led_four, 0);

the following code works just fine to sequence the leds in a infinite loop, but i cant make it work with the button
(i know is not the most elegant solution, so i´m open to other ways to do the same thing)

int one=11;
int two=10;
int three=9;
int four=6;
unsigned long previousMillis=0;
const long interval=200;
void setup(){///////////////////////////////////////////////////////////////////////
Serial.begin(9600);
pinMode(one,OUTPUT);
pinMode(two,OUTPUT);
pinMode(three,OUTPUT);
pinMode(four,OUTPUT);
}
void loop(){////////////////////////////////////////////////////////////////////////
unsigned long currentMillis=millis();

if((currentMillis-previousMillis>=interval)&&(currentMillis-previousMillis<=interval*2)){
analogWrite(one,255);
Serial.println("one");}

if((currentMillis-previousMillis>=interval*2)&&(currentMillis-previousMillis<=interval*3)){
analogWrite(one,0);
analogWrite(two,255);
Serial.println("two");}

if((currentMillis-previousMillis>=interval*3)&&(currentMillis-previousMillis<=interval*4)){
analogWrite(two,0);
analogWrite(three,255);
Serial.println("three");}

if((currentMillis-previousMillis>=interval*4)&&(currentMillis-previousMillis<=interval*5)){
analogWrite(three,0);
analogWrite(four,255);
Serial.println("four");}

if((currentMillis-previousMillis>=interval*5)&&(currentMillis-previousMillis<=interval*6)){
analogWrite(four,0);
previousMillis = currentMillis;}}

LED_SEQUENCE_WITHOUT_DELAY.ino (1.19 KB)

Sadly, my ipad can’t open ino files.
If you follow the forum guidelines many people might be more inclined to help.

It’s good that you have made a start and posted it, but no good if we can’t see it.
good luck.

Sorry i didn´t know how to publish the code, it is my first post here in the forum.
But i just added the code ^^

Great...
You’re heading in the right direction, but at first glance, your long if() statements are getting a bit clunky.
There’s nothing wrong with that, but it will become hard to read & maintain next week!

Have a look at state machines
For example...
You’ll initially be at state 0: IDLE
then, when you press the button, you’ll bump to state 1, and the millis() event can then tick through the remaining states, finally running through back to IDLE.
A switch-case structure can implement the 1-2-3 states, and fall back at the end.

It sounds tricky, but after a day of tinkering, you’ll find it very easy to implement, modify and maintain. And a new skill learned.