FastLED 3.9.7
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 FL_PROGMEM;
40
41// If you are using the fastled compiler, then you must declare your functions
42// before you use them. This is standard in C++ and C projects, but ino's are
43// special in that they do this for you. Eventually we will try to emulate this
44// feature ourselves but in the meantime you'll have to declare your functions
45// before you use them if you want to use our compiler.
46void ChangePalettePeriodically();
47void FillLEDsFromPaletteColors(uint8_t colorIndex);
48void SetupPurpleAndGreenPalette();
49void SetupTotallyRandomPalette();
50void SetupBlackAndWhiteStripedPalette();
51
52
53void setup() {
54 delay( 3000 ); // power-up safety delay
55 FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
56 FastLED.setBrightness( BRIGHTNESS );
57
58 currentPalette = RainbowColors_p;
59 currentBlending = LINEARBLEND;
60}
61
62
63void loop()
64{
65 ChangePalettePeriodically();
66
67 static uint8_t startIndex = 0;
68 startIndex = startIndex + 1; /* motion speed */
69
70 FillLEDsFromPaletteColors( startIndex);
71
72 FastLED.show();
73 FastLED.delay(1000 / UPDATES_PER_SECOND);
74}
75
76void FillLEDsFromPaletteColors( uint8_t colorIndex)
77{
78 uint8_t brightness = 255;
79
80 for( int i = 0; i < NUM_LEDS; ++i) {
81 leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
82 colorIndex += 3;
83 }
84}
85
86
87// There are several different palettes of colors demonstrated here.
88//
89// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
90// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
91//
92// Additionally, you can manually define your own color palettes, or you can write
93// code that creates color palettes on the fly. All are shown here.
94
95void ChangePalettePeriodically()
96{
97 uint8_t secondHand = (millis() / 1000) % 60;
98 static uint8_t lastSecond = 99;
99
100 if( lastSecond != secondHand) {
101 lastSecond = secondHand;
102 if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; }
103 if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
104 if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; }
105 if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; }
106 if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; }
107 if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
108 if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; }
109 if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
110 if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; }
111 if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
112 if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
113 }
114}
115
116// This function fills the palette with totally random colors.
117void SetupTotallyRandomPalette()
118{
119 for( int i = 0; i < 16; ++i) {
120 currentPalette[i] = CHSV( random8(), 255, random8());
121 }
122}
123
124// This function sets up a palette of black and white stripes,
125// using code. Since the palette is effectively an array of
126// sixteen CRGB colors, the various fill_* functions can be used
127// to set them up.
128void SetupBlackAndWhiteStripedPalette()
129{
130 // 'black out' all 16 palette entries...
131 fill_solid( currentPalette, 16, CRGB::Black);
132 // and set every fourth one to white.
133 currentPalette[0] = CRGB::White;
134 currentPalette[4] = CRGB::White;
135 currentPalette[8] = CRGB::White;
136 currentPalette[12] = CRGB::White;
137
138}
139
140// This function sets up a palette of purple and green stripes.
141void SetupPurpleAndGreenPalette()
142{
143 CRGB purple = CHSV( HUE_PURPLE, 255, 255);
144 CRGB green = CHSV( HUE_GREEN, 255, 255);
145 CRGB black = CRGB::Black;
146
147 currentPalette = CRGBPalette16(
148 green, green, black, black,
149 purple, purple, black, black,
150 green, green, black, black,
151 purple, purple, black, black );
152}
153
154
155// This example shows how to set up a static color palette
156// which is stored in PROGMEM (flash), which is almost always more
157// plentiful than RAM. A static PROGMEM palette like this
158// takes up 64 bytes of flash.
159const TProgmemPalette16 myRedWhiteBluePalette_p FL_PROGMEM =
160{
161 CRGB::Red,
162 CRGB::Gray, // 'white' is too bright compared to red and blue
165
166 CRGB::Red,
170
171 CRGB::Red,
172 CRGB::Red,
179};
180
181
182
183// Additional notes on FastLED compact palettes:
184//
185// Normally, in computer graphics, the palette (or "color lookup table")
186// has 256 entries, each containing a specific 24-bit RGB color. You can then
187// index into the color palette using a simple 8-bit (one byte) value.
188// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
189// is quite possibly "too many" bytes.
190//
191// FastLED does offer traditional 256-element palettes, for setups that
192// can afford the 768-byte cost in RAM.
193//
194// However, FastLED also offers a compact alternative. FastLED offers
195// palettes that store 16 distinct entries, but can be accessed AS IF
196// they actually have 256 entries; this is accomplished by interpolating
197// between the 16 explicit entries to create fifteen intermediate palette
198// entries between each pair.
199//
200// So for example, if you set the first two explicit entries of a compact
201// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
202// the first sixteen entries from the virtual palette (of 256), you'd get
203// Green, followed by a smooth gradient from green-to-blue, and then Blue.
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:45
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:190
void setBrightness(uint8_t scale)
Set the global brightness scaling.
Definition FastLED.h:723
void show(uint8_t scale)
Update all our controllers with the current led colors, using the passed in brightness.
Definition FastLED.cpp:94
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:79
RGB color palette with 16 discrete values.
Definition colorutils.h:997
#define TProgmemPalette16
Alias for TProgmemRGBPalette16.
Definition colorutils.h:79
#define FL_PROGMEM
PROGMEM keyword for storage.
@ 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.
@ HUE_PURPLE
Purple (270°)
Definition chsv.h:104
@ HUE_GREEN
Green (135°)
Definition chsv.h:101
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:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54
@ White
Definition crgb.h:632
@ Gray
Definition crgb.h:542
@ Blue
Definition crgb.h:498
@ Red
Definition crgb.h:608
@ Black
Definition crgb.h:496