I need an advice for my Neopixel Code - fading

Hello.
I’m working on my project with neopixel and I would like to make my effect to fade in and out.
Right now I have sparkle effect on my stripe.
I want to make this sparkle to fade in and out like following loop.

Fade in sparkle 0% → 100% (10sec)
Sparkle 100% (1min)
Sparkle fade out 100% → 0% (10sec)
0% (5min)

This is sparkle effect code on my stripe.

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 144
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(144, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}


void loop() { 
   Sparkle(0xff, 0xff, 0xff, 0);
}


void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}


void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,red,green,blue);
  showStrip();
  delay(SpeedDelay);
  setPixel(Pixel,0,0,0);

}
  void  fadeOut (uint8_t wait) {  
  uint16_t i, j;  
  
  for (j = 254 ; j> 0 ; j--) {  
    for (i = 0 ; i <strip.numPixels (); i ++) {  
      strip.setBrightness (i + j);  
    }  
    strip.show ();  
    delay (wait);  
  }  
 blackout();  
 }  
    
  void  blackout () {  
    strip.setPixelColor ( 0 ,  0 ,  0 ,  0 );  
    strip.setPixelColor ( 1 ,  0 ,  0 ,  0 );  
    strip.setBrightness ( 255 );  
    strip.show ();  
  }

Thank you

Download the FastLED library and use that instead of the NeoPixel library. The FastLED library comes with many examples that you can leverage.

Thank you so much for your suggestion.
I've been checking Fast LED library and found interesting stuff-
But I still don't get how I can fade in and out my sparkly effect-
If you have any suggestion...
Thank you!

blh64:
Download the FastLED library and use that instead of the NeoPixel library. The FastLED library comes with many examples that you can leverage.

It does, but there are also (many) disadvantages to using that library.

void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,red,green,blue);
  showStrip();
  delay(SpeedDelay);
  setPixel(Pixel,0,0,0);
}

as a sparkle function can be called from the fade loop

void  fadeOut (int wait) {
  for (j = 254 ; j> 0 ; j--) {  
    for (i = 0 ; i <strip.numPixels (); i ++) {  
      strip.setBrightness (i + j);
      Sparkle(0xff, 0xff, 0xff, wait);      
    } 
  }
}

and from loop call

void loop() { 
   fadeOut(20); // a little delay should show you better what is happening
}

Thank You so much Deva_Rishi! I really appreciate your kind advice!
I tried your code, and now the strip seems doing bit like a breathing-
It’s doing weak sparkle to strong sparkle every 4 seconds. ( but it’s like weak → fading to strong, weak ->fading to strong loop)

I donno how I can add a scene like following
1: 0% brightness sparkle → fading 5 seconds to 100%
2: 100% sparkle stays for 10seconds
3: 100% sparkle → fade out 5seconds to 0% brightness sparkle
4: 10seconds black out (silent)

This is the code right now adopted to your suggestion!

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 144
Adafruit_NeoPixel strip = Adafruit_NeoPixel(144, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}


void loop() { 
   Sparkle(0xff, 0xff, 0xff, 0);
   fadeOut(20);
   delay(500);
}


void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}


void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,red,green,blue);
  showStrip();
  delay(SpeedDelay);
  setPixel(Pixel,0,0,0);

}

void  fadeOut (int wait) {
  int i, j;
  for (j = 254 ; j> 0 ; j--) {  
    for (i = 0 ; i <strip.numPixels (); i ++) {  
      strip.setBrightness (i + j);
      Sparkle(0xff, 0xff, 0xff, wait);      
    } 
  }
  }

Thank you!

there are also (many) disadvantages to using that library.

Such as ?

UKHeliBob:
Such as ?

First of all, the pixels are stored in an Array which has to be separately declared outside of the class, and which results in users overrunning the size, (it happens often i code that gets posted) since there is no internal error check for it like there is for the neopixel.setPixelColor() (I really think that if you are going to make a library like that you should have the pixelbuffer as part of the class anyhow).
Secondly, the library comes with all these nice functions for very simple patterns, which from an educational point of view stops users from inventing (the very simple ones are usually a good start) or investigating how these patterns are created, but mostly all those patterns are created in a 'blocking' function, which here on the forum is discouraged.
On top of that all those functions use the FastLED.delay() function which is just ludicrous ! since it uses millis() to continuously send a LED-signal until it times out, but since interrupts get turned off for sending the signal (as neopixel does as well bitbanging with such precision just can't be interrupted) this results in interrupts being turned off for nearly all the time a pattern is played.
So the name may suggest that FastLED is fast, but it is no faster than neopixel, and in my opinion unless the chip is not supported in neopixel, there is to need to use FastLED.

NSK100:
Thank You so much Deva_Rishi! I really appreciate your kind advice!
I tried your code, and now the strip seems doing bit like a breathing-
It's doing weak sparkle to strong sparkle every 4 seconds. ( but it's like weak -> fading to strong, weak ->fading to strong loop)

I donno how I can add a scene like following
1: 0% brightness sparkle -> fading 5 seconds to 100%
2: 100% sparkle stays for 10seconds
3: 100% sparkle -> fade out 5seconds to 0% brightness sparkle
4: 10seconds black out (silent)

oh eh a made a small error in the fadeout function, the nested loop is not required

void  fadeOut (int wait) {
  for (j = 255 ; j> 0 ; j--) {  
      strip.setBrightness (j);
      Sparkle(0xff, 0xff, 0xff, wait);      
  }
}

this should just create the fadeOut() , then i figured you'd work out how to do a fadeIn() (by reversing the direction of the loop)
and how you can call sparkle for 10 seconds using millis() for timing before you can continue with the fade out, and how to do nothing for some time, well, i don't really want to get into that.

the length of the fade is now defined by the time it takes to do the sparkle function X the length of the loop (so as it is sparkle has a wait of (20ms + time it takes to send the signal 1-2 ms ) * 255 = approx 10200ms which is about 10 seconds. but if you want to do it more accurately (and if you want the time a sparkle lights to be separated from the fade time) you can use millis() within you fade function by either blocking the loop until the time has passed, or (even better) set the total fade time and determine how far faded you strip should be by dividing the elapsed time by total-time and multiply that by the range of the brightness (best is actually to do it like this

uint8_t br=( elapsed*255) / fadetime;
strip.setBrightness (br);

multiply first and then divide)