Flashing LED with Modulo?

I recently found some code that flashed an LED using a Modulo command (I think it was Modulo?). I liked it because it was simple and didn't require a "high delay low delay" series to run.
Does anyone have an idea where to find the code?

Thanks in advance I will save it this time if I can find it again.

Did the code use delay() or millis() ?

Kiwi_Bloke:
I recently found some code that flashed an LED using a Modulo command

Er… there’s the ‘modulo’ operator: % which gives the remainder after division

So this is possible, but maybe a bit cryptic:

for( int i=0; i<50; i++ ){
 digitalWrite( LED_BUILTIN, i % 2 );
 delay( 500 );
}

Yours,
TonyWilk

UKHeliBob:
Did the code use delay() or millis() ?

I think it used delay but I will use millis when I use it in a program

TonyWilk:
Er… there’s the ‘modulo’ operator: % which gives the remainder after division

So this is possible, but maybe a bit cryptic:

for( int i=0; i<50; i++ ){

digitalWrite( LED_BUILTIN, i % 2 );
delay( 500 );
}




Yours,
TonyWilk

No it was much simpler than that but thanks, I should have saved it when I found it the first time

Here's a hint - try using the modulo operation on the value returned by the millis() function.

Kiwi_Bloke:
No it was much simpler than that but thanks, I should have saved it when I found it the first time

You have me intrigued now...

How about this:

digitalWrite( LED_BUILTIN, millis() >> 8 & 1 );

//or

digitalWrite( LED_BUILTIN, millis() % 500 > 250 );

Yours,
TonyWilk

Something like this?

void setup() {
 bitSet(DDRB,5);
}
void loop() {
 int interval = 1000, pulseLen = 30;
 bitWrite(PORTB,5,millis() % interval <= pulseLen);
}

I may be having a thicker day than normal, but where does modulo come in to blinking an led?

1: Let’s say, at this moment, millis() = 5000 (5 seconds since startup), millis() % 1000 = 0.
2: millis() % 1000 = 0 and is < 30, so LED = ON.
3: 31 mS later, millis() % 1000 = 31 and is NOT < 30, LED = OFF.
4: 969 mS later, millis() = 6000 (6 seconds since startup), back to line 2.

wilfredmedlin:
I may be having a thicker day than normal, but where does modulo come in to blinking an led?

Useful when an asymmetric blink is required

unsigned long periods[] = {1000, 5000};
byte counter;
const byte ledPin = 13;

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

void loop()
{
  delay(periods[counter % 2]);
  counter++;
  digitalWrite(ledPin, !digitalRead(ledPin));
}

Easy to extend

unsigned long periods[] = {1000, 5000, 500, 200};
byte counter;
const byte ledPin = 13;

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

void loop()
{
  delay(periods[counter % 4]);
  counter++;
  digitalWrite(ledPin, !digitalRead(ledPin));
}

UKHeliBob:
Useful when an asymmetric blink is required

Ah ok, so the remainder is the index: cunning, thanks.

Funnily enough, I implemented a blink without delay the other day with a short on, short off, short on, long off to give like a heart beat duf-duf-pause, duf-duf-pause, which I run in the background on L13 as a sign-of-life.

This will make that a lot neater.

Something like this, perhaps?

void loop() {
  if (millis() % 1000 == 0) digitalWrite(13, HIGH);  // turn on at the one-second marks
  if (millis() % 1000 == 100) digitalWrite(13, LOW); // turn off at the 1.1s marks
  //
  // other (non-delaying) code
  //
}

Note that "%" (modulus, remainder, etc) is a relatively computationally complex operation.

I hate using millis( ) for timing. I use the following scheme especially for LEDs. The count at 1/60 of a second is
like a 60fps movie. You can generate any LED changes that your eyes can resolve.

// before setup( )
byte     iCount   = 0;
byte     iFrame   = 0;
long int lastTick;
// in setup( )
lastTick = millis( );


// in loop( )
// generate a 1-second iCount to 60
// never mess with willis( ) in your code after this
// loop( ) has to run < 17mS worst case. (If necessary, lengthen time between iCount)
if( millis( ) - lastTick >= 17 ) {
    lastTick = millis( );
    if( ++iCount >= 61 )    // iCount runs 1 - 60...
        iCount = 1;         // ...about 1/60 of second 
    iFrame = 1;             // iCount has changed
}


void example( ) {
    // about half second on half second off:
    if( iCount == 1 )       digitalWrite( LEDpin, HIGH );
    else if( iCount == 30 ) digitalWrite( LEDpin, LOW );
}

void another( ) {
    // about 5 flashes/sec: 
    if( !(iCount % 6) )   digitalWrite( LEDpin, ~digitalRead( LEDpin ) );
}
        .
        .
        .
if( iFrame == 1 ) {     // so you don't call functions unless you know iCount has changed
                        //    or also test iCount before calling any function.
    iFrame = 0;         // clear immediately
    example( );         
    another( );
}

westfw:
Something like this, perhaps?

I like it! So much processing burnt on blinking a LED :slight_smile:

Maybe there should be a list of "Ways to blink an LED that you are not advised to suggest in an interview"

Yours,
TonyWilk

FWIW

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

void loop()
{
  digitalWrite(LED_BUILTIN, millis()&2048);
}

aarg:
FWIW

void setup()

digitalWrite(LED_BUILTIN, millis()&2048);

Almost, but no cigar.
The parameter has to be a zero or nonzero byte; you could make that truthy: (millis&2048)==0

Yours,
TonyWilk

TonyWilk:
Almost, but no cigar.
The parameter has to be a zero or nonzero byte; you could make that truthy: (millis&2048)==0

Yours,
TonyWilk

Didn't you mean

(millis()&2048)==0

:wink:

So much processing burnt on blinking a LED

well, with a little adjustment it could become pretty good...
If the idea is to just "flash the led once in a while to show we're still alive", you can get a way with optimizations:

void loop() {
  if ((millis() & 1023) == 0) digitalWrite(13, HIGH);  // turn on at the ~ one-second marks
  if ((millis() & 1023) == 100) digitalWrite(13, LOW); // turn off at the ~1.1s marks
}

(bitwise AND with one less than a power of two is equivalent to modulus with the power of two.)

westfw:
If the idea is to just "flash the led once in a while to show we're still alive", you can get a way with optimizations:

I was about to complain that code doesn't work, when I realised I hadn't pinMode'ed as OUTPUT :wink: . I like to have proof of life in sketches, so that is now my way to do it.