Shop OBEX P1 Docs P2 Docs Learn Events
WS2812B RGB - Fade Color — Parallax Forums

WS2812B RGB - Fade Color

I'm trying to fade from one color to another with a WS2812B RGB strip.
Working in the Simple IDE compiler so if possible I need it in C Code.
I can turn the RGBs on/off/set the colors... Now I want to fade from one color to another.
Anyone got some C code to smoothly fade from one color to another?

Looking for a procedure like this:
void Color_Fade(long _Start_Color, long _End_Color, int speedMS)

Here is example declarations for the color that I found in the Elev-8 flight controller.

// WS2812B RGB LED Module Variable Definitions
const int Extra_LED_Module_Count = 8; // Extra WS2812B RGB LED Module Count (Do not count the onboard WS2812B Module)
const int LED_COUNT = Extra_LED_Module_Count + 1; // + Onboard WS2812B
static long LEDValue[LED_COUNT]; // LED Outputs (Copied to the LEDs by the Sensors cog)
static long LEDModeColor; // Used to Set the LED Mode Color
const int FC_LED_Module = 0; // Flight Controller WS2812B RBG LED Module Index

//LED Brightness values - AND with color values to dim them
const int LED_Full = 0xffffff;
const int LED_Half = 0x7f7f7f;
const int LED_Quarter = 0x3f3f3f;
const int LED_Eighth = 0x1f1f1f;
const int LED_Dim = 0x0f0f0f;

//LED Color values
const int LED_Off = 0x000000;
const int LED_Red = 0x00FF00;
const int LED_Green = 0xFF0000;
const int LED_Blue = 0x0000FF;
const int LED_White = 0xFFFFFF;
const int LED_Yellow = LED_Red | LED_Green;
const int LED_Violet = LED_Red | LED_Blue;
const int LED_Cyan = LED_Blue | LED_Green;
const int LED_DimCyan = (LED_Blue | LED_Green) & LED_Half;
const int LED_DimWhite = LED_White & LED_Half;

// Used to attenuate the brightness of the LEDs, if desired. A shift of zero is full brightness
const int LEDBrightShift = 0;
const int LEDSingleMask = 0xFF - ((1<<LEDBrightShift)-1);
const int LEDBrightMask = LEDSingleMask | (LEDSingleMask << 8) | (LEDSingleMask << 16);

Comments

  • Refer to this in the Propellar forum.
    https://forums.parallax.com/post/quote/168435/Discussion_168435
    It will be in Spin but ther is conversion software to make it "C"
    Jim
  • While this is not the same setup you have the values returned by this C code should be valid.

    This code is for individual RED, GREEN, and BLUE LED.

    How to use a RGB LED

    Mike
  • JonnyMacJonnyMac Posts: 9,104
    edited 2018-05-10 17:10
    I only use C when I'm forced to use the Schmarschmino, so I tested this code -- which are converted from methods in my advanced pixel library -- in the Arduino compiler.

    The morph() function will let you transition from one color to another. Put this into a timed loop.
    uint32_t color(uint8_t r, uint8_t g, uint8_t b, uint8_t w)
    {
      return (r << 24) | (g << 16) | (b << 8) | w;
    }
    
    uint32_t scale_rgbw(uint32_t rgbw, uint8_t level)
    {
      if (level == 0) {
        return 0x00000000;
      }
      else if (level == 255) {
        return rgbw;
      }
      else {
        uint8_t r = ((rgbw >> 24) & 0xFF) * level / 255;
        uint8_t g = ((rgbw >> 16) & 0xFF) * level / 255;
        uint8_t b = ((rgbw >>  8) & 0xFF) * level / 255;
        uint8_t w = ((rgbw) & 0xFF)       * level / 255;
        return color(r, g, b, w);
      }
    }
    
    uint32_t morph(uint32_t c1, uint32_t c2, uint8_t phase)
    {
      if (phase == 0) {
        return c1;
      }
      else if (phase == 255) {
        return c2;
      }
      else {
        c1 = scale_rgbw(c1, phase);
        c2 = scale_rgbw(c2, 255-phase);
        return c1 + c2;
      }
    }
    
Sign In or Register to comment.