Fixed brightness jump on overflow

With the previous algorithm, once every 65 seconds, there would be a significant jump in
the brightness of the "breathing" LEDs as the 16-bit value recorded from `millis()`
overflowed. Instead of dividing by 12, I changed it to a bit shift (4 bits; equivalent to
division by 16), so when the integer overflow occurs, the next value is what it should be.
pull/365/head
Michael Richters 7 years ago
parent 6836de6899
commit cef1926465

@ -5,7 +5,9 @@ breath_compute() {
// This code is adapted from FastLED lib8tion.h as of dd5d96c6b289cb6b4b891748a4aeef3ddceaf0e6 // This code is adapted from FastLED lib8tion.h as of dd5d96c6b289cb6b4b891748a4aeef3ddceaf0e6
// Eventually, we should consider just using FastLED // Eventually, we should consider just using FastLED
uint8_t i = (uint16_t)millis() / 12; // We do a bit shift here instead of division to ensure that there's no discontinuity
// in the output brightness when the integer overflows.
uint8_t i = (uint16_t)millis() >> 4;
if (i & 0x80) { if (i & 0x80) {
i = 255 - i; i = 255 - i;

Loading…
Cancel
Save