Repository navigation
Description
What happened?
The brightness transition in handleTransitions() was changed from float to integer math between v0.15.x and v16.0.0. The integer version uses C's truncation-toward-zero division, which causes small brightness deltas to produce zero change, stalling the transition indefinitely.
This is most visible during nightlight fade at low starting brightness, where the brightness appears stuck and then snaps off at the end.
Root cause
v0.15.x used float math:
float tper = (millis() - transitionStartTime) / (float)strip.getTransition();
briT = briOld + ((bri - briOld) * tper);v16.0.0 uses integer math:
int deltaBri = (int)bri - (int)briOld;
briT = briOld + (deltaBri * ti / tr);For a brightness change of -1 with default transition time of 700ms, at 100ms into the transition:
- Float (v0.15.x):
4 + (-1 * 0.143) = 3.86-> truncated tobriT = 3. Works. - Integer (v16.0.0):
4 + (-1 * 100 / 700) = 4 + (0) = 4. C truncates-100/700toward zero, giving 0. briT never changes.
This interacts badly with nightlight, which calls colorUpdated() every 100ms (via handleNightlight). Each call restarts the transition timer in stateUpdated() (transitionStartTime = now). Since the integer math produces zero change in 100ms for small deltas, and the transition is restarted before it can complete, briT is permanently stuck at its starting value.
The transition completes only when ti >= tr (700ms elapsed), but nightlight restarts it every 100ms, so this condition is never reached. The brightness remains stuck until the nightlight period expires and forces bri=0.
Fix
Use floor division instead of C's default truncation-toward-zero for the interpolation. This matches the v0.15.x float behavior where fractional results truncate downward:
int deltaBri = (int)bri - (int)briOld;
- briT = briOld + (deltaBri * ti / tr);
+ long interp = (long)deltaBri * ti;
+ if (interp < 0) interp -= (tr - 1); // floor division instead of truncation toward zero
+ briT = briOld + interp / tr;With this fix, deltaBri=-1 at ti=1ms gives: (-1 - 699) / 700 = -1, so briT immediately steps to the target value — matching v0.15.x behavior where the float result (e.g., 3.99) truncated to 3 on the first frame.
Environment
- WLED v16.0.0
- Default transition time: 700ms
- Nightlight mode: fade or color fade
To Reproduce Bug
- Set any effect, brightness to 4-10.
- Enable nightlight fade to 0 over 1 minute
- Expected: Brightness steps down gradually (4, 3, 2, 1, 0)
- Actual: Brightness stays at starting value, then snaps to 0 when nightlight ends
Note the reported bri brightness and UI does drop, just the observed LED brightness does not.
This affects any brightness transition where |deltaBri| * updateInterval < transitionTime. With the default 700ms transition and 100ms nightlight update interval, any brightness change of less than 8 levels stalls.
Expected Behavior
LEDs reduce to very low brightness before finally turning off.
Install Method
Binary from WLED.me
What version of WLED?
16.0
Which microcontroller/board are you seeing the problem on?
ESP32
Relevant log/trace output
Anything else?
No response
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
DedeHai commentedon May 16, 2026
just a question: did AI tell you this? I don't think that is true.
softhack007 commentedon May 16, 2026
no, it wasn't. definitely.
?????????????????????????????
[-]Brightness transitions stall for small changes due to integer division truncation[/-][+]nightlight problem with brightness transitions (for small changes, maybe due to integer truncation)[/+]softhack007 commentedon May 16, 2026
@WJCFerguson please describe the problem from your user's perspective, in your own words.
softhack007 commentedon May 16, 2026
@coderabbitai while the description makes a lot of assumptions that seems unusual or even show lack of understanding of C/C++ semantics, I want to give this problem report a second chance.
mainvs.0_15_x.coderabbitai commentedon May 17, 2026
🧩 Analysis chain
Let me fetch the issue and the relevant code from both branches.
🏁 Script executed:
Length of output: 4640
🏁 Script executed:
Length of output: 247
Let me fetch the code directly via the GitHub API.
🏁 Script executed:
Length of output: 99
🏁 Script executed:
Length of output: 1339
🏁 Script executed:
Length of output: 3190
🏁 Script executed:
Length of output: 5834
🏁 Script executed:
Length of output: 18568
@softhack007— I've read bothled.cppfiles in full. Here's my analysis:What the code actually shows
1. Float → Integer: the claim is TRUE
0_15_xhandleTransitions():mainhandleTransitions():The change from float to integer in the brightness interpolation formula is real and present in the code.
2. C truncation claim: also correct
For
deltaBri = -1,ti = 100,tr = 700(default transition):deltaBri * ti = -100-100 / 700in C integer arithmetic = 0 (truncates toward zero, not toward negative infinity)briT = briOld + 0→ no changeThe
?????reaction may have been premature — this is valid C behavior.In
0_15_x, the same scenario:(bri - briOld) * tper = -1 * 0.143f = -0.143fbriT = (byte)(4.0f + (-0.143f)) = (byte)(3.857f)→ truncates to 3 ✓The float path produces a brightness step; the integer path produces zero.
3. The nightlight timer restart: also real
In both branches,
stateUpdated()— when called while a transition is already active — does this:handleNightlight()callscolorUpdated()→stateUpdated()every 100ms. So every 100ms,tiresets to zero. The transition window forhandleTransitions()is always a fresh 0–100ms slice. Withtr = 700ms,deltaBri * ti / tris always0for|deltaBri| < 7.Is it a bug or just an accuracy change?
It's a real functional regression. Specifically:
In
0_15_x, even a 1-unit delta attper = 100/700 ≈ 0.143produces3.857fwhich truncates to3(byte), so the LED visibly steps. The float path "accidentally" benefits from how floating-point subtraction interacts with byte truncation.From a user's perspective:
bridecrements slowly via float math inhandleNightlight()— correct behavior.briT) stays stuck at the starting value the entire nightlight duration.briis forced to 0, and suddenly the LEDs snap off.The reported symptoms are internally consistent with the code.
Credibility assessment
The reporter's technical root-cause analysis is largely correct: the float→integer change, the C truncation behaviour, and the nightlight timer restart all check out in the code. Some of the framing may be AI-assisted given the very structured writeup, but the underlying observations hold. The bug is plausible and the effect would be reproducible at low brightness with fade nightlight — worth a closer look.
One thing the reporter's proposed fix gets wrong: using floor division isn't the right abstraction here. The real fix would be ensuring the integer interpolation accumulates properly across the transition window, or restoring float math for this specific calculation — but that's for you to judge.
softhack007 commentedon May 17, 2026
@coderabbitai can you create two plots for the nightlight transition starting at brightness =6, so I can see how 0.15.x and 16.0.0 behave differently?
softhack007 commentedon May 17, 2026
@coderabbitai so a simple error diffusion would improve accuracy and avoid jumps? Or should we better use fixed point math?
coderabbitai commentedon May 17, 2026
🧩 Analysis chain
Now let me use the simulated data to build the Mermaid charts.
Here are the two comparison plots simulated from the actual code logic (starting brightness = 6, nightlight fading to 0, transition time = 700 ms, nightlight tick = 100 ms):
WLED 0.15.x — Float math (
briT = briOld + Δbri × tper)xychart-beta title "0.15.x: briT over time (start bri=6, fade to 0)" x-axis "Time (ms)" [0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700, 4800, 4900, 5000, 5100, 5200, 5300, 5400, 5500, 5600, 5700, 5800, 5900, 6000, 6100, 6200, 6300, 6400, 6500] y-axis "Brightness (briT)" 0 --> 7 line [6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]WLED 16.0.0 (main) — Integer math (
briT = briOld + Δbri × ti / tr)xychart-beta title "16.0.0 (main): briT over time (start bri=6, fade to 0)" x-axis "Time (ms)" [0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700, 4800, 4900, 5000, 5100, 5200, 5300, 5400, 5500, 5600, 5700, 5800, 5900, 6000, 6100, 6200, 6300, 6400, 6500] y-axis "Brightness (briT)" 0 --> 7 line [6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0]Root cause summary
briOld + Δbri × (ti / TR)briOld + (Δbri × ti) / TRtiwhen NL ticksΔbri × ti / TRforΔbri=11 × 0.143 = 0.143→ visible1 × 100 / 700 = 0→ invisibleThe nightlight ticker resetting the transition every 100 ms is the same in both versions — but floating-point arithmetic "saves" the old code by producing a non-zero fractional step, while integer truncation in the new code produces exactly zero every time.
🏁 Script executed:
Length of output: 6849
🏁 Script executed:
Length of output: 2947
Good catch from the simulation — it revealed that my simulation had a critical bug: Python's
//does floor division (-90 // 700 = -1), but C truncates toward zero (-90 / 700 = 0). This masked the entire divergence. Let me fix it.🏁 Script executed:
Length of output: 15815
🏁 Script executed:
Length of output: 6985
@softhack007— The corrected simulation (using proper C-style truncation toward zero viamath.trunc()) now clearly shows the divergence. Here are the plots:0.15.x — briT tracks
bristep-by-stepxychart-beta title "0.15.x: briT during nightlight fade (bri=6→0, TR=700ms)" x-axis "Time (ms)" [0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000, 2200, 2400, 2600, 2800, 3000, 3200, 3400, 3600, 3800, 4000, 4200, 4400, 4600, 4800, 5000, 5200, 5400, 5600, 5800, 6000, 6200, 6400] y-axis "briT (rendered brightness)" 0 --> 7 line [6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0]16.0.0 (main) — briT frozen at 6, then snaps
xychart-beta title "16.0.0 (main): briT during nightlight fade (bri=6→0, TR=700ms)" x-axis "Time (ms)" [0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000, 2200, 2400, 2600, 2800, 3000, 3200, 3400, 3600, 3800, 4000, 4200, 4400, 4600, 4800, 5000, 5200, 5400, 5600, 5800, 6000, 6200, 6400] y-axis "briT (rendered brightness)" 0 --> 7 line [6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6]Why the divergence happens (the cascade lock)
The key is what happens at t = 1010 ms (10 ms after the first
bridecrement):0.15.x(float)main(C integer)6 + (−1) × (10 / 700.0f)6 + trunc(−1 × 10 / 700)5.9857…→ byte5✅6 + trunc(−0.014…) = 6 + 0 = 6❌bri_oldset toOnce
bri_olddiverges, it cascades: the integer branch re-locksbri_old = 6at every 100 ms tick, becausebriTnever moves. Even asbricontinues decrementing to 0,bri_oldstays at 6,deltagrows to −6, buttrunc(−6 × 90 / 700) = trunc(−0.77) = 0— still zero. The LED stays frozen at full brightness6for the entire nightlight duration and never fades.coderabbitai commentedon May 17, 2026
@softhack007— Both are viable, but they solve the problem at different levels. Here's the tradeoff:Option A: Error Diffusion (Bresenham accumulator)
Carry the fractional remainder forward in a persistent state variable:
What this does: across N calls with
ti ≈ 100msandtr = 700ms, the accumulated numerator grows bydeltaBri * 100each tick. After 7 ticks (700ms), the sum reachesdeltaBri * 700 = tr, andstepfinally becomes ±1. This perfectly mirrors what the float path "accidentally" achieved.Downside:
briErrormust be reset to 0 wheneverbriOldis re-anchored (i.e., at the start of every new transition instateUpdated()). Forgetting this produces a phantom jump on the next transition.Option B: Fixed-Point Math
Scale up before dividing so the fractional part isn't discarded:
This is algebraically equivalent to
briOld + deltaBri * tper(the old float formula) but in integer math with no accumulator state.deltaBri * tifits comfortably in int32 (max 255 × 65535 ≈ 16.7M).Downside: slightly more arithmetic per call; need care about which scale factor you choose (256 = 8 fractional bits is plenty for an 8-bit brightness value).
Which to prefer?
briError)int32_t)My recommendation: fixed-point math. It's stateless, directly equivalent to the 0.15.x float formula, and doesn't require touching
stateUpdated()to reset an accumulator. Error diffusion is elegant for pixel dithering but adds hidden state to what is currently a purely functional computation — and in an embedded context that state needs careful lifecycle management.If the goal is minimal diff / lowest risk, you could also simply restore
floatfor just this one line. It's a single lerp, not a hot path, andfloaton ESP32 is hardware-accelerated anyway.softhack007 commentedon May 17, 2026
@DedeHai what do you think? I'm still not 100% convinced of a bug, but if confirmed, the transition calculation should only happen once per frame so I'd favour code simplicity here - linear interpolation in plain
float.5 remaining items
coderabbitai commentedon May 17, 2026
DedeHai commentedon May 17, 2026
coderabbitai commentedon May 17, 2026
🧩 Analysis chain
Apologies for the off-topic response. Let me look at the actual nightlight code to answer your question properly.
🏁 Script executed:
Length of output: 3240
🏁 Script executed:
Length of output: 3995
🏁 Script executed:
Length of output: 3667
@DedeHai— sorry for the previous off-track response.Here's the direct answer:
Simplest fix: guard the transition restart in
stateUpdated()The transition is restarted unconditionally at the bottom of
stateUpdated()wheneverstrip.getTransition() > 0. The one-liner change is to extend the existing short-circuit condition:When nightlight is in FADE/COLORFADE, this takes the
applyFinalBri()path directly, which setsbriT = briimmediately — exactly what nightlight wants since it's already doing its own float interpolation forbrievery 100ms. No accumulator, no extra state.Side effects per mode
colorUpdated()on ticks?NL_MODE_SET(0)NL_MODE_FADE(1)NL_MODE_COLORFADE(2)NL_MODE_SUN(3)The only observable change for FADE/COLORFADE is that
briTtracksbriimmediately on each 100ms tick instead of smoothly interpolating within that 100ms window. Sincebriitself is already float-interpolated by nightlight across the full duration, the 100ms step size is fine and matches 0.15.x behaviour.You'd also want to make sure
transitionActiveis cleared (it already is in thegetTransition() == 0branch) so an ongoing unrelated transition doesn't linger after nightlight ends.DedeHai commentedon May 17, 2026
@coderabbitai a simpler fix would be to call applyFinalBri() after colorUpdated(CALL_MODE_NO_NOTIFY) in handleNightlight(). Please point out if there are any issues using this approach.
coderabbitai commentedon May 17, 2026
🧩 Analysis chain
🏁 Script executed:
Length of output: 9107
🏁 Script executed:
Length of output: 9311
@DedeHai— The approach is sound. Here's what happens step by step and the only things worth noting:Why it works:
After
colorUpdated(CALL_MODE_NO_NOTIFY)returns,stateUpdated()has settransitionActive = trueand resettransitionStartTime = now(the bug). CallingapplyFinalBri()immediately after overwritesbriOld = briandbriT = bri, so the rendered brightness is correct right now. Then between NL ticks,handleTransitions()seestransitionActive = truebutdeltaBri = bri − briOld = 0, so it computes no change — correctly frozen at the current NL brightness until the next tick.Two minor side effects, neither harmful:
transitionActiveis not cleared.applyFinalBri()doesn't settransitionActive = false. It will staytrueuntil either the next NL tick resetstransitionStartTimeagain, orti >= tr(700 ms after the last tick), at which pointhandleTransitions()will callapplyFinalBri()itself withdeltaBri = 0. This is benign.Extra
strip.trigger()per NL tick.applyFinalBri()callsstrip.trigger()— one extra forced render every 100 ms. Completely harmless.No effect on other modes:
NL_MODE_SETandNL_MODE_SUNare outside the FADE/COLORFADE block — untouched.NL_MODE_COLORFADEcolor transitions are managed by the strip's own segment transition engine (set once viastrip.setTransitionMode(true)at new transition start, not on every tick), soapplyFinalBri()— which only touches brightness — doesn't interfere with color fading.The proposed change is effectively a one-liner:
No architectural changes needed and no interaction with any other nightlight mode.
softhack007 commentedon May 17, 2026
@coderabbitai is the fix in #5625 only relevant for the latest release, or would it also apply to the
0_15_xbranch?coderabbitai commentedon May 17, 2026
🧩 Analysis chain
🏁 Script executed:
Length of output: 175
🏁 Script executed:
Length of output: 4300
@softhack007— The fix in#5625is only relevant tomain(16.0.0+). Here's why:The root cause — nightlight's FADE/COLORFADE calling
colorUpdated()every 100 ms and thereby restarting the transition timer — exists identically in both branches. The0_15_xcode at line 251 does the same thing:However,
0_15_xis not affected because itshandleTransitions()uses float math:Even when
tiis only ~100 ms out of 700 ms,tper ≈ 0.143produces a non-zero fractional delta that — after byte truncation — yields a visible brightness step. The float path accidentally compensates for the timer restart.In
main, the integer formulabriOld + (deltaBri * ti / tr)givestrunc(-1 × 100 / 700) = 0, so no step ever occurs — that's where the bug manifests and why theapplyFinalBri()fix is needed.Summary:
main(16.0.0)#56250_15_x[-]nightlight problem with brightness transitions (for small changes, maybe due to integer truncation)[/-][+]nightlight problem with brightness transitions (for small changes)[/+]WJCFerguson commentedon May 18, 2026
Apologies for trusting AI too much in the report and being AFK since reporting. The "fix" it gave me appeared to work to me, and so I then trusted its analysis/fix as being legit, and I should have left it all out.
The issue in my own words:
As some of the comments seem to have identified, nightlight from low brightness, say 6, to zero, in 0.15.4 gave a slow fade out, progressively more pixels at the end would go dark, and finally the last few very dim pixels twinkled out. It makes for a very peaceful nighttime shutdown
With 0.16, it stays very bright, perhaps roughly as bright as at brightness level 4, with very slight if any dimming from there, and then everything turns off suddenly.
Clarification and cleanup of nightlight