Fade when using Neopixel library

Hi!

I am woking on a project and its going along pretty well. My code seems to work like it should.
The last thing i want to implement is a fade between a registrer of colours, activated by a button.
Is there a smooth way to implement it, to the switch register i have created?

Code under:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define BUTTON_PIN   2 
#define FADEUP_PIN   12
#define FADEDW_PIN   13
#define PIN 4

#define NUM_LEDS 6

#define BRIGHTNESS dimvalue

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;
int delayval = 100;
int dimvalue = 0;
int delayfade = 1500;
int ledFadeTime = 5;
;byte neopix_gamma[] = {
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
    1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,
    2,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  5,  5,  5,
    5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9, 10,
   10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
   17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
   25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
   37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
   51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
   69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
   90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
  115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
  144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
  177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
  215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };

//color presets

uint32_t green = strip.Color(0, 255, 0, 0);
 uint32_t red = strip.Color(255,0, 0, 0);
 uint32_t blue = strip.Color(0, 0, 255, 0);
 uint32_t magenta = strip.Color(255, 0, 254, 0);
 uint32_t white = strip.Color(0, 0, 0, 255); 
 uint32_t all = strip.Color(255, 255, 255, 255); 
 uint32_t ww = strip.Color(0, 20, 00, 255); 
 uint32_t pink = strip.Color(0, 125, 254, 0); 
 uint32_t paleorange = strip.Color(158, 255, 0, 0); 
 uint32_t black = strip.Color(0, 0, 0, 0); 
 

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT);
  pinMode(FADEUP_PIN, INPUT_PULLUP);
  pinMode(FADEDW_PIN, INPUT_PULLUP);
 
  
  strip.setBrightness(BRIGHTNESS);
  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() 
{
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 8)
        showType=0;
      startShow(showType);
    }
  }

  // Set the last button state to the old state.
  oldState = newState;
  
{
  if(digitalRead(FADEDW_PIN) == LOW) dimvalue--;   // Reduce brightness
  if(dimvalue < 0) dimvalue = 0;        // Don't let brightness drop below zero

  if(digitalRead(FADEUP_PIN) == LOW) dimvalue++;   // Increase brightness
  if(dimvalue > 255) dimvalue = 255;    // Don't let brightness get above 255
  
  Serial.println (dimvalue);
  
  delay(10);
}

    strip.setBrightness(BRIGHTNESS);// Set the brightness of the LEDS
    strip.show();//
}

void startShow(int i) {
  switch(i){
    case 0:
    {
    for(int x=0; x<NUM_LEDS; x++)
    
    strip.setPixelColor(x,green);
    strip.show();
    break;}
    
    case 1:
    {
    for(int x=0; x<NUM_LEDS; x++)
    strip.setPixelColor(x,red);
    strip.show();
    break;}
    
    case 2:
    {
    for(int x=0; x<NUM_LEDS; x++)
    strip.setPixelColor(x,blue);
    strip.show();
    break;}
        
    case 3:
    {
    for(int x=0; x<NUM_LEDS; x++)
    strip.setPixelColor(x,white);
    strip.show();
    break;}
        
    case 4:
    {
    for(int x=0; x<NUM_LEDS; x++)
    strip.setPixelColor(x,ww);
    strip.show();
    delay(delayfade);
    break;}
       
   case 5:
   {
    for(int x=0; x<NUM_LEDS; x++)
    strip.setPixelColor(x,pink);
    strip.show();
    break; }  
  
   case 6:
   {
    for(int x=0; x<NUM_LEDS; x++)
    strip.setPixelColor(x,paleorange);
    strip.show();
    break; 
    }  
    
  case 7:
  {
    for(int x=0; x<NUM_LEDS; x++)
    strip.setPixelColor(x,black);
    strip.show();            
    break;}
  }
}

With delay() in it, I doubt it...

But where are you stuck? What should happen? What is happening?

The delay dont need to be there.

I want it to fade from one colour to the next, when the button is activated.

I have a bunch of preset for colours with the uint32_t function.

In the switch register i change from one colour to the next i a loop.

Can i implement a fade when changing colour?

Can i implement a fade when changing colour?

Yes.

Basically you have three colour numbers, RGB, for the start colour and three for the destination. To fade between the two you calculate the difference between each element from start to finish. Note that some of these might be negitave.

Then you calculate an increment value for each colour by dividing the difference number by the number of steps you want to use in the fade. These will be floating point numbers.

Finally you have a for loop for that number of steps, and on each step your new colour value is simply the start colour component plus the increment value times the loop index.