Skip to content

Instantly share code, notes, and snippets.

@kriegsman
Last active March 2, 2024 01:37
Show Gist options
  • Save kriegsman/1f7ccbbfa492a73c015e to your computer and use it in GitHub Desktop.
Save kriegsman/1f7ccbbfa492a73c015e to your computer and use it in GitHub Desktop.
How to cross-fade between color palettes using nblendPaletteTowardPalette
#include <FastLED.h>
#define LED_PIN 3
#define NUM_LEDS 30
#define BRIGHTNESS 96
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
// This example shows how to cross-fade between different color palettes
// using the function nblendPaletteTowardPalette.
//
// The basic idea is that you always have a "current palette" that you're
// pulling colors from with ColorFromPalette, and you have a "target palette"
// which is the 'next' palette that you want to get to.
//
// After that, implementation is relatively simple: just periodically call
// nblendPaletteTowardPalette( currentPalette, targetPalette);
// If the current palette is not yet equal to the target palette, this
// function will make a few small changes to the current palette to make
// it slightly more like the target. Over time, the current palette will
// come to be equal to the target.
// There's no need to test the current and target for equality; it's safe
// to keep calling nblendPaletteTowardPalette even after current reaches target.
// For faster blending, call nblendPaletteTowardPalette twice per loop.
CRGBPalette16 currentPalette( CRGB::Black);
CRGBPalette16 targetPalette( PartyColors_p );
void setup() {
delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
}
void loop()
{
ChangePalettePeriodically();
// Crossfade current palette slowly toward the target palette
//
// Each time that nblendPaletteTowardPalette is called, small changes
// are made to currentPalette to bring it closer to matching targetPalette.
// You can control how many changes are made in each call:
// - the default of 24 is a good balance
// - meaningful values are 1-48. 1=veeeeeeeery slow, 48=quickest
// - "0" means do not change the currentPalette at all; freeze
uint8_t maxChanges = 24;
nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex + sin8(i*16), brightness);
colorIndex += 3;
}
}
void ChangePalettePeriodically()
{
uint8_t secondHand = (millis() / 1000) % 60;
static uint8_t lastSecond = 99;
if( lastSecond != secondHand) {
lastSecond = secondHand;
CRGB p = CHSV( HUE_PURPLE, 255, 255);
CRGB g = CHSV( HUE_GREEN, 255, 255);
CRGB b = CRGB::Black;
CRGB w = CRGB::White;
if( secondHand == 0) { targetPalette = RainbowColors_p; }
if( secondHand == 10) { targetPalette = CRGBPalette16( g,g,b,b, p,p,b,b, g,g,b,b, p,p,b,b); }
if( secondHand == 20) { targetPalette = CRGBPalette16( b,b,b,w, b,b,b,w, b,b,b,w, b,b,b,w); }
if( secondHand == 30) { targetPalette = LavaColors_p; }
if( secondHand == 40) { targetPalette = CloudColors_p; }
if( secondHand == 50) { targetPalette = PartyColors_p; }
}
}
@koraysels
Copy link

CRGB p = CHSV( HUE_PURPLE, 255, 255); CRGB g = CHSV( HUE_GREEN, 255, 255); CRGB b = CRGB::Black; CRGB w = CRGB::White;

Why is this needed ?

@ronnyandre
Copy link

ronnyandre commented Jun 6, 2018

@koraysels Better readability on line 90 and 91 when you are making the palette.

@oldnewledguy
Copy link

how would i add this code to an existing program..

#include PaletteCrossFade.h

and save it the same folder?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment