Why is the LED not blinking?

Hello All,

Every "Pause without delay" tutorial uses LED state variable to toggle the LED, so I'm wondering what's wrong with my approach and why it's not working? :confused:

unsigned long timer1 = 0;
unsigned long timer2 = 0;
const long interval = 1000;

void setup() 
{
}


void loop()
{

  timer1 = millis();
  digitalWrite(13,HIGH);
  if (millis() - timer1 >= interval) digitalWrite(13,LOW);
  
  timer2 = millis();
  digitalWrite(13,LOW);
  if (millis() - timer2 >= interval) digitalWrite(13,LOW);

}

I don't really wish to use toggling method. Help?

  if (millis() - timer1 >= interval)

How likely is it that 1000 milliseconds will have passed since you copied the value of millis() to timer1 ?

It would also be helpful if pin 13 were set as an output.

Have you looked at Using millis() for timing. A beginners guide

UKHeliBob:
How likely is it that 1000 milliseconds will have passed since you copied the value of millis() to timer1 ?

It would also be helpful if pin 13 were set as an output.

Have you looked at Using millis() for timing. A beginners guide

It's very likely because the first thing the board does is light the LED -and never switches it off-.

I did read the pinned post about millis() but I'm wondering why my code isn't alternating states? It makes perfect sense to me when I try to read it in plain English :slight_smile:

And I did assign a pinMode in the setup().

Every time around loop() (and that's a lot of times every second), you turn the LED on.
Occasionally, you turn it off, then a few microseconds later, on again.

The LED is blinking, it's just that human eyes don't work well on microsecond timescales - you should see it with an oscilloscope though.

I did assign a pinMode in the setup().

Not in the code that you posted, which raises the possibility that the rest of the program is different as well.

Please check the code that you are testing and post it here in a new post.

Will do, and I'll perform more experiments and come back later.

Thanks for now,

Study this simple "winker", is turning something ON then OFF repeatedly not called toggling?
Change "pulse" and "interval" to suit.

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

void loop()
{
  static unsigned long tStart = 0;
  unsigned int pulse = 50,        //  ON time = 50 mS
              interval = 1000; // OFF =  interval - pulse = 950 mS
  digitalWrite(LED_BUILTIN,millis() - tStart < pulse);
  if(millis() - tStart > interval)
   tStart += interval;
}