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.
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:
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.
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( );
}
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 . I like to have proof of life in sketches, so that is now my way to do it.