blinking led on pin 13 with millis() problem

Can someone explain why this coed does not work.
Thanks.

unsigned long lastTime=0 ;
void setup(){
 pinMode(13,OUTPUT); 
}

void loop(){


unsigned long currentTime = millis();
   if (currentTime - lastTime >= 1000){
    lastTime = currentTime;
    digitalWrite(13,HIGH);}
      else{
        digitalWrite(13,LOW);}
      
    }

Wow, that formatting! A } should go on a blank matching line. After reformating

unsigned long lastTime=0 ;


void setup(){
 pinMode(13,OUTPUT);
}

void loop(){


  unsigned long currentTime = millis();
  if (currentTime - lastTime >= 1000){
    lastTime = currentTime;
    digitalWrite(13,HIGH);
  }
  else{
    digitalWrite(13,LOW);
  }   
}

But it works perfectly fine :wink: It does exactly what you tell it to. But problem i, that's probably not what you want. Because when time is over

if (currentTime - lastTime >= 1000){

Turns on the output just fine.

But next time the loop runs the statement isn't true anymore and then you turn off the output. And the loop runs very very fast so you don't even see it!

How long are you expecting the LED to stay on ?
Put another way, how long will currentTime - lastTime be greater than 1000 ?

I think the problem is that LED is on for such a short time that you can't see it.

Have a look at how the LEDs are blinked in Several Things at a Time

...R