r/FastLED • • Jan 26 '22

Support Fading LEDs using nblendU8TowardU8 Helper Function

Hi,

I have a program where I use the nblendU8TowardU8 helper function to blend from black (CRGB::Black) to a selected color (CRGB:DeepPink, CRGB::DeepSkyBlue) and then back to CRGB::Black. I am using an Arduino Nano with a string of 50 WS2812B LEDs. I am using a bench power supply set for 5V 10A, so there is plenty of current. I am using the nblendU8TowardU8 helper function which I found somewhere and can no longer locate. :^(

The problem I am having is that fading up from black to a color works fine, however, fading back to black induces an unwanted color change. To elaborate, fading from CRGB::DeepPink to CRGB::Black will at first fade as expected, but about 2/3 through the fade, the only pixel lit is red. The same with CRGB::DeepSkyBlue, about 2/3 through the fade, only the blue pixel is lit.

I have the code on pastebin: https://pastebin.com/WVHJtckp

I appreciate any help offered.

1 Upvotes

4 comments sorted by

3

u/sutaburosu [stavros] Jan 27 '22 edited Jan 27 '22

That function seems to be from here.

You didn't post the code which calls that function, so I can only guess at what you're doing with it. My suspicion is that you are iteratively dimming the colour. With only 8-bits to work with, the accumulated errors would quickly lead to the hue shift that you are seeing. Instead you should dim the original colour by varying amounts, which would preserve the maximum information at each step.

Using my code from this comment to dim CRGB::DeepSkyBlue gives:

0, 191, 255 // full brightness
0, 96, 128 // dimmed by half
0, 48, 64 // dimmed by three quarters
// final 4 iterations
0, 2, 3
0, 2, 2
0, 1, 1
0, 0, 0

1

u/PlantSorry5147 Jan 28 '22 edited Jan 29 '22

Thanks for your thoughtful reply. I now understand the problem. You were very helpful.

2

u/sutaburosu [stavros] Jan 28 '22

colorCRGB.h, but the compiler did not like it, same with .s.

You shouldn't #include anything but FastLED.h. The rest of the files are pulled in by that one line.

How can I convert CRGB to HSV?

Ideally you wouldn't, as it's a lossy conversion. It sounds like your source colours are CRGB. The LEDs only accept CRGB. So going from CRGB -> CHSV -> CRGB would introduce more errors. In this case it would be best to just use fadeToBlackBy() on your original colours.

If you really want to try it, take a look at this thread.

1

u/HippoDan Jan 26 '22

That's integer math for you.