The Fader pattern is one of the most commonly requested Neopixel effects on the forums.  This pattern produces a smooth linear fade from one color to another.

Initialization:

Fade() initializes the NeoPattern object to execute the Fade pattern.

The color1 parameter specifies the starting color.

color2 specifies the ending color.

steps specifies how many steps it should take to get from color1 to color2.

interval specifies the number of milliseconds between steps and controls the speed of the fade.

direction allows to to reverse the fade so it goes from color2 to color1.

    // Initialize for a Fade
    void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD)
    {
        ActivePattern = FADE;
        Interval = interval;
        TotalSteps = steps;
        Color1 = color1;
        Color2 = color2;
        Index = 0;
        Direction = dir;
    }

Update:

FadeUpdate() sets all the pixels on the strip to the color corresponding to the next step.

The color is calculated as a linear interpolation between the red, green and blue components of color1 and color2.  To keep it fast, we use all integer math.  To minimize truncation errors, the division is performed last.

As with all the other patterns, the ScannerUpdate calls show() to write to the strip and Increment() to advance the state machine.

// Update the Fade Pattern
    void FadeUpdate()
    {
        uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
        uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
        uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
        ColorSet(Color(red, green, blue));
        show();
        Increment();
    }

This guide was first published on Mar 02, 2015. It was last updated on Mar 02, 2015.

This page (Fader) was last updated on Jan 28, 2015.

Text editor powered by tinymce.