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
I'm using the builtin fill_rainbow function for generating a rainbow spectrum on an LED strip. The issue is, I get a random red pixel midway through the yellow region. I've tried it on 3 different Arduino nano's, and two different LED-strips. Here's the code:
Here pixel number 60 gets the rgb code 160, 0, 0. When it should have something like 160, 150, 0.
The strangest part of all is that I can get rid of the problem in two ways, either by uncommenting the line rgb = hsv;, or by commenting out the fill_rainbow function in the loop function. Any ideas whats going wrong?
Could be worth mentioning that its not always there. Depending on the value of hue it sometimes there and sometimes not. So one pixel (which physical pixel changes, and follows the yellow region of the spectrum) is flickering between red and yellow when the variable hue is slowly increased.
Sometimes I get 'smooth' color outputs, and sometimes I'm seeing a glitch around hue=60. Making small ("unrelated") changes to the sketch change the results. This looks like a compiler-level issue, which is weird already. (Maybe it's possibly related to ambiguities in the treatment of inline asm? Maybe? Investigating...)
Here's a segment out output showing R G B, and also the delta-R, delta-G, and delta-B between successive hues, when the glitch hits (me).
hue R dR G dG B dB
56 160 0 140 -3 0 0
57 160 0 143 -3 0 0
58 160 0 144 -1 0 0
59 160 0 147 -3 0 0
60 160 0 160 -13 0 0 <-- that's a big jump in G
61 160 0 152 8 0 0 <-- and then G jumps back again
62 160 0 155 -3 0 0
63 160 0 158 -3 0 0
64 160 0 160 -2 0 0
Making any number of small changes to the sketch itself can make this problem go away; the numbers change.
Just rendering the data to show the glitch. It's not a math error per se, and the fact that re-arranging the source code has an effect on it is indicative of a certain kind of problem.
Just for my own reference, here's 'my' version of the code which shows a funky value at pixel 60 (aka hue = 60) repeatably, at least on an Leonardo clone, under avr-gcc 5.4.0 (Arduino 1.8.6 hourly build 2018/07/20... which is questionable, but still)
I was just coming to the issues section to submit one of my own when I found this thread.
Running Arduino 1.8.7 on Windows 10.
I was playing around with a color cycle animation, and trying to use CHSV(i, 200, 255) for a more desaturated look. Because of that, I'm getting the hiccup in a slightly different spot on the hue wheel, but still the same problem.
Thank you for the addition data point. I am definitely seeing some weird
stuff going on — and it’s not “just” a quirk in the arithmetic. Something
else is up, I think. I’m going to keep trying to isolate it.
On Thu, Dec 6, 2018 at 10:37 AM reyemxela ***@***.***> wrote:
I was just coming to the issues section to submit one of my own when I
found this thread.
Running Arduino 1.8.7 on Windows 10.
I was playing around with a color cycle animation, and trying to use
CHSV(i, 200, 255) for a more desaturated look. Because of that, I'm getting
the hiccup in a slightly different spot on the hue wheel, but still the
same problem.
hue-r---g---b
47 146 107 12
48 146 110 12
49 146 112 12
50 146 114 12
51 146 145 12 <---
52 146 118 12
53 146 120 12
54 146 122 12
—
You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
<#668 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABRdatNfW_9T3QMRMr9QjxD4J8og86I0ks5u2TmegaJpZM4XilEo>
.
I noticed the line with hsv.sat = 240; has a number that doesn't fit into 255 ( I know right, I never even looked beyond this ).
Anyway changing to hsv.sat = 255; seems to have fixed the issue at my end.
What is weird is that I'm passing 240 in the sat parameter of fill_rainbow now and it's working without the red pixel. But this only works if I have code present which alters the value of sat...
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string, then the string's value:
if (inChar == '\n') {
sat = inString.toInt();
Serial.print("Value:");
Serial.println(sat);
inString = "";
}
}
If I remove this last section of code, having a hard coded sat value the problem still exists. How odd.
I have been struggling over the same problem in the function “fill_rainbow()” where a wrong color is generated if hue = 60.
During my investigations I detected a strange behavior. The generated code size differs if the number of leds is changed in the “fill_rainbow()” call:
fill_rainbow( leds, 69, hue, 1); => 4786 Bytes No Error
fill_rainbow( leds, 70, hue, 1); => 4844 Bytes Error !!
For the tests I use GCC 7.03.00, FastLED version 3.003.002. But the effect is similar with GCC 5.04.00
In my test program I have noticed that the wrong LED could also be pink if NUM_LEDS is modified.
RGB values
160 147 0 160 0 160 <= Error pink LED
160 152 0
160 155 0
160 158 0
160 160 0
But I have not been able to generate this failure with the code above.
As a work around I have changed the “hsv.sat = 240;” to 242. This has also been mentioned by jamesgohl.
If the code isn’t working, its worth it to me to understand exactly why that is, what changed in the compilation process, so that I can understand where else to be aware of it. Everyone has been very helpful here with this one, especially during a very hard personal time, and I really appreciate all of it.
Reacted by Marc Miller, ddesborough, ohsix, Torsten Ascher, Elliott Kember and Dave Madison
I am experiencing this problem. I am a coding novice, so I don't know the significance of my findings. If you use delay() the red pixel shows. However if you use millis() it does not.
The following code creates a red pixel in the yellowish region:
This is a fairly simple change (a small change to two lines in a text file) and it will let you work around the issue for now. Use the -O1 option and you should be good to go. This route will let you write "normal" code that you won't need to change later on.
I almost forgot, you can also try using Arduino create (the web based version) rather than a local install as it seems to compile differently and not have the issue. At least it did back when I was testing this.
Closing — the remaining half of this is fixed in #3808 (merged as 26df858).
This bug had two independent causes sharing one root hazard, which is why it survived a fix.
Half one, fixed back in #943: the compiler was reordering the chained scale8_LEAVING_R1_DIRTY calls. asm volatile("") barriers between them stopped that. This was @ddesborough's third proposed remedy, and it's still in the source with a comment naming this issue.
Half two, fixed now:scale8*_LEAVING_R1_DIRTY skips restoring AVR's __zero_reg__ so several calls can share one cleanup_R1(). Its docs say "You MUST call cleanup_R1() after using this function."hsv2rgb_rainbow() had three paths that returned without doing so:
if (Gscale) g = scale8_video_LEAVING_R1_DIRTY(g, Gscale) dirties R1 — then sat == 255 skips the saturation block andval == 255 skips the value block, so nothing cleans up.
Same call, then the sat == 0 branch, which only assigns 255s.
val = scale8_video_LEAVING_R1_DIRTY(val, val) dirties R1 — then the val == 0 branch assigns zeros; only the else batched into a cleanup_R1().
Returning with r1 non-zero violates the AVR ABI: the compiler treats r1 as a hard zero and emits code assuming it, so whatever runs next can compute garbage.
That accounts for the parts of this that never made sense:
@Tim-AQ — the wrong pixel tracks the hue because hue decides which branch runs, and only some branches leaked.
@kriegsman — your delta tables were right that it wasn't a math error, and your instinct that "making small unrelated changes to the sketch change the results" pointed at codegen was exactly correct. The leak's damage depends on what the compiler happens to schedule after the call.
@ddesborough — -O1 helping, and Arduino Create behaving differently, both follow: they change surrounding codegen, not the conversion math.
@OneEyeRick — your delay() vs millis() finding was a genuine signal, not noise. Same mechanism.
You should no longer need #define SCALE8_C 1 or -O1 as a workaround.
Also worth recording: @kriegsman wrote in 2019, "if we're having this issue here, we're probably having it other places too but we just haven't isolated those bugs yet." That was right, and the same LEAVING_R1_DIRTY / cleanup_R1 pairing appears in crgb.hpp and colorutils.cpp.hpp too. Those uses look correctly paired, but they're worth an audit — I'd rather say so than imply this class of bug is now provably gone everywhere.
Thanks @Tim-AQ for the original report and for noticing it was intermittent, @ddesborough for the root-cause analysis that made this fixable, and @reyemxela and @OneEyeRick for reproductions that narrowed the trigger.
Activity
kriegsman commentedon Oct 17, 2018
Great bug report, thank you I’ll take a look.
Tim-AQ commentedon Oct 17, 2018
Could be worth mentioning that its not always there. Depending on the value of hue it sometimes there and sometimes not. So one pixel (which physical pixel changes, and follows the yellow region of the spectrum) is flickering between red and yellow when the variable hue is slowly increased.
kriegsman commentedon Oct 19, 2018
Something funny is going on here.
Sometimes I get 'smooth' color outputs, and sometimes I'm seeing a glitch around hue=60. Making small ("unrelated") changes to the sketch change the results. This looks like a compiler-level issue, which is weird already. (Maybe it's possibly related to ambiguities in the treatment of inline asm? Maybe? Investigating...)
Here's a segment out output showing R G B, and also the delta-R, delta-G, and delta-B between successive hues, when the glitch hits (me).
hue R dR G dG B dB
56 160 0 140 -3 0 0
57 160 0 143 -3 0 0
58 160 0 144 -1 0 0
59 160 0 147 -3 0 0
60 160 0 160 -13 0 0 <-- that's a big jump in G
61 160 0 152 8 0 0 <-- and then G jumps back again
62 160 0 155 -3 0 0
63 160 0 158 -3 0 0
64 160 0 160 -2 0 0
Making any number of small changes to the sketch itself can make this problem go away; the numbers change.
kriegsman commentedon Oct 19, 2018
Just rendering the data to show the glitch. It's not a math error per se, and the fact that re-arranging the source code has an effect on it is indicative of a certain kind of problem.
kriegsman commentedon Oct 19, 2018
Just for my own reference, here's 'my' version of the code which shows a funky value at pixel 60 (aka hue = 60) repeatably, at least on an Leonardo clone, under avr-gcc 5.4.0 (Arduino 1.8.6 hourly build 2018/07/20... which is questionable, but still)
`#include <FastLED.h>
#define DATA_PIN 13
#define NUM_LEDS 114
CRGB leds[NUM_LEDS];
uint8_t hue = 0;
void setup() {
Serial.begin(115200);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
delay(3000);
}
void loop() {
fill_rainbow( leds, NUM_LEDS, hue, 1);
static bool dump = true;
if( dump) {
dump = false;
}
FastLED.show();
}
`
Output =
56 160 140 0
57 160 143 0
58 160 144 0
59 160 147 0
60 160 160 0 <-- 'G' is too big here
61 160 152 0
62 160 155 0
63 160 158 0
64 160 160 0
PS I hate github Markdown. I'll get over it.
kriegsman commentedon Oct 19, 2018
What version of Arduino are you running, @Tim-AQ, and on what platform?
Tim-AQ commentedon Oct 22, 2018
Im using Arduino version 1.8.7 , and the platform is Manjaro Linux.
When the glitch occurs for me, this is the output i get:
hue R G B
58 160 144 0
59 160 147 0
60 160 0 0
61 160 152 0
62 160 155 0
reyemxela commentedon Dec 6, 2018
I was just coming to the issues section to submit one of my own when I found this thread.
Running Arduino 1.8.7 on Windows 10.
I was playing around with a color cycle animation, and trying to use CHSV(i, 200, 255) for a more desaturated look. Because of that, I'm getting the hiccup in a slightly different spot on the hue wheel, but still the same problem.
hue-r---g---b
47 146 107 12
48 146 110 12
49 146 112 12
50 146 114 12
51 146 145 12 <---
52 146 118 12
53 146 120 12
54 146 122 12
kriegsman commentedon Dec 6, 2018
sabifa commentedon May 6, 2019
Is this issue still present? I'm not quite sure why but I'm facing this problem right now.
My rainbow code looks like the following:
If I delete following code (which is in an entirely different method) it works fine:
Do you have any idea why this is caused and how I am able to fix the problem?
quakec commentedon Oct 12, 2019
@kriegsman
I had just had the exact same issue.
My setup: Arduino Uno and a WS2812 strip on a pcb ring.
I suspected the problem lie somewhere with a value being floored from an integer division. So I looked at the fill_rainbow function in colorutils.cpp
I noticed the line with
hsv.sat = 240;has a number that doesn't fit into 255 ( I know right, I never even looked beyond this ).Anyway changing to
hsv.sat = 255;seems to have fixed the issue at my end.Hope this helps you in the quest to isolate this.
James
quakec commentedon Oct 12, 2019
@kriegsman
Okay further updates, I had modified the sig for the fill_rainbow function to accept a
uint8_t satparameter.What is weird is that I'm passing 240 in the sat parameter of
fill_rainbownow and it's working without the red pixel. But this only works if I have code present which alters the value ofsat...If I remove this last section of code, having a hard coded sat value the problem still exists. How odd.
kriegsman commentedon Oct 12, 2019
Thank you for the continued help and investigations. This is still on my list to look into further. Much appreciated.
Hardi-St commentedon Nov 6, 2019
Hi together,
first of all I’d like to thank you for the wonderful FastLED library. I’m using it in “my” MobaLedLib library which could be found here: https://github.com/Hardi-St
The library controls the lights and more on a model railway. The functions are described here: https://www.stummiforum.de/viewtopic.php?f=7&t=165060&sd=a&start=0
I have been struggling over the same problem in the function “fill_rainbow()” where a wrong color is generated if hue = 60.
During my investigations I detected a strange behavior. The generated code size differs if the number of leds is changed in the “fill_rainbow()” call:
fill_rainbow( leds, 69, hue, 1); => 4786 Bytes No Error
fill_rainbow( leds, 70, hue, 1); => 4844 Bytes Error !!
For the tests I use GCC 7.03.00, FastLED version 3.003.002. But the effect is similar with GCC 5.04.00
Next I compared the generated ASM code of the two builds. For some reasons there are a lot of changes in the ASM code. One change may cause the problem. It affects the call to “scale8_LEAVING_R1_DIRTY”. For me it looks like the compiler has changed the sequence of the R1 usage. It’s shown in this picture:
https://abload.de/image.php?img=compare_fill_rainbow_afjvu.jpg
Unfortunately I have no experience with the AVR assembler code.
I hope that this may help to fix the problem.
Here is the code I use for the tests:
In my test program I have noticed that the wrong LED could also be pink if NUM_LEDS is modified.
RGB values
160 147 0
160 0 160 <= Error pink LED
160 152 0
160 155 0
160 158 0
160 160 0
But I have not been able to generate this failure with the code above.
As a work around I have changed the “hsv.sat = 240;” to 242. This has also been mentioned by jamesgohl.
Hardi
11 remaining items
kriegsman commentedon Dec 8, 2019
If the code isn’t working, its worth it to me to understand exactly why that is, what changed in the compilation process, so that I can understand where else to be aware of it. Everyone has been very helpful here with this one, especially during a very hard personal time, and I really appreciate all of it.
Random red pixel fix
OneEyeRick commentedon Dec 29, 2020
I am experiencing this problem. I am a coding novice, so I don't know the significance of my findings. If you use delay() the red pixel shows. However if you use millis() it does not.
The following code creates a red pixel in the yellowish region:
The following code does not create a red pixel:
I don't know if this helps solve the problem. Hopefully someone can use it to get rid of their red pixel.
ddesborough commentedon Dec 30, 2020
@OneEyeRick - Please see my comment where I discuss the compiler options: #668 (comment)
This is a fairly simple change (a small change to two lines in a text file) and it will let you work around the issue for now. Use the -O1 option and you should be good to go. This route will let you write "normal" code that you won't need to change later on.
I almost forgot, you can also try using Arduino create (the web based version) rather than a local install as it seems to compile differently and not have the issue. At least it did back when I was testing this.
fix(hsv): stop hsv2rgb_rainbow leaving AVR's R1 dirty on three paths (#…
zackees commentedon Aug 1, 2026
Closing — the remaining half of this is fixed in #3808 (merged as
26df858).This bug had two independent causes sharing one root hazard, which is why it survived a fix.
Half one, fixed back in #943: the compiler was reordering the chained
scale8_LEAVING_R1_DIRTYcalls.asm volatile("")barriers between them stopped that. This was @ddesborough's third proposed remedy, and it's still in the source with a comment naming this issue.Half two, fixed now:
scale8*_LEAVING_R1_DIRTYskips restoring AVR's__zero_reg__so several calls can share onecleanup_R1(). Its docs say "You MUST call cleanup_R1() after using this function."hsv2rgb_rainbow()had three paths that returned without doing so:if (Gscale) g = scale8_video_LEAVING_R1_DIRTY(g, Gscale)dirties R1 — thensat == 255skips the saturation block andval == 255skips the value block, so nothing cleans up.sat == 0branch, which only assigns255s.val = scale8_video_LEAVING_R1_DIRTY(val, val)dirties R1 — then theval == 0branch assigns zeros; only theelsebatched into acleanup_R1().Returning with
r1non-zero violates the AVR ABI: the compiler treatsr1as a hard zero and emits code assuming it, so whatever runs next can compute garbage.That accounts for the parts of this that never made sense:
-O1helping, and Arduino Create behaving differently, both follow: they change surrounding codegen, not the conversion math.delay()vsmillis()finding was a genuine signal, not noise. Same mechanism.You should no longer need
#define SCALE8_C 1or-O1as a workaround.Also worth recording: @kriegsman wrote in 2019, "if we're having this issue here, we're probably having it other places too but we just haven't isolated those bugs yet." That was right, and the same
LEAVING_R1_DIRTY/cleanup_R1pairing appears incrgb.hppandcolorutils.cpp.hpptoo. Those uses look correctly paired, but they're worth an audit — I'd rather say so than imply this class of bug is now provably gone everywhere.Thanks @Tim-AQ for the original report and for noticing it was intermittent, @ddesborough for the root-cause analysis that made this fixable, and @reyemxela and @OneEyeRick for reproductions that narrowed the trigger.
test(gfx): assert hue ramps are continuous instead of printing them (#…