Skip to content

Instantly share code, notes, and snippets.

@chemdoc77
Last active December 31, 2021 03:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemdoc77/fa92780bf2b0598ccda00e4b1b766cbc to your computer and use it in GitHub Desktop.
Save chemdoc77/fa92780bf2b0598ccda00e4b1b766cbc to your computer and use it in GitHub Desktop.
Using Palettes to slow fade to different colors
// This sketch by Chemdoc77 is based on Mark Kriegsman's PaletteCrossfade.ino
// It uses palettes to slow fade to different colors.
// at: https://gist.github.com/kriegsman/1f7ccbbfa492a73c015e
#include <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 24
#define BRIGHTNESS 60
#define LED_TYPE NEOPIXEL
//#define COLOR_ORDER RBG
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 10
// 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 );
CRGBPalette16 targetPalette( CRGBPalette16(CRGB::Red) );
void setup() {
delay(1000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN>(leds, NUM_LEDS);
FastLED.setBrightness( BRIGHTNESS );
FastLED.setMaxPowerInVoltsAndMilliamps(5,1000);
set_max_power_indicator_LED(13);
}
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 = 48;//5; //24;
nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);
static uint8_t startIndex = 0;
startIndex = startIndex + 3;//1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);
// FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness =150; //255
CRGB color=ColorFromPalette( currentPalette, 1, brightness, LINEARBLEND);
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = color;
// colorIndex += 3;
}
}
void ChangePalettePeriodically()
{
uint8_t secondHand = (millis() / 1000) % 60;
static uint8_t lastSecond = 54; //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;
CRGB r = CRGB::Red;
if( secondHand == 0) { targetPalette = CRGBPalette16(CRGB::Red); }
if( secondHand == 12) { targetPalette = CRGBPalette16(CRGB::Yellow);} //Yellow
if( secondHand == 18) { targetPalette = CRGBPalette16(CRGB::Green); }//24
if( secondHand == 30) { targetPalette = CRGBPalette16( CRGB::Blue); }//Blue 36
if( secondHand == 38) { targetPalette =CRGBPalette16(CRGB::Purple); }//48
if( secondHand == 46) { targetPalette = CRGBPalette16( CRGB::Red); }
if( secondHand == 52) { targetPalette = CRGBPalette16( CRGB::Blue); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment