0

A better fader algorithm?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

A better fader algorithm?

by splatten on Tue Oct 10, 2017 3:16 pm

I've got a fader working but I'm not terribly happy with it....if I fade from 0xff2100 (on neopixels this is very orange) to black, as it gets closer to black it turns more and more red....what I really want is for the colour to stay as close as possible to the original colour and fade away....I can't use brightness as I need to fade individual pixels.

Here is the algorithm I'm currently using:

Code: Select all | TOGGLE FULL SIZE
//Iterate through each pixel checking if it needs fading
      for( uint16_t f=0; f<numPixels(); f++ ) {
//Look for pixels that require fading
        faderDef* pobjFader = &mparyFaderInfo[f];

        if ( pobjFader->blnUsed == true ) {
//Extract the individual colour components       
          uint8_t uint8CB, uint8CG, uint8CR
                 ,uint8EB = uint8GetCompB(muint32FadeEnd)
                 ,uint8EG = uint8GetCompG(muint32FadeEnd)
                 ,uint8ER = uint8GetCompR(muint32FadeEnd)
                 ,uint8SB = uint8GetCompB(muint32FadeStart)     
                 ,uint8SG = uint8GetCompG(muint32FadeStart)
                 ,uint8SR = uint8GetCompR(muint32FadeStart);
          if ( pobjFader->uint8Step >= FADER_ITERATIONS ) {
            pobjFader->blnUsed = false;
            continue;       
          }
          uint8CB = uint8SB + (uint8EB - uint8SB)
                    * pobjFader->uint8Step / FADER_ITERATIONS;
          uint8CG = uint8SG + (uint8EG - uint8SG)
                    * pobjFader->uint8Step / FADER_ITERATIONS;
          uint8CR = uint8SR + (uint8ER - uint8SR)
                    * pobjFader->uint8Step / FADER_ITERATIONS;
//Increment step                   
          pobjFader->uint8Step++;                   
          uint32_t uint32RGB = uint32tEncodeRGB(uint8CR, uint8CG, uint8CB);     
          setPixelColor(pobjFader->uint16Pixel, uint32RGB);   
        }
      }


Hopefully this will give all the information.

splatten
 
Posts: 399
Joined: Mon Mar 17, 2014 3:08 pm
Location: United Kingdom, Norwich

Re: A better fader algorithm?

by kcl1s on Tue Oct 10, 2017 3:50 pm

You might want to try the FastLED library http://fastled.io/

It has a built in fadeToBlackBy function that may work. It also as the ability to work with HSV (Hue, Saturation, Value/brightness) colors where you can just vary the V to fade.

I still use the Adafruit Neopixel library as my goto library but the FastLED has some good features.

Keith

kcl1s
 
Posts: 351
Joined: Tue Aug 30, 2016 12:06 pm

Re: A better fader algorithm?

by splatten on Tue Oct 10, 2017 3:57 pm

Thank you, I will look into it.

splatten
 
Posts: 399
Joined: Mon Mar 17, 2014 3:08 pm
Location: United Kingdom, Norwich

Re: A better fader algorithm?

by caitlinsdad on Wed Oct 11, 2017 5:42 pm

Just info if you are interested: You might be running into that problem where eyes don't see colors you expect correlated to the values you are coding in. This is compensated for with "gamma correction". You'll see gamma correction code implemented in most of the Trinket/Gemma neopixel projects and I recall something in the Adafruit Learn on it. Look at the code for Larson Scanner or the Cylon eyes to see how it fades the nearby neopixels to make it appear to go to black. Good luck.

caitlinsdad
 
Posts: 393
Joined: Fri Feb 24, 2012 4:23 pm

Re: A better fader algorithm?

by adafruit_support_bill on Wed Oct 11, 2017 6:17 pm

Here is the guide on Gamma Correction: https://learn.adafruit.com/led-tricks-g ... /the-issue

adafruit_support_bill
 
Posts: 62289
Joined: Sat Feb 07, 2009 10:11 am

Re: A better fader algorithm?

by splatten on Thu Oct 12, 2017 2:00 am

Thank you, something for the weekend.

splatten
 
Posts: 399
Joined: Mon Mar 17, 2014 3:08 pm
Location: United Kingdom, Norwich

Please be positive and constructive with your questions and comments.