Problem with Millis

Hello. This is my first post here so dont judge me and help me out if I did anything wrong. :blush:
I am working on a drag racing light system and currently I am making little segments (programs) individualy and later I will put them all together to work. While working on one segment I am experiencing a problem with millis. This is actually my first time working with millis, and I did good on basic examples of millis function, but now it seems like Im stuck.
I want the first LED to light up, then after 500ms it should go LOW and led2 goes HIGH and so on. When I run the code LED 1 lights up, after 500ms LED2 goes HIGH but first one doesnt go LOW...
I guess I dont know how to use millis properly so if anyone could help me out it would be so awesome. Here is the void loop which doesnt work.

void loop() {
    unsigned long currentMillis = millis();
    digitalWrite(led1,HIGH);
    if(currentMillis>=500){ digitalWrite(led1,LOW);    digitalWrite(led2,HIGH);}
    if(currentMillis>=1000){ digitalWrite(led2,LOW);    digitalWrite(led3,HIGH);}  
}

Hello,

try if(millis() - currentMillis>=500){

Dieter

after 500ms LED2 goes HIGH but first one doesnt go LOW...

I think it does go low.... But, every time you start the loop you write high again. :wink:

Try this:

digitalWrite(led1,HIGH);   //  Write le1 high once before starting loop.

void loop() {
    unsigned long currentMillis = millis();


    if(currentMillis>=500){ digitalWrite(led1,LOW);    digitalWrite(led2,HIGH);}
 
    if(currentMillis>=1000){ digitalWrite(led2,LOW);    digitalWrite(led3,HIGH);}  
}[/quote]

Use a static variable to remember currentMillis.

void loop() {
    static unsigned long currentMillis = 0;

    if(currentMillis == 0)
    {
      digitalWrite(led1,HIGH);
      currentMillis = millis();
    }
    if(millis() - currentMillis>=500){ digitalWrite(led1,LOW);    digitalWrite(led2,HIGH);}
    if(millis() - currentMillis>=1000){ digitalWrite(led2,LOW);    digitalWrite(led3,HIGH);} 
}

Next can can implement a button reading in the loop. If the button is pressed, set currentMillis to 0 and the sequence starts again.

void loop() {
    unsigned long currentMillis = millis();
    digitalWrite(led1,HIGH);
    if(currentMillis>=500){ digitalWrite(led1,LOW);    digitalWrite(led2,HIGH);}
    if(currentMillis>=1000){ digitalWrite(led2,LOW);    digitalWrite(led3,HIGH);}  
}

millis() returns the time in milli seconds since the sketch was started, and resets ~49 days.
The two if statements will be true at 1/2 and 1 second after the sketch has started. >=
You should have one line for each statement.
These will not be true when 49 days has gone by when millis resets then things repeat as before.
The time between the two digitalWrites is very very short, micro seconds, therefore you will not see the flash.
Is this what you wanted?

Everything you need to know about millis

Hello. This is my first post here so dont judge me and help me out if I did anything wrong. :blush:

You need a thicker skin, don't read replies that you don't like.

Should "led3" be "led1" ?

Also, I think you are reloading currentMillis all the time -- it'll never get to 500.

Have a look at how millis() is used in Several Things at a Time

Reply #1 has nearly got it right.

...R

cerebellum:
Hello,

try if(millis() - currentMillis>=500){

Dieter

Isn't going to work, because only a couple of hundred nanoseconds will have elapsed since currentMillis was updated.

 if(currentMillis==1500)

Maybe it only works for a second and a half.
Maybe the value returned by millis () never is 1500.
Please post your code.

gabz:
So I have a problem with millis function.

Did you study the link in Reply #8 ?

...R