FastLED 3.9.7
Loading...
Searching...
No Matches
NoisePlusPalette.ino
Go to the documentation of this file.
1
4
5#include <FastLED.h>
6
7#ifdef __AVR__
8// Don't compile this for avr because they typically don't have enough memory.
9void setup() {}
10void loop() {}
11#else
12
13#define LED_PIN 3
14#define BRIGHTNESS 96
15#define LED_TYPE WS2811
16#define COLOR_ORDER GRB
17
18// Params for width and height
19const uint8_t kMatrixWidth = 16;
20const uint8_t kMatrixHeight = 16;
21
22// Param for different pixel layouts
23const bool kMatrixSerpentineLayout = true;
24
25
26// This example combines two features of FastLED to produce a remarkable range of
27// effects from a relatively small amount of code. This example combines FastLED's
28// color palette lookup functions with FastLED's Perlin noise generator, and
29// the combination is extremely powerful.
30//
31// You might want to look at the "ColorPalette" and "Noise" examples separately
32// if this example code seems daunting.
33//
34//
35// The basic setup here is that for each frame, we generate a new array of
36// 'noise' data, and then map it onto the LED matrix through a color palette.
37//
38// Periodically, the color palette is changed, and new noise-generation parameters
39// are chosen at the same time. In this example, specific noise-generation
40// values have been selected to match the given color palettes; some are faster,
41// or slower, or larger, or smaller than others, but there's no reason these
42// parameters can't be freely mixed-and-matched.
43//
44// In addition, this example includes some fast automatic 'data smoothing' at
45// lower noise speeds to help produce smoother animations in those cases.
46//
47// The FastLED built-in color palettes (Forest, Clouds, Lava, Ocean, Party) are
48// used, as well as some 'hand-defined' ones, and some proceedurally generated
49// palettes.
50
51
52#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
53#define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
54
55// The leds
56CRGB leds[kMatrixWidth * kMatrixHeight];
57
58// The 16 bit version of our coordinates
59static uint16_t x;
60static uint16_t y;
61static uint16_t z;
62
63// We're using the x/y dimensions to map to the x/y pixels on the matrix. We'll
64// use the z-axis for "time". speed determines how fast time moves forward. Try
65// 1 for a very slow moving effect, or 60 for something that ends up looking like
66// water.
67uint16_t speed = 20; // speed is set dynamically once we've started up
68
69// Scale determines how far apart the pixels in our noise matrix are. Try
70// changing these values around to see how it affects the motion of the display. The
71// higher the value of scale, the more "zoomed out" the noise iwll be. A value
72// of 1 will be so zoomed in, you'll mostly see solid colors.
73uint16_t scale = 30; // scale is set dynamically once we've started up
74
75// This is the array that we keep our computed noise values in
76uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
77
78CRGBPalette16 currentPalette( PartyColors_p );
79uint8_t colorLoop = 1;
80
81
82// Forward declare our functions so that we have maximum compatibility
83// with other build tools outside of ArduinoIDE. The *.ino files are
84// special in that Arduino will generate function prototypes for you.
85void SetupRandomPalette();
86void SetupPurpleAndGreenPalette();
87void SetupBlackAndWhiteStripedPalette();
88void SetupRandomPalette();
89void ChangePaletteAndSettingsPeriodically();
90void mapNoiseToLEDsUsingPalette();
91uint16_t XY( uint8_t x, uint8_t y);
92
93void setup() {
94 delay(3000);
95 FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
96 FastLED.setBrightness(BRIGHTNESS);
97
98 // Initialize our coordinates to some random values
99 x = random16();
100 y = random16();
101 z = random16();
102}
103
104
105
106// Fill the x/y array of 8-bit noise values using the inoise8 function.
107void fillnoise8() {
108 // If we're runing at a low "speed", some 8-bit artifacts become visible
109 // from frame-to-frame. In order to reduce this, we can do some fast data-smoothing.
110 // The amount of data smoothing we're doing depends on "speed".
111 uint8_t dataSmoothing = 0;
112 if( speed < 50) {
113 dataSmoothing = 200 - (speed * 4);
114 }
115
116 for(int i = 0; i < MAX_DIMENSION; i++) {
117 int ioffset = scale * i;
118 for(int j = 0; j < MAX_DIMENSION; j++) {
119 int joffset = scale * j;
120
121 uint8_t data = inoise8(x + ioffset,y + joffset,z);
122
123 // The range of the inoise8 function is roughly 16-238.
124 // These two operations expand those values out to roughly 0..255
125 // You can comment them out if you want the raw noise data.
126 data = qsub8(data,16);
127 data = qadd8(data,scale8(data,39));
128
129 if( dataSmoothing ) {
130 uint8_t olddata = noise[i][j];
131 uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
132 data = newdata;
133 }
134
135 noise[i][j] = data;
136 }
137 }
138
139 z += speed;
140
141 // apply slow drift to X and Y, just for visual variation.
142 x += speed / 8;
143 y -= speed / 16;
144}
145
146
147
148void mapNoiseToLEDsUsingPalette()
149{
150 static uint8_t ihue=0;
151
152 for(int i = 0; i < kMatrixWidth; i++) {
153 for(int j = 0; j < kMatrixHeight; j++) {
154 // We use the value at the (i,j) coordinate in the noise
155 // array for our brightness, and the flipped value from (j,i)
156 // for our pixel's index into the color palette.
157
158 uint8_t index = noise[j][i];
159 uint8_t bri = noise[i][j];
160
161 // if this palette is a 'loop', add a slowly-changing base value
162 if( colorLoop) {
163 index += ihue;
164 }
165
166 // brighten up, as the color palette itself often contains the
167 // light/dark dynamic range desired
168 if( bri > 127 ) {
169 bri = 255;
170 } else {
171 bri = dim8_raw( bri * 2);
172 }
173
174 CRGB color = ColorFromPalette( currentPalette, index, bri);
175 leds[XY(i,j)] = color;
176 }
177 }
178
179 ihue+=1;
180}
181
182void loop() {
183 // Periodically choose a new palette, speed, and scale
184 ChangePaletteAndSettingsPeriodically();
185
186 // generate noise data
187 fillnoise8();
188
189 // convert the noise data to colors in the LED array
190 // using the current palette
191 mapNoiseToLEDsUsingPalette();
192
193 FastLED.show();
194 // delay(10);
195}
196
197
198
199// There are several different palettes of colors demonstrated here.
200//
201// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
202// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
203//
204// Additionally, you can manually define your own color palettes, or you can write
205// code that creates color palettes on the fly.
206
207// 1 = 5 sec per palette
208// 2 = 10 sec per palette
209// etc
210#define HOLD_PALETTES_X_TIMES_AS_LONG 1
211
212void ChangePaletteAndSettingsPeriodically()
213{
214 uint8_t secondHand = ((millis() / 1000) / HOLD_PALETTES_X_TIMES_AS_LONG) % 60;
215 static uint8_t lastSecond = 99;
216
217 if( lastSecond != secondHand) {
218 lastSecond = secondHand;
219 if( secondHand == 0) { currentPalette = RainbowColors_p; speed = 20; scale = 30; colorLoop = 1; }
220 if( secondHand == 5) { SetupPurpleAndGreenPalette(); speed = 10; scale = 50; colorLoop = 1; }
221 if( secondHand == 10) { SetupBlackAndWhiteStripedPalette(); speed = 20; scale = 30; colorLoop = 1; }
222 if( secondHand == 15) { currentPalette = ForestColors_p; speed = 8; scale =120; colorLoop = 0; }
223 if( secondHand == 20) { currentPalette = CloudColors_p; speed = 4; scale = 30; colorLoop = 0; }
224 if( secondHand == 25) { currentPalette = LavaColors_p; speed = 8; scale = 50; colorLoop = 0; }
225 if( secondHand == 30) { currentPalette = OceanColors_p; speed = 20; scale = 90; colorLoop = 0; }
226 if( secondHand == 35) { currentPalette = PartyColors_p; speed = 20; scale = 30; colorLoop = 1; }
227 if( secondHand == 40) { SetupRandomPalette(); speed = 20; scale = 20; colorLoop = 1; }
228 if( secondHand == 45) { SetupRandomPalette(); speed = 50; scale = 50; colorLoop = 1; }
229 if( secondHand == 50) { SetupRandomPalette(); speed = 90; scale = 90; colorLoop = 1; }
230 if( secondHand == 55) { currentPalette = RainbowStripeColors_p; speed = 30; scale = 20; colorLoop = 1; }
231 }
232}
233
234
235
236// This function generates a random palette that's a gradient
237// between four different colors. The first is a dim hue, the second is
238// a bright hue, the third is a bright pastel, and the last is
239// another bright hue. This gives some visual bright/dark variation
240// which is more interesting than just a gradient of different hues.
241void SetupRandomPalette()
242{
243 currentPalette = CRGBPalette16(
244 CHSV( random8(), 255, 32),
245 CHSV( random8(), 255, 255),
246 CHSV( random8(), 128, 255),
247 CHSV( random8(), 255, 255));
248}
249
250
251// This function sets up a palette of black and white stripes,
252// using code. Since the palette is effectively an array of
253// sixteen CRGB colors, the various fill_* functions can be used
254// to set them up.
255void SetupBlackAndWhiteStripedPalette()
256{
257 // 'black out' all 16 palette entries...
258 fill_solid( currentPalette, 16, CRGB::Black);
259 // and set every fourth one to white.
260 currentPalette[0] = CRGB::White;
261 currentPalette[4] = CRGB::White;
262 currentPalette[8] = CRGB::White;
263 currentPalette[12] = CRGB::White;
264
265}
266
267// This function sets up a palette of purple and green stripes.
268void SetupPurpleAndGreenPalette()
269{
270 CRGB purple = CHSV( HUE_PURPLE, 255, 255);
271 CRGB green = CHSV( HUE_GREEN, 255, 255);
272 CRGB black = CRGB::Black;
273
274 currentPalette = CRGBPalette16(
275 green, green, black, black,
276 purple, purple, black, black,
277 green, green, black, black,
278 purple, purple, black, black );
279}
280
281
282//
283// Mark's xy coordinate mapping code. See the XYMatrix for more information on it.
284//
285uint16_t XY( uint8_t x, uint8_t y)
286{
287 uint16_t i;
288 if( kMatrixSerpentineLayout == false) {
289 i = (y * kMatrixWidth) + x;
290 }
291 if( kMatrixSerpentineLayout == true) {
292 if( y & 0x01) {
293 // Odd rows run backwards
294 uint8_t reverseX = (kMatrixWidth - 1) - x;
295 i = (y * kMatrixWidth) + reverseX;
296 } else {
297 // Even rows run forwards
298 i = (y * kMatrixWidth) + x;
299 }
300 }
301 return i;
302}
303
304
305#endif
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:45
central include file for FastLED, defines the CFastLED class/object
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
void fill_solid(struct CRGB *targetArray, int numToFill, const struct CRGB &color)
Fill a range of LEDs with a solid color.
LIB8STATIC uint8_t dim8_raw(uint8_t x)
Adjust a scaling value for dimming.
Definition scale8.h:703
LIB8STATIC_ALWAYS_INLINE uint8_t qadd8(uint8_t i, uint8_t j)
Add one byte to another, saturating at 0xFF.
Definition math8.h:31
LIB8STATIC_ALWAYS_INLINE uint8_t qsub8(uint8_t i, uint8_t j)
Subtract one byte from another, saturating at 0x00.
Definition math8.h:103
uint8_t inoise8(uint16_t x, uint16_t y, uint16_t z)
8-Bit, fixed point implementation of Perlin's noise.
Definition noise.cpp:512
CRGB ColorFromPalette(const CRGBPalette16 &pal, uint8_t index, uint8_t brightness, TBlendType blendType)
Get a color from a palette.
@ 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 OceanColors_p
Ocean colors, blues and whites.
const TProgmemRGBPalette16 CloudColors_p
Cloudy color palette.
const TProgmemRGBPalette16 ForestColors_p
Forest colors, greens.
const TProgmemRGBPalette16 LavaColors_p
Lava color palette.
const TProgmemRGBPalette16 PartyColors_p
HSV color ramp: blue, purple, pink, red, orange, yellow (and back).
const TProgmemRGBPalette16 RainbowColors_p
HSV Rainbow.
LIB8STATIC uint16_t random16()
Generate a 16-bit random number.
Definition random8.h:50
LIB8STATIC uint8_t random8()
Generate an 8-bit random number.
Definition random8.h:40
LIB8STATIC_ALWAYS_INLINE uint8_t scale8(uint8_t i, fract8 scale)
Scale one byte by a second one, which is treated as the numerator of a fraction whose denominator is ...
Definition scale8.h:34
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
@ Black
Definition crgb.h:496