Skip to content

Instantly share code, notes, and snippets.

@adam19691026
Created June 21, 2015 14:27
Show Gist options
  • Save adam19691026/6427957e0e39fffa800a to your computer and use it in GitHub Desktop.
Save adam19691026/6427957e0e39fffa800a to your computer and use it in GitHub Desktop.
#include "FastLED.h"
#define LED_PIN 6
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 64
CRGB leds[NUM_LEDS];
// Twinkling 'holiday' lights that fade up and down in brightness.
// Colors are chosen from a palette; a few palettes are provided.
//
// The basic operation is that all pixels stay black until they
// are 'seeded' with a relatively dim color. The dim colors
// are repeatedly brightened until they reach full brightness, then
// are darkened repeatedly until they are fully black again.
//
// A set of 'directionFlags' is used to track whether a given
// pixel is presently brightening up or darkening down.
//
// For illustration purposes, two implementations of directionFlags
// are provided: a simple one-byte-per-pixel flag, and a more
// complicated, more compact one-BIT-per-pixel flag.
//
// Darkening colors accurately is relatively easy: scale down the
// existing color channel values. Brightening colors is a bit more
// error prone, as there's some loss of precision. If your colors
// aren't coming our 'right' at full brightness, try increasing the
// STARTING_BRIGHTNESS value.
//
// -Mark Kriegsman, December 2014
#define MASTER_BRIGHTNESS 80
#define STARTING_BRIGHTNESS 64
#define FADE_IN_SPEED 50
#define FADE_OUT_SPEED 5
#define DENSITY 400
void setup() {
delay(3000);
FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(MASTER_BRIGHTNESS);
}
void loop()
{
chooseColorPalette();
colortwinkles();
FastLED.show();
FastLED.delay(50);
}
CRGBPalette16 gPalette;
void chooseColorPalette()
{
uint8_t numberOfPalettes = 15;
uint8_t secondsPerPalette = 10;
uint8_t whichPalette = (millis()/(1000*secondsPerPalette)) % numberOfPalettes;
CRGB r(CRGB::Red), b(CRGB::Blue), w(85,85,85), g(CRGB::Green), W(CRGB::White), l(0xE1A024) , p(CRGB::HotPink) , P(CRGB::Purple) , y(CRGB::Yellow), a(CRGB::Aqua) , o(CRGB::Orange),
m(0x800000) , d(0x00CED1), A(0x9966CC) , L(0x00FF00);
switch( whichPalette) {
case 0: // Red, Green, and White
gPalette = CRGBPalette16( r,r,r,r, r,r,r,r, g,g,g,g, w,w,w,w );
break;
case 1: // Blue and White
//gPalette = CRGBPalette16( b,b,b,b, b,b,b,b, w,w,w,w, w,w,w,w );
gPalette = CloudColors_p; // Blues and whites!
break;
case 2: // Rainbow of colors
gPalette = RainbowColors_p;
break;
case 3: // Incandescent "fairy lights"
gPalette = CRGBPalette16( l,l,l,l, l,l,l,l, l,l,l,l, l,l,l,l );
break;
case 4: // Snow
gPalette = CRGBPalette16( W,W,W,W, w,w,w,w, w,w,w,w, w,w,w,w );
break;
case 5: //DAD 2015
gPalette = CRGBPalette16( o,o,o,o, r,r,r,r, b,b,b,b, P,P,P,P );
break;
case 6: // DAD 2015 Red white and blue all the girls love you
gPalette = CRGBPalette16( r,r,r,r, W,W,W,W, b,b,b,b, w,w,w,w );
break;
case 7: //FIRE
gPalette = CRGBPalette16( r,r,r,r, W,W,W,W, y,y,y,y, l,l,l,l );
break;
case 8: // Rainbowstripe of colors
gPalette = RainbowStripeColors_p;
break;
case 9: // Ocean
gPalette = OceanColors_p;
break;
case 10: // Lava
gPalette = LavaColors_p;
break;
case 11: // Forest
gPalette = ForestColors_p;
break;
case 12: // Party
gPalette = PartyColors_p;
break;
case 13: // Clouds
gPalette = CloudColors_p;
break;
case 14: // Bleagh(WILLIE)
gPalette = CRGBPalette16( m,m,m,m, d,d,d,d, A,A,A,A, L,L,L,L );
break;
}
}
enum { GETTING_DARKER = 0, GETTING_BRIGHTER = 1 };
void colortwinkles()
{
// Make each pixel brighter or darker, depending on
// its 'direction' flag.
brightenOrDarkenEachPixel( FADE_IN_SPEED, FADE_OUT_SPEED);
// Now consider adding a new random twinkle
if( random8() < DENSITY ) {
int pos = random16(NUM_LEDS);
if( !leds[pos]) {
leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND);
setPixelDirection(pos, GETTING_BRIGHTER);
}
}
}
void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount)
{
for( uint16_t i = 0; i < NUM_LEDS; i++) {
if( getPixelDirection(i) == GETTING_DARKER) {
// This pixel is getting darker
leds[i] = makeDarker( leds[i], fadeDownAmount);
} else {
// This pixel is getting brighter
leds[i] = makeBrighter( leds[i], fadeUpAmount);
// now check to see if we've maxxed out the brightness
if( leds[i].r == 255 || leds[i].g == 255 || leds[i].b == 255) {
// if so, turn around and start getting darker
setPixelDirection(i, GETTING_DARKER);
}
}
}
}
CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter)
{
CRGB incrementalColor = color;
incrementalColor.nscale8( howMuchBrighter);
return color + incrementalColor;
}
CRGB makeDarker( const CRGB& color, fract8 howMuchDarker)
{
CRGB newcolor = color;
newcolor.nscale8( 255 - howMuchDarker);
return newcolor;
}
// For illustration purposes, there are two separate implementations
// provided here for the array of 'directionFlags':
// - a simple one, which uses one byte (8 bits) of RAM for each pixel, and
// - a compact one, which uses just one BIT of RAM for each pixel.
// Set this to 1 or 8 to select which implementation
// of directionFlags is used. 1=more compact, 8=simpler.
#define BITS_PER_DIRECTION_FLAG 1
#if BITS_PER_DIRECTION_FLAG == 8
// Simple implementation of the directionFlags array,
// which takes up one byte (eight bits) per pixel.
uint8_t directionFlags[NUM_LEDS];
bool getPixelDirection( uint16_t i) {
return directionFlags[i];
}
void setPixelDirection( uint16_t i, bool dir) {
directionFlags[i] = dir;
}
#endif
#if BITS_PER_DIRECTION_FLAG == 1
// Compact (but more complicated) implementation of
// the directionFlags array, using just one BIT of RAM
// per pixel. This requires a bunch of bit wrangling,
// but conserves precious RAM. The cost is a few
// cycles and about 100 bytes of flash program memory.
uint8_t directionFlags[ (NUM_LEDS+7) / 8];
bool getPixelDirection( uint16_t i) {
uint16_t index = i / 8;
uint8_t bitNum = i & 0x07;
// using Arduino 'bitRead' function; expanded code below
return bitRead( directionFlags[index], bitNum);
// uint8_t andMask = 1 << bitNum;
// return (directionFlags[index] & andMask) != 0;
}
void setPixelDirection( uint16_t i, bool dir) {
uint16_t index = i / 8;
uint8_t bitNum = i & 0x07;
// using Arduino 'bitWrite' function; expanded code below
bitWrite( directionFlags[index], bitNum, dir);
// uint8_t orMask = 1 << bitNum;
// uint8_t andMask = 255 - orMask;
// uint8_t value = directionFlags[index] & andMask;
// if( dir ) {
// value += orMask;
// }
// directionFlags[index] = value;
}
#endif
//AliceBlue = 0xF0F8FF
//Amethyst = 0x9966CC
//AntiqueWhite = 0xFAEBD7
//Aqua = 0x00FFFF
//Aquamarine = 0x7FFFD4
//Azure = 0xF0FFFF
//Beige = 0xF5F5DC
//Bisque = 0xFFE4C4
//Black = 0x000000
//BlanchedAlmond = 0xFFEBCD
//Blue = 0x0000FF
//BlueViolet = 0x8A2BE2
//Brown = 0xA52A2A
//BurlyWood = 0xDEB887
//CadetBlue = 0x5F9EA0
//Chartreuse = 0x7FFF00
//Chocolate = 0xD2691E
//Coral = 0xFF7F50
//CornflowerBlue = 0x6495ED
//Cornsilk = 0xFFF8DC
//Crimson = 0xDC143C
//Cyan = 0x00FFFF
//DarkBlue = 0x00008B
//DarkCyan = 0x008B8B
//DarkGoldenrod = 0xB8860B
//DarkGray = 0xA9A9A9
//DarkGreen = 0x006400
//DarkKhaki = 0xBDB76B
//DarkMagenta = 0x8B008B
//DarkOliveGreen = 0x556B2F
//DarkOrange = 0xFF8C00
//DarkOrchid = 0x9932CC
//DarkRed = 0x8B0000
//DarkSalmon = 0xE9967A
//DarkSeaGreen = 0x8FBC8F
//DarkSlateBlue = 0x483D8B
//DarkSlateGray = 0x2F4F4F
//DarkTurquoise = 0x00CED1
//DarkViolet = 0x9400D3
//DeepPink = 0xFF1493
//DeepSkyBlue = 0x00BFFF
//DimGray = 0x696969
//DodgerBlue = 0x1E90FF
//FireBrick = 0xB22222
//FloralWhite = 0xFFFAF0
//ForestGreen = 0x228B22
//Fuchsia = 0xFF00FF
//Gainsboro = 0xDCDCDC
//GhostWhite = 0xF8F8FF
//Gold = 0xFFD700
//Goldenrod = 0xDAA520
//Gray = 0x808080
//Green = 0x008000
//GreenYellow = 0xADFF2F
//Honeydew = 0xF0FFF0
//HotPink = 0xFF69B4
//IndianRed = 0xCD5C5C
//Indigo = 0x4B0082
//Ivory = 0xFFFFF0
//Khaki = 0xF0E68C
//Lavender = 0xE6E6FA
//LavenderBlush = 0xFFF0F5
//LawnGreen = 0x7CFC00
//LemonChiffon = 0xFFFACD
//LightBlue = 0xADD8E6
//LightCoral = 0xF08080
//LightCyan = 0xE0FFFF
//LightGoldenrodYellow = 0xFAFAD2
//LightGreen = 0x90EE90
//LightGrey = 0xD3D3D3
//LightPink = 0xFFB6C1
//LightSalmon = 0xFFA07A
//LightSeaGreen = 0x20B2AA
//LightSkyBlue = 0x87CEFA
//LightSlateGray = 0x778899
//LightSteelBlue = 0xB0C4DE
//LightYellow = 0xFFFFE0
//Lime = 0x00FF00
//LimeGreen = 0x32CD32
//Linen = 0xFAF0E6
//Magenta = 0xFF00FF
//Maroon = 0x800000
//MediumAquamarine = 0x66CDAA
//MediumBlue = 0x0000CD
//MediumOrchid = 0xBA55D3
//MediumPurple = 0x9370DB
//MediumSeaGreen = 0x3CB371
//MediumSlateBlue = 0x7B68EE
//MediumSpringGreen = 0x00FA9A
//MediumTurquoise = 0x48D1CC
//MediumVioletRed = 0xC71585
//MidnightBlue = 0x191970
//MintCream = 0xF5FFFA
//MistyRose = 0xFFE4E1
//Moccasin = 0xFFE4B5
//NavajoWhite = 0xFFDEAD
//Navy = 0x000080
//OldLace = 0xFDF5E6
//Olive = 0x808000
//OliveDrab = 0x6B8E23
//Orange = 0xFFA500
//OrangeRed = 0xFF4500
//Orchid = 0xDA70D6
//PaleGoldenrod = 0xEEE8AA
//PaleGreen = 0x98FB98
//PaleTurquoise = 0xAFEEEE
//PaleVioletRed = 0xDB7093
//PapayaWhip = 0xFFEFD5
//PeachPuff = 0xFFDAB9
//Peru = 0xCD853F
//Pink = 0xFFC0CB
//Plaid = 0xCC5533
//Plum = 0xDDA0DD
//PowderBlue = 0xB0E0E6
//Purple = 0x800080
//Red = 0xFF0000
//RosyBrown = 0xBC8F8F
//RoyalBlue = 0x4169E1
//SaddleBrown = 0x8B4513
//Salmon = 0xFA8072
//SandyBrown = 0xF4A460
//SeaGreen = 0x2E8B57
//Seashell = 0xFFF5EE
//Sienna = 0xA0522D
//Silver = 0xC0C0C0
//SkyBlue = 0x87CEEB
//SlateBlue = 0x6A5ACD
//SlateGray = 0x708090
//Snow = 0xFFFAFA
//SpringGreen = 0x00FF7F
//SteelBlue = 0x4682B4
//Tan = 0xD2B48C
//Teal = 0x008080
//Thistle = 0xD8BFD8
//Tomato = 0xFF6347
//Turquoise = 0x40E0D0
//Violet = 0xEE82EE
//Wheat = 0xF5DEB3
//White = 0xFFFFFF
//WhiteSmoke = 0xF5F5F5
//Yellow = 0xFFFF00
//YellowGreen = 0x9ACD32
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment