Skip to content

fill_rainbow() - Unexpected red pixel #668

Closed

Description

@Tim-AQ

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:

#include <FastLED.h>

#define DATA_PIN 2
#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);

  fill_rainbow( leds, NUM_LEDS, hue, 1);
  for(int i=0; i<NUM_LEDS; i++) {
    Serial.print(i);
    Serial.print(' ');
    Serial.print(leds[i].r);
    Serial.print(' ');
    Serial.print(leds[i].g);
    Serial.print(' ');
    Serial.println(leds[i].b);
  }

  CHSV hsv;
  CRGB rgb;
  //rgb = hsv;
}


void loop() {
  fill_rainbow( leds, NUM_LEDS, hue, 1);
  FastLED.show();
}

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?

Activity

  1. kriegsman commented on Oct 17, 2018

    @kriegsman
    Member

    Great bug report, thank you I’ll take a look.

  2. Tim-AQ commented on Oct 17, 2018

    @Tim-AQ
    Author

    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.

  3. kriegsman commented on Oct 19, 2018

    @kriegsman
    Member

    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.

  4. kriegsman commented on Oct 19, 2018

    @kriegsman
    Member

    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.

    screen shot 2018-10-19 at 12 59 58 pm-colored

  5. self-assigned this
    on Oct 19, 2018
  6. kriegsman commented on Oct 19, 2018

    @kriegsman
    Member

    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;

    for(int i=56; i<65; i++) {
      Serial.print(i);
      Serial.print(' ');
      Serial.print(leds[i].r);
      Serial.print(' ');
      Serial.print(leds[i].g);
      Serial.print(' ');
      Serial.println(leds[i].b);
    }
    

    }

    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.

  7. kriegsman commented on Oct 19, 2018

    @kriegsman
    Member

    What version of Arduino are you running, @Tim-AQ, and on what platform?

  8. Tim-AQ commented on Oct 22, 2018

    @Tim-AQ
    Author

    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

  9. reyemxela commented on Dec 6, 2018

    @reyemxela

    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

  10. kriegsman commented on Dec 6, 2018

    @kriegsman
    Member
  11. sabifa commented on May 6, 2019

    @sabifa

    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:

    uint8_t hue = 0;
    
    (..)
    
    void LightFading()
    {
        thishue++;
        fill_rainbow(leds, NUM_PIXELS, hue, 4);
        FastLED.show();
    }
    

    If I delete following code (which is in an entirely different method) it works fine:

    long color = strtol(colorString.c_str(), NULL, 16);
    

    Do you have any idea why this is caused and how I am able to fix the problem?

  12. quakec commented on Oct 12, 2019

    @quakec

    @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

    void fill_rainbow( struct CRGB * pFirstLED, int numToFill,
                      uint8_t initialhue,
                      uint8_t deltahue )
    {
        CHSV hsv;
        hsv.hue = initialhue;
        hsv.val = 255;
        hsv.sat = 240;
        for( int i = 0; i < numToFill; i++) {
            pFirstLED[i] = hsv;
            hsv.hue += deltahue;
        }
    }
    
    void fill_rainbow( struct CHSV * targetArray, int numToFill,
                      uint8_t initialhue,
                      uint8_t deltahue )
    {
        CHSV hsv;
        hsv.hue = initialhue;
        hsv.val = 255;
        hsv.sat = 240;
        for( int i = 0; i < numToFill; i++) {
            targetArray[i] = hsv;
            hsv.hue += deltahue;
        }
    }
    

    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

  13. quakec commented on Oct 12, 2019

    @quakec

    @kriegsman

    Okay further updates, I had modified the sig for the fill_rainbow function to accept a uint8_t sat parameter.

    void fill_rainbow( struct CRGB * pFirstLED, int numToFill,
                      uint8_t initialhue,
                      uint8_t deltahue,
    				  uint8_t sat)
    {
        CHSV hsv;
        hsv.hue = initialhue;
        hsv.val = 255;
    	hsv.sat = sat;
        for( int i = 0; i < numToFill; i++) {
            pFirstLED[i] = hsv;
            hsv.hue += deltahue;
        }
    }
    
    void fill_rainbow( struct CHSV * targetArray, int numToFill,
                      uint8_t initialhue,
                      uint8_t deltahue,
    				  uint8_t sat)
    {
        CHSV hsv;
        hsv.hue = initialhue;
        hsv.val = 255;
        hsv.sat = sat;
        for( int i = 0; i < numToFill; i++) {
            targetArray[i] = hsv;
            hsv.hue += deltahue;
        }
    }
    

    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.

  14. kriegsman commented on Oct 12, 2019

    @kriegsman
    Member

    Thank you for the continued help and investigations. This is still on my list to look into further. Much appreciated.

  15. Hardi-St commented on Nov 6, 2019

    @Hardi-St

    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:

    #include <FastLED.h>
    
    #define DATA_PIN 6
    #define NUM_LEDS 114
    CRGB leds[NUM_LEDS];
    
    uint8_t hue = 59;
    
    void setup() {
      Serial.begin(115200);
      FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
    
      fill_rainbow( leds, 70 /*NUM_LEDS*/, hue, 1);
      for(int i=0; i<NUM_LEDS; i++) {
        Serial.print(i);
        Serial.print(' ');
        Serial.print(leds[i].r);
        Serial.print(' ');
        Serial.print(leds[i].g);
        Serial.print(' ');
        Serial.println(leds[i].b);
      }
      FastLED.show();
      CHSV hsv;
      CRGB rgb;
      //rgb = hsv;
    }
    /*
        6 LEDs 4786 Bytes No Error
       60 LEDs 4786 Bytes No Error
       65 LEDs 4786 Bytes No Error
       67 LEDs 4786 Bytes No Error
       68 LEDs 4786 Bytes No Error
       69 LEDs 4786 Bytes No Error
       70 LEDs 4844 Bytes Error
       75 LEDs 4844 Bytes Error
       96 LEDs 4844 Bytes Error
    */
    
    
    void loop() {
    //  fill_rainbow( leds, NUM_LEDS, hue, 1);
    //  FastLED.show();
    }
    
    

    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

  16. 11 remaining items

  17. kriegsman commented on Dec 8, 2019

    @kriegsman
    Member

    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.

  18. added a commit that references this issue on Dec 20, 2019
  19. OneEyeRick commented on Dec 29, 2020

    @OneEyeRick

    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:

    fill_rainbow( leds, NUM_LEDS, hue, 1);
      FastLED.show();
      hue++;
    delay(30);  //any delay causes red pixel
    

    The following code does not create a red pixel:

    if(millis() > time_now + period){ 
     time_now = millis();
     fill_rainbow( leds, NUM_LEDS, hue, 1);
     FastLED.show();
     hue++;
    }
    

    I don't know if this helps solve the problem. Hopefully someone can use it to get rid of their red pixel.

  20. ddesborough commented on Dec 30, 2020

    @ddesborough

    @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.

  21. added
    type: bug
    area: core
    status: stale
    status: triage
    on Apr 16, 2026
  22. zackees commented on Aug 1, 2026

    @zackees
    Member

    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:

    1. if (Gscale) g = scale8_video_LEAVING_R1_DIRTY(g, Gscale) dirties R1 — then sat == 255 skips the saturation block and val == 255 skips the value block, so nothing cleans up.
    2. Same call, then the sat == 0 branch, which only assigns 255s.
    3. 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.

  23. moved this from Triage to Done in FastLED Trackeron Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: corestatus: stalestatus: triagetype: bug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions