FastLED 3.9.3
Loading...
Searching...
No Matches
ColorPalette.ino
Go to the documentation of this file.
1
4
5#include <FastLED.h>
6
7#define LED_PIN 5
8#define NUM_LEDS 50
9#define BRIGHTNESS 64
10#define LED_TYPE WS2811
11#define COLOR_ORDER GRB
12CRGB leds[NUM_LEDS];
13
14#define UPDATES_PER_SECOND 100
15
16// This example shows several ways to set up and use 'palettes' of colors
17// with FastLED.
18//
19// These compact palettes provide an easy way to re-colorize your
20// animation on the fly, quickly, easily, and with low overhead.
21//
22// USING palettes is MUCH simpler in practice than in theory, so first just
23// run this sketch, and watch the pretty lights as you then read through
24// the code. Although this sketch has eight (or more) different color schemes,
25// the entire sketch compiles down to about 6.5K on AVR.
26//
27// FastLED provides a few pre-configured color palettes, and makes it
28// extremely easy to make up your own color schemes with palettes.
29//
30// Some notes on the more abstract 'theory and practice' of
31// FastLED compact palettes are at the bottom of this file.
32
33
34
35CRGBPalette16 currentPalette;
36TBlendType currentBlending;
37
38extern CRGBPalette16 myRedWhiteBluePalette;
39extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
40
41
42void setup() {
43 delay( 3000 ); // power-up safety delay
44 FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
45 FastLED.setBrightness( BRIGHTNESS );
46
47 currentPalette = RainbowColors_p;
48 currentBlending = LINEARBLEND;
49}
50
51
52void loop()
53{
54 ChangePalettePeriodically();
55
56 static uint8_t startIndex = 0;
57 startIndex = startIndex + 1; /* motion speed */
58
59 FillLEDsFromPaletteColors( startIndex);
60
61 FastLED.show();
62 FastLED.delay(1000 / UPDATES_PER_SECOND);
63}
64
65void FillLEDsFromPaletteColors( uint8_t colorIndex)
66{
67 uint8_t brightness = 255;
68
69 for( int i = 0; i < NUM_LEDS; ++i) {
70 leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
71 colorIndex += 3;
72 }
73}
74
75
76// There are several different palettes of colors demonstrated here.
77//
78// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
79// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
80//
81// Additionally, you can manually define your own color palettes, or you can write
82// code that creates color palettes on the fly. All are shown here.
83
84void ChangePalettePeriodically()
85{
86 uint8_t secondHand = (millis() / 1000) % 60;
87 static uint8_t lastSecond = 99;
88
89 if( lastSecond != secondHand) {
90 lastSecond = secondHand;
91 if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; }
92 if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
93 if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; }
94 if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; }
95 if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; }
96 if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
97 if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; }
98 if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
99 if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; }
100 if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
101 if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
102 }
103}
104
105// This function fills the palette with totally random colors.
106void SetupTotallyRandomPalette()
107{
108 for( int i = 0; i < 16; ++i) {
109 currentPalette[i] = CHSV( random8(), 255, random8());
110 }
111}
112
113// This function sets up a palette of black and white stripes,
114// using code. Since the palette is effectively an array of
115// sixteen CRGB colors, the various fill_* functions can be used
116// to set them up.
117void SetupBlackAndWhiteStripedPalette()
118{
119 // 'black out' all 16 palette entries...
120 fill_solid( currentPalette, 16, CRGB::Black);
121 // and set every fourth one to white.
122 currentPalette[0] = CRGB::White;
123 currentPalette[4] = CRGB::White;
124 currentPalette[8] = CRGB::White;
125 currentPalette[12] = CRGB::White;
126
127}
128
129// This function sets up a palette of purple and green stripes.
130void SetupPurpleAndGreenPalette()
131{
132 CRGB purple = CHSV( HUE_PURPLE, 255, 255);
133 CRGB green = CHSV( HUE_GREEN, 255, 255);
134 CRGB black = CRGB::Black;
135
136 currentPalette = CRGBPalette16(
137 green, green, black, black,
138 purple, purple, black, black,
139 green, green, black, black,
140 purple, purple, black, black );
141}
142
143
144// This example shows how to set up a static color palette
145// which is stored in PROGMEM (flash), which is almost always more
146// plentiful than RAM. A static PROGMEM palette like this
147// takes up 64 bytes of flash.
148const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
149{
150 CRGB::Red,
151 CRGB::Gray, // 'white' is too bright compared to red and blue
154
155 CRGB::Red,
159
160 CRGB::Red,
161 CRGB::Red,
168};
169
170
171
172// Additional notes on FastLED compact palettes:
173//
174// Normally, in computer graphics, the palette (or "color lookup table")
175// has 256 entries, each containing a specific 24-bit RGB color. You can then
176// index into the color palette using a simple 8-bit (one byte) value.
177// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
178// is quite possibly "too many" bytes.
179//
180// FastLED does offer traditional 256-element palettes, for setups that
181// can afford the 768-byte cost in RAM.
182//
183// However, FastLED also offers a compact alternative. FastLED offers
184// palettes that store 16 distinct entries, but can be accessed AS IF
185// they actually have 256 entries; this is accomplished by interpolating
186// between the 16 explicit entries to create fifteen intermediate palette
187// entries between each pair.
188//
189// So for example, if you set the first two explicit entries of a compact
190// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
191// the first sixteen entries from the virtual palette (of 256), you'd get
192// Green, followed by a smooth gradient from green-to-blue, and then Blue.
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:33
central include file for FastLED, defines the CFastLED class/object
void delay(unsigned long ms)
Delay for the given number of milliseconds.
Definition FastLED.cpp:178
void setBrightness(uint8_t scale)
Set the global brightness scaling.
Definition FastLED.h:722
void show(uint8_t scale)
Update all our controllers with the current led colors, using the passed in brightness.
Definition FastLED.cpp:82
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:67
RGB color palette with 16 discrete values.
Definition colorutils.h:997
#define TProgmemPalette16
Alias for TProgmemRGBPalette16.
Definition colorutils.h:79
@ TypicalLEDStrip
Typical values for SMD5050 LEDs.
Definition color.h:19
void fill_solid(struct CRGB *targetArray, int numToFill, const struct CRGB &color)
Fill a range of LEDs with a solid color.
TBlendType
Color interpolation options for palette.
CRGB ColorFromPalette(const CRGBPalette16 &pal, uint8_t index, uint8_t brightness, TBlendType blendType)
Get a color from a palette.
@ NOBLEND
No interpolation between palette entries.
@ LINEARBLEND
Linear interpolation between palette entries, with wrap-around from end to the beginning again.
@ White
Definition crgb.h:615
@ Gray
Definition crgb.h:525
@ Blue
Definition crgb.h:481
@ Red
Definition crgb.h:591
@ Black
Definition crgb.h:479
const TProgmemRGBPalette16 RainbowStripeColors_p
HSV Rainbow colors with alternatating stripes of black.
const TProgmemRGBPalette16 CloudColors_p
Cloudy color palette.
const TProgmemRGBPalette16 PartyColors_p
HSV color ramp: blue, purple, pink, red, orange, yellow (and back).
const TProgmemRGBPalette16 RainbowColors_p
HSV Rainbow.
LIB8STATIC uint8_t random8()
Generate an 8-bit random number.
Definition random8.h:40
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:11
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39