From cef192646567e3c19d0fec0829653f18218a80be Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Tue, 3 Apr 2018 23:28:23 -0500 Subject: [PATCH] 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. --- src/LEDUtils.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/LEDUtils.cpp b/src/LEDUtils.cpp index 734771ce..20c4f565 100644 --- a/src/LEDUtils.cpp +++ b/src/LEDUtils.cpp @@ -5,7 +5,9 @@ breath_compute() { // This code is adapted from FastLED lib8tion.h as of dd5d96c6b289cb6b4b891748a4aeef3ddceaf0e6 // 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) { i = 255 - i;