Go Down

Topic: Flashing LED with Modulo? (Read 1 time) previous topic - next topic

Kiwi_Bloke

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.


UKHeliBob

Did the code use delay() or millis() ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

TonyWilk

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:

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


Yours,
  TonyWilk

Kiwi_Bloke

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

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

Kiwi_Bloke

Er... there's the 'modulo' operator: % which gives the remainder after division

So this is possible, but maybe a bit cryptic:

Code: [Select]
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

GrooveFlotilla

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

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

TonyWilk

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:

Code: [Select]

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

//or

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


Yours,
  TonyWilk

outsider

#7
Jan 25, 2018, 11:29 am Last Edit: Jan 25, 2018, 11:39 am by outsider
Something like this?
Code: [Select]
void setup() {
 bitSet(DDRB,5);
}
void loop() {
 int interval = 1000, pulseLen = 30;
 bitWrite(PORTB,5,millis() % interval <= pulseLen);
}

wilfredmedlin

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

Keep Calm and call 0118 999 88199 9119725...... 3

outsider

#9
Jan 25, 2018, 01:41 pm Last Edit: Jan 25, 2018, 01:50 pm by outsider
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.
 

UKHeliBob

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

Code: [Select]

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
Code: [Select]

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));
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

wilfredmedlin

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.
Keep Calm and call 0118 999 88199 9119725...... 3

westfw

Something like this, perhaps?
Code: [Select]
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.

boolrules

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.

Code: [Select]

// 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( );
}

TonyWilk

Something like this, perhaps?
I like it!  So much processing burnt on blinking a LED :)

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

Yours,
 TonyWilk

Go Up