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