Skip to content

Instantly share code, notes, and snippets.

@danthegoodman1
Created January 26, 2018 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danthegoodman1/1954a7c1479ad3f46126585a29bca30c to your computer and use it in GitHub Desktop.
Save danthegoodman1/1954a7c1479ad3f46126585a29bca30c to your computer and use it in GitHub Desktop.
#include "FastLED.h"
// ColorWavesWithPalettes
// Animated shifting color waves, with several cross-fading color palettes.
// by Mark Kriegsman, August 2015
//
// Color palettes courtesy of cpt-city and its contributors:
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/
//
// Color palettes converted for FastLED using "PaletteKnife" v1:
// http://fastled.io/tools/paletteknife/
//
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define DATA_PIN 6
//#define CLK_PIN 4
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 300
#define BRIGHTNESS 255
CRGB leds[NUM_LEDS];
// ten seconds per color palette makes a good demo
// 20-120 is better for deployment
#define SECONDS_PER_PALETTE 15
void setup() {
delay(1000); // 3 second delay for recovery
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
//.setCorrection(TypicalLEDStrip) // cpt-city palettes have different color balance
.setDither(BRIGHTNESS < 255);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
}
// Forward declarations of an array of cpt-city gradient palettes, and
// a count of how many there are. The actual color palette definitions
// are at the bottom of this file.
extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
extern const uint8_t gGradientPaletteCount;
// Current palette number from the 'playlist' of color palettes
uint8_t gCurrentPaletteNumber = 0;
CRGBPalette16 gCurrentPalette( CRGB::Black);
CRGBPalette16 gTargetPalette( gGradientPalettes[0] );
void loop()
{
EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
}
EVERY_N_MILLISECONDS(40) {
nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 16);
}
colorwaves( leds, NUM_LEDS, gCurrentPalette);
FastLED.show();
FastLED.delay(0);
}
// This function draws color waves with an ever-changing,
// widely-varying set of parameters, using a color palette.
void colorwaves( CRGB* ledarray, uint16_t numleds, CRGBPalette16& palette)
{
static uint16_t sPseudotime = 0;
static uint16_t sLastMillis = 0;
static uint16_t sHue16 = 0;
uint8_t sat8 = beatsin88( 87, 220, 250);
uint8_t brightdepth = beatsin88( 341, 96, 224);
uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
uint8_t msmultiplier = beatsin88(147, 23, 60);
uint16_t hue16 = sHue16;//gHue * 256;
uint16_t hueinc16 = beatsin88(113, 300, 1500);
uint16_t ms = millis();
uint16_t deltams = ms - sLastMillis ;
sLastMillis = ms;
sPseudotime += deltams * msmultiplier;
sHue16 += deltams * beatsin88( 400, 5,9);
uint16_t brightnesstheta16 = sPseudotime;
for( uint16_t i = 0 ; i < numleds; i++) {
hue16 += hueinc16;
uint8_t hue8 = hue16 / 256;
uint16_t h16_128 = hue16 >> 7;
if( h16_128 & 0x100) {
hue8 = 255 - (h16_128 >> 1);
} else {
hue8 = h16_128 >> 1;
}
brightnesstheta16 += brightnessthetainc16;
uint16_t b16 = sin16( brightnesstheta16 ) + 32768;
uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
bri8 += (255 - brightdepth);
uint8_t index = hue8;
//index = triwave8( index);
index = scale8( index, 240);
CRGB newcolor = ColorFromPalette( palette, index, bri8);
uint16_t pixelnumber = i;
pixelnumber = (numleds-1) - pixelnumber;
nblend( ledarray[pixelnumber], newcolor, 128);
}
}
// Alternate rendering function just scrolls the current palette
// across the defined LED strip.
void palettetest( CRGB* ledarray, uint16_t numleds, const CRGBPalette16& gCurrentPalette)
{
static uint8_t startindex = 0;
startindex--;
fill_palette( ledarray, numleds, startindex, (256 / NUM_LEDS) + 1, gCurrentPalette, 255, LINEARBLEND);
}
DEFINE_GRADIENT_PALETTE( es_emerald_dragon_08_gp ) {
0, 97,255, 1,
101, 47,133, 1,
178, 13, 43, 1,
255, 2, 10, 1};
DEFINE_GRADIENT_PALETTE( es_ocean_breeze_001_gp ) {
0, 139,152,142,
127, 1, 74,109,
130, 1, 72,105,
255, 1, 1, 1};
DEFINE_GRADIENT_PALETTE( es_seadreams_11_gp ) {
0, 1, 1,240,
255, 1,239,230};
DEFINE_GRADIENT_PALETTE( es_seadreams_12_gp ) {
0, 1, 1, 4,
68, 1, 14, 21,
140, 17,139,180,
178, 137,235,233,
219, 184,248,247,
255, 244,233,230};
DEFINE_GRADIENT_PALETTE( es_skywalker_19_gp ) {
0, 2, 40,127,
255, 1, 1, 7};
DEFINE_GRADIENT_PALETTE( es_skywalker_06_gp ) {
0, 6, 29, 41,
76, 1, 17, 38,
153, 1, 9, 21,
255, 0, 1, 1};
DEFINE_GRADIENT_PALETTE( es_ocean_breeze_025_gp ) {
0, 52, 78, 71,
76, 0, 16, 29,
127, 1, 1, 1,
192, 1, 6, 9,
255, 0, 16, 29};
// Gradient palette "es_ocean_breeze_030_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/es/ocean_breeze/tn/es_ocean_breeze_030.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 16 bytes of program space.
DEFINE_GRADIENT_PALETTE( es_ocean_breeze_030_gp ) {
0, 1, 1, 1,
63, 0, 16, 29,
127, 43, 88,106,
255, 0, 16, 29};
// Gradient palette "es_ocean_breeze_036_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/es/ocean_breeze/tn/es_ocean_breeze_036.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 16 bytes of program space.
DEFINE_GRADIENT_PALETTE( es_ocean_breeze_036_gp ) {
0, 1, 6, 7,
89, 1, 99,111,
153, 144,209,255,
255, 0, 73, 82};
// Gradient palette "es_ocean_breeze_091_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/es/ocean_breeze/tn/es_ocean_breeze_091.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 24 bytes of program space.
DEFINE_GRADIENT_PALETTE( es_ocean_breeze_091_gp ) {
0, 43, 88,106,
76, 1, 26, 88,
127, 0, 11, 41,
130, 43, 88,106,
216, 1, 26, 88,
255, 1, 15, 46};
// Gradient palette "es_emerald_dragon_01_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/es/emerald_dragon/tn/es_emerald_dragon_01.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 20 bytes of program space.
DEFINE_GRADIENT_PALETTE( es_emerald_dragon_01_gp ) {
0, 1, 1, 1,
79, 1, 19, 7,
130, 1, 59, 25,
229, 28,255,255,
255, 28,255,255};
DEFINE_GRADIENT_PALETTE( es_skywalker_15_gp ) {
0, 29, 91,140,
124, 0, 14, 46,
229, 0, 1, 1,
255, 0, 1, 1};
DEFINE_GRADIENT_PALETTE( es_ocean_breeze_099_gp ) {
0, 51,156,137,
51, 0,103, 89,
127, 1, 33, 29,
128, 27,104,106,
130, 150,219,247,
255, 2, 65, 62};
DEFINE_GRADIENT_PALETTE( es_autumn_09_gp ) {
0, 82, 90, 18,
76, 186, 13, 7,
140, 252, 1, 4,
244, 91, 78, 16,
255, 91, 78, 16};
DEFINE_GRADIENT_PALETTE( es_landscape_62_gp ) {
0, 123,166, 1,
127, 1, 13, 1,
130, 92,180,245,
255, 1, 39,108};
DEFINE_GRADIENT_PALETTE( es_landscape_69_gp ) {
0, 71, 34, 3,
38, 132,101, 9,
127, 182,169,130,
130, 45,144,151,
255, 1, 1, 1};
DEFINE_GRADIENT_PALETTE( es_rosa_67_gp ) {
0, 139, 1, 13,
63, 146, 7, 29,
127, 155, 30, 55,
191, 179, 91,111,
255, 182,180,176};
DEFINE_GRADIENT_PALETTE( es_rosa_52_gp ) {
0, 15, 0, 5,
76, 15, 0, 5,
204, 60,115, 1,
255, 118,157, 1};
const TProgmemRGBGradientPalettePtr gGradientPalettes[] = {
es_skywalker_15_gp,
es_ocean_breeze_091_gp,
es_seadreams_12_gp,
es_landscape_62_gp,
es_rosa_52_gp,
es_ocean_breeze_091_gp,
es_landscape_69_gp,
es_ocean_breeze_099_gp,
es_rosa_67_gp,
es_autumn_09_gp,
es_ocean_breeze_091_gp,
es_emerald_dragon_01_gp,
};
// Count of how many cpt-city gradients are defined:
const uint8_t gGradientPaletteCount =
sizeof( gGradientPalettes) / sizeof( TProgmemRGBGradientPalettePtr );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment