Control the blink LED time and without using delay

I want to blink the LED in a pattern. For example, on (0.5 sec)-off (0.5 sex)-on(0.5 sec)-off(1 sec) and keep repeating.

I don't want to use delay, and I've also check the example of BlinkLedWithoughtDelay. But I cannot figure out how to set the LED on or off time.

Any ideas? Thanks!!!


   if (millis() - previousMillis > interval) {
     // save the last time you blinked the LED 
     previousMillis = millis();   

     // if the LED is off turn it on and vice-versa:
     if (ledState == LOW)
       ledState = HIGH;
       ledState = LOW;

     // set the LED with the ledState of the variable:
     digitalWrite(ledPin, ledState);

Look for the above in the blink without delay sketch

the first if here is checking millis() which returns how long the sketch has been running. Minus the last time the led blinked, if that value is > interval (1 second by default in this sketch) update the previousMillis with the current time and move on...

doing timing without delay in any situation, is the same thing

hope that helps?

Thank you for replying so fast!

I'm not very good at coding, I've tried this code and it works fine. But couls you be more specific how to put timing in? It's not just blink, but blink in different timeline, like "on(0.5 sec)-off(1sec)-on(0.5sec)-off(2sec), and keep repeating this kind loop.


If you expand the part of the code that toggles the state of the LED ("if (ledState == LOW)" etc) with some braces {}, you can put in a variable delay for how long you want the LED to staty in that state.