Hello, I need some help on a project I'm working on.

Hello,

I need some help on a project I’m working on. I would like to have a add the Sparks() animation over a solid colour. That way, the fadetoblackby() seems to not work as expected and I have like a glitter effect instead of the Sparks when I’m doing this (only partial code but maybe enough to understand the issue) :

void Sparks(int16_t chanceOfGlitter, uint8_t fadeSpd, int16_t bright = _BRIGHT)
{
fract8 density = (fract8)chanceOfGlitter;

EVERY_N_MILLISECONDS(10)
{
    fadeToBlackBy( fLEDS, nLEDS, fadeSpd);
    
    if( random8() < density)
        fLEDS[ random16(nLEDS) ] += CHSV( 0, 0, bright);
}

}

loop()
{
fill_solid( fLEDS, nLEDS, CHSV( _HUE, _SAT, _BRIGHT) );
Sparks(_DENSITY, _FADESPD, 255);
LEDS.show();
}

I thought to maybe change the fade to black to a blend, but I’m not sure that the way of doing is the good one.

Thanks a lot!

I wrote code for this just recently- rather than call fadeToBlack, each iteration take a proportion of the difference between white and the background colour off of every pixel.

This is excerpts from the code i have:

colour is a CRGB type containing the “background colour”

CRGB diff;
uint8_t step;

init() {
/** Using time, and white and background colour, work out size of fade for each loop increment /
diff = CRGB(CRGB::White) - colour;
int iterations = 20; // number of times aroudn the loop to change from white to colour

step = 256/iterations; // step size for fast math

}
void update( ) {
  // modified version of fadeUsingColor(strip, length, colour); to stop fading at original colour

  uint8_t fr, fg, fb;
  fr = colour.r;
  fg = colour.g;
  fb = colour.b;

  for ( uint16_t i = 0; i < length; i++) {
    
    leds[i].r = max( leds[i].r - scale8(diff.r,  step), fr);
    leds[i].g = max( leds[i].g - scale8(diff.g,  step), fg);
    leds[i].b = max( leds[i].b - scale8(diff.b,  step), fb);
  }


  // put the glitter bit here
}

Thanks for your comment. I tried and I also had a look into FastLed library. I tried both of them, but I’m only still getting glitter without any fadeout to Color…

Here the code based on your comment :

void fadeToColor( CRGB* leds, uint16_t numLeds, const CRGB& color, const uint8_t iter)
{
// modified version of fadeUsingColor(strip, length, colour); to stop fading at original colour

uint8_t fr, fg, fb;
fr = color.r;
fg = color.g;
fb = color.b;
uint8_t step;

CRGB diff = CRGB(CRGB::White) - color;
step = 256/iter;

for ( uint16_t i = 0; i < numLeds; i++) {
	
	leds[i].r = max( leds[i].r - scale8(diff.r,  step), fr);
	leds[i].g = max( leds[i].g - scale8(diff.g,  step), fg);
	leds[i].b = max( leds[i].b - scale8(diff.b,  step), fb);
}

}

void Sparks(int16_t chanceOfGlitter, uint8_t fadeSpd, int16_t bright = _BRIGHT, CRGB toColor = CRGB::Black)
{
fract8 density = (fract8)chanceOfGlitter;

EVERY_N_MILLISECONDS(25)
{
	fadeToColor( fLEDS, nLEDS, toColor, 255);

// Glitter
if( random8() < density)
fLEDS[ random16(nLEDS) ] += CHSV( 0, 0, bright);
}
}

I think that if you change
fadeToColor( fLEDS, nLEDS, toColor, 255);
To
fadeToColor( fLEDS, nLEDS, toColor, 10);
I expect it should work for you.

Play with that last argument to adjust the rate of fade.

Right, it does work indeed. Thanks a lot :wink:
I do not understand now why if I set the color to black the effect is less smooth than using fadeToBlackBy (I presume that is due to the calculation on 3 channel instead of 1 - brightness). Thanks a lot !