You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On line 15, leds[num], if num is ever larger then 58 then you might be over writing bits off memory on the controller causing bad things. The value of num should only be a value of 0 to 58.
In C and C++, an array of length 59 is indexed from 0..58 (also known as zero-based indexing). However, your output shows that, eventually, at line 10 you executed with num set to 59. This is one index too large for the array you allocated.
As a result, when the process uses the value 59 for the array index at line 11, it is referencing the memory immediately following the leds array.
What's in that memory location? num is declared as a static variable immediately before leds. Compilers may (commonly?) put static variables next to each other, essentially in reverse order of their declaration. Thus, the leds array may be immediately after the memory location for num.
How would you fix this bug?
Delete line 18, and replace with code that does the following:
If num >= NUM_LEDS, then set num to NUM_LEDS - 1, and set moe to -1.
if num <= 0, then set num to zero and set moe to +1.
Closing — this was a bug in the sketch, not in FastLED, and @marmilicious and @henrygab both diagnosed it correctly in 2019.
Recording the answer in text (the report was a screenshot, and those don't survive well):
The bug:leds[num] was indexed with num == NUM_LEDS. An array of 59 elements is valid for indices 0..58, so index 59 is one past the end.
Why the symptom looked like "variable reset":@henrygab's explanation is the good part. The compiler had placed num in memory immediately before the leds array, so writing to leds[59] wrote on top ofnum. The variable appeared to spontaneously change value because the LED write was clobbering it.
That's a classic and genuinely confusing failure mode — the corrupted variable and the out-of-bounds write look unrelated, and nothing in the code that touches num is wrong.
The fix, as @henrygab gave it — clamp before writing rather than after:
if (num >= NUM_LEDS) { num = NUM_LEDS - 1; moe = -1; }
if (num <= 0) { num = 0; moe = +1; }
Worth knowing generally: on AVR there is no memory protection, so an out-of-bounds LED write silently corrupts whatever is adjacent — another variable, the stack, anything. If a variable changes value for no visible reason, an off-by-one on an array index is the first thing to suspect. Symptoms range from a wrong number to resets to a hang, depending on what got hit.
Closing since GitHub issues here are for FastLED bugs; for sketch help, GitHub Discussions or r/FastLED will get you a faster answer.
Activity
marmilicious commentedon Sep 13, 2019
On line 15, leds[num], if num is ever larger then 58 then you might be over writing bits off memory on the controller causing bad things. The value of num should only be a value of 0 to 58.
henrygab commentedon Oct 20, 2019
Marmilicious got it right.
Details about the bug in your code.
In C and C++, an array of length
59is indexed from0..58(also known as zero-based indexing). However, your output shows that, eventually, at line 10 you executed withnumset to59. This is one index too large for the array you allocated.As a result, when the process uses the value
59for the array index at line 11, it is referencing the memory immediately following theledsarray.What's in that memory location?
numis declared as a static variable immediately beforeleds. Compilers may (commonly?) put static variables next to each other, essentially in reverse order of their declaration. Thus, theledsarray may be immediately after the memory location fornum.How would you fix this bug?
Delete line 18, and replace with code that does the following:
num >= NUM_LEDS, then setnumtoNUM_LEDS- 1, and setmoeto-1.num <= 0, then setnumto zero and setmoeto+1.Hope that helps.
zackees commentedon Aug 1, 2026
Closing — this was a bug in the sketch, not in FastLED, and @marmilicious and @henrygab both diagnosed it correctly in 2019.
Recording the answer in text (the report was a screenshot, and those don't survive well):
The bug:
leds[num]was indexed withnum == NUM_LEDS. An array of 59 elements is valid for indices0..58, so index 59 is one past the end.Why the symptom looked like "variable reset": @henrygab's explanation is the good part. The compiler had placed
numin memory immediately before theledsarray, so writing toleds[59]wrote on top ofnum. The variable appeared to spontaneously change value because the LED write was clobbering it.That's a classic and genuinely confusing failure mode — the corrupted variable and the out-of-bounds write look unrelated, and nothing in the code that touches
numis wrong.The fix, as @henrygab gave it — clamp before writing rather than after:
Worth knowing generally: on AVR there is no memory protection, so an out-of-bounds LED write silently corrupts whatever is adjacent — another variable, the stack, anything. If a variable changes value for no visible reason, an off-by-one on an array index is the first thing to suspect. Symptoms range from a wrong number to resets to a hang, depending on what got hit.
Closing since GitHub issues here are for FastLED bugs; for sketch help, GitHub Discussions or r/FastLED will get you a faster answer.