Skip to content

Instantly share code, notes, and snippets.

@ctkjedi
Created December 12, 2018 09:51
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 ctkjedi/e645b2710fd83ea83b111e832c3288f6 to your computer and use it in GitHub Desktop.
Save ctkjedi/e645b2710fd83ea83b111e832c3288f6 to your computer and use it in GitHub Desktop.
Full FastLED tree
#include "FastLED.h"
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define NUM_STRIPS 5
#define NUM_LEDS_PER_STRIP 60
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS NUM_LEDS_PER_STRIP * NUM_STRIPS
#define BRIGHTNESS 255
const uint8_t kMatrixWidth = 16;
const uint8_t kMatrixHeight = 16;
const bool kMatrixSerpentineLayout = true;
CRGB leds[NUM_LEDS];
CRGB horizonLeds[NUM_LEDS];
// ten seconds per color palette makes a good demo
// 20-120 is better for deployment
#define SECONDS_PER_PALETTE 30
unsigned long startMillis;
unsigned long currentMillis;
unsigned long twinkleMillis;
const unsigned long period = 1000; //the value is a number of milliseconds, ie 1 second
void setup() {
delay(3000); // 3 second delay for recovery
FastLED.addLeds<LED_TYPE, 3>(leds, 0, NUM_LEDS_PER_STRIP);
FastLED.addLeds<LED_TYPE, 4>(leds, NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<LED_TYPE, 5>(leds, 2 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<LED_TYPE, 6>(leds, 3 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<LED_TYPE, 7>(leds, 4 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
}
// Helper function that translates from x, y into an index into the LED array
// Handles both 'row order' and 'serpentine' pixel layouts.
uint16_t XY( uint8_t x, uint8_t y)
{
uint16_t i;
if ( kMatrixSerpentineLayout == false) {
i = (y * kMatrixWidth) + x;
} else {
if ( y & 0x01) {
// Odd rows run backwards
uint8_t reverseX = (kMatrixWidth - 1) - x;
i = (y * kMatrixWidth) + reverseX;
} else {
// Even rows run forwards
i = (y * kMatrixWidth) + x;
}
}
return i;
}
// 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()
{
currentMillis = millis();
EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
}
EVERY_N_MILLISECONDS(10) {
nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 16);
}
colorwaves( leds, NUM_LEDS, gCurrentPalette);
FastLED.show();
FastLED.delay(20);
twinkle();
}
// 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);
}
void runRainbow() {
if (currentMillis - startMillis >= 30) {
startMillis = currentMillis;
static uint8_t hue = 0;
fill_rainbow(leds, NUM_LEDS, hue++);
FastLED.show();
}
}
void twinkle() {
//create random twinkle
int rp = random(500, 2000);
if (currentMillis - twinkleMillis >= rp) {
twinkleMillis = currentMillis;
int pixel = random(NUM_LEDS);
leds[random(NUM_LEDS)] = CRGB::White;
FastLED.show();
}
}
void starSparkle() {
fadeToBlackBy (leds, NUM_LEDS, 64);
int rp = random(500, 2000);
if (currentMillis - twinkleMillis >= rp) {
twinkleMillis = currentMillis;
int pixel = random(NUM_LEDS);
leds[random(NUM_LEDS)] = CRGB::White;
}
FastLED.show();
}
DEFINE_GRADIENT_PALETTE( holly_gp) {
0, 0, 255, 0, //green
48, 0, 255, 0, //green
49, 255, 0, 0, //red
64, 255, 0, 0, //red
65, 0, 255, 0, //green
114, 0, 255, 0, //green
115, 255, 0, 0, //red
118, 255, 0, 0, //red
119, 0, 255, 0, //green
168, 0, 255, 0, //green
169, 255, 0, 0, //red
184, 255, 0, 0, //red
185, 0, 255, 0, //green
234, 0, 255, 0, //green
235, 255, 0, 0, //red
255, 255, 0, 0 //red
};
DEFINE_GRADIENT_PALETTE( candycane_gp) {
0 , 128, 128, 128, //white
32 , 128, 128, 128, //white
33 , 255, 0, 0, //red
66 , 255, 0, 0, //red
67 , 128, 128, 128, //white
100 , 128, 128, 128, //white
101 , 255, 0, 0, //red
134 , 255, 0, 0, //red
135 , 128, 128, 128, //white
168 , 128, 128, 128, //white
169 , 255, 0, 0, //red
202 , 255, 0, 0, //red
203 , 128, 128, 128, //white
236 , 128, 128, 128, //white
237 , 255, 0, 0, //red
255 , 255, 0, 0 //red
};
// Gradient palette "humidity_gp", originally from
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/ukmo/wow/tn/humidity.png.index.html
// converted for FastLED with gammas (2.6, 2.2, 2.5)
// Size: 200 bytes of program space.
DEFINE_GRADIENT_PALETTE( humidity_gp ) {
0, 3, 0, 43,
10, 3, 0, 43,
10, 0, 0, 46,
20, 0, 0, 46,
20, 0, 7, 105,
30, 0, 7, 105,
30, 0, 0, 255,
40, 0, 0, 255,
40, 0, 53, 255,
51, 0, 53, 255,
51, 0, 131, 255,
61, 0, 131, 255,
61, 1, 175, 55,
71, 1, 175, 55,
71, 5, 108, 34,
81, 5, 108, 34,
81, 2, 103, 2,
91, 2, 103, 2,
91, 2, 149, 2,
102, 2, 149, 2,
102, 0, 255, 4,
112, 0, 255, 4,
112, 13, 255, 0,
122, 13, 255, 0,
122, 74, 255, 0,
132, 74, 255, 0,
132, 255, 255, 0,
142, 255, 255, 0,
142, 255, 156, 0,
153, 255, 156, 0,
153, 255, 81, 0,
163, 255, 81, 0,
163, 255, 33, 0,
173, 255, 33, 0,
173, 255, 22, 0,
183, 255, 22, 0,
183, 255, 0, 0,
193, 255, 0, 0,
193, 255, 0, 44,
204, 255, 0, 44,
204, 255, 3, 61,
214, 255, 3, 61,
214, 255, 28, 102,
224, 255, 28, 102,
224, 255, 61, 128,
234, 255, 61, 128,
234, 255, 109, 166,
244, 255, 109, 166,
244, 255, 175, 207,
255, 255, 175, 207
};
const TProgmemRGBGradientPalettePtr gGradientPalettes[] = {
holly_gp,
candycane_gp,
humidity_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