LED on and off

Does anyone know how to make the led turn on and off repeatedly without using delay?

OP’s image.

There is an example ‘blink without delay’ under the IDE examples.

BTW, you need a series resistor with the LED ~220Ω

modapro1:
Does anyone know how to make the led turn on and off repeatedly without using delay?

Try this:

const int pinLED = 13;

//unsigned longs will hold the value of the
//system millisecond counter "now" and "then"
unsigned long
    timeThen,
    timeNow;
    
//this "boolean" -- true or false -- is used
//to remember the state of the LED
bool
    bLEDOnorOff;
    
void setup( void )
{
    //init the pin and set it low (LED off)
    pinMode( pinLED, OUTPUT );
    digitalWrite( pinLED, LOW );
    //set the flag to 0 ("off") to match the LED
    bLEDOnorOff = 0;

    //get the current value of the millisecond
    //counter and save it as "then"
    timeThen = millis();
    
}//setup

void loop( void )
{
    //each time through loop we check the "now" value
    //of the mS counter against the "then" time. As the
    //mS pass, this difference will increase. When it
    //reaches 250 (in this case) -- which is 250mS -- the
    //compare is "true" so we go into...
    timeNow = millis();
    if( (timeNow - timeThen) >= 250 )
    {
        //...here. For the next compare, in 250mS, 
        //we want to save the "now" time as "then"
        timeThen = timeNow;

        //this is an exclusive or binary operation
        //is basically flips the first bit in 
        //out flag bLEDOnorOff. When it's '0' we
        //turn the LED off and when it's '1' we turn it
        //on
        bLEDOnorOff ^= 1;
        if( bLEDOnorOff == 0 )
            digitalWrite( pinLED, LOW );
        else
            digitalWrite( pinLED, HIGH );
            
    }//if
    
}//loop

This one is quit complex for a newbie

bLEDOnorOff ^= 1;

You can simplify it to

bLEDOnorOff = 1 - bLEDOnorOff;

BTW, 'ms' is for milliseconds and 'mS" is for milliSiemens (the reciprocal of a resistance) :slight_smile:

lesept:
This one is quit complex for a newbie

bLEDOnorOff ^= 1;

That's Blackfin's preferred and stated way of doing things: s/he shows off with snazzy code, then waits for the helpee to ask wtf it means.

Why anyone would suggest doing what the OP wants without just pointing to good ol' BlinkWithOutDelay, is totally beyond me.

larryd:
BTW, you need a series resistor with the LED ~220Ω

OP might have one of these though.

There’s 5 lines of comment above the simple exclusive OR statement. Five. Lines. OP learns how to make an LED blink without using delay and learns a new concept, all in one post.

Win-win.

Blackfin:
There's 5 lines of comment above the simple exclusive OR statement. Five. Lines. OP learns how to make an LED blink without using delay and learns a new concept, all in one post.

Win-win.

If you want to teach someone something, you do not post a working cut-'n-paste sollution but instead some inspiring pseudo-code :slight_smile: