FastLED 3.9.15
Loading...
Searching...
No Matches
Fire2012WithPalette.ino
Go to the documentation of this file.
1// @filter: (board is not stm32f103cb) // Maple Mini lacks pin 5 (uses libmaple numbering)
5
6#include <Arduino.h>
7#include <FastLED.h>
8
9#define LED_PIN 5
10#define COLOR_ORDER GRB
11#define CHIPSET WS2811
12#define NUM_LEDS 30
13
14#define BRIGHTNESS 200
15#define FRAMES_PER_SECOND 60
16
17bool gReverseDirection = false;
18
20
21CRGBPalette16 gPal;
22
23// Fire2012 by Mark Kriegsman, July 2012
24// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
26// This basic one-dimensional 'fire' simulation works roughly as follows:
27// There's a underlying array of 'heat' cells, that model the temperature
28// at each point along the line. Every cycle through the simulation,
29// four steps are performed:
30// 1) All cells cool down a little bit, losing heat to the air
31// 2) The heat from each cell drifts 'up' and diffuses a little
32// 3) Sometimes randomly new 'sparks' of heat are added at the bottom
33// 4) The heat from each cell is rendered as a color into the leds array
34// The heat-to-color mapping uses a black-body radiation approximation.
35//
36// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
37//
38// This simulation scales it self a bit depending on NUM_LEDS; it should look
39// "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
40//
41// I recommend running this simulation at anywhere from 30-100 frames per second,
42// meaning an interframe delay of about 10-35 milliseconds.
43//
44// Looks best on a high-density LED setup (60+ pixels/meter).
45//
46//
47// There are two main parameters you can play with to control the look and
48// feel of your fire: COOLING (used in step 1 above), and SPARKING (used
49// in step 3 above).
50//
51// COOLING: How much does the air cool as it rises?
52// Less cooling = taller flames. More cooling = shorter flames.
53// Default 55, suggested range 20-100
54#define COOLING 55
55
56// SPARKING: What chance (out of 255) is there that a new spark will be lit?
57// Higher chance = more roaring fire. Lower chance = more flickery fire.
58// Default 120, suggested range 50-200.
59#define SPARKING 120
60
61
63{
64// Array of temperature readings at each simulation cell
65 static uint8_t heat[NUM_LEDS];
66
67 // Step 1. Cool down every cell a little
68 for( int i = 0; i < NUM_LEDS; i++) {
69 heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
70 }
71
72 // Step 2. Heat from each cell drifts 'up' and diffuses a little
73 for( int k= NUM_LEDS - 1; k >= 2; k--) {
74 heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
75 }
76
77 // Step 3. Randomly ignite new 'sparks' of heat near the bottom
78 if( random8() < SPARKING ) {
79 int y = random8(7);
80 heat[y] = qadd8( heat[y], random8(160,255) );
81 }
82
83 // Step 4. Map from heat cells to LED colors
84 for( int j = 0; j < NUM_LEDS; j++) {
85 // Scale the heat value from 0-255 down to 0-240
86 // for best results with color palettes.
87 uint8_t colorindex = scale8( heat[j], 240);
88 CRGB color = ColorFromPalette( gPal, colorindex);
89 int pixelnumber;
90 if( gReverseDirection ) {
91 pixelnumber = (NUM_LEDS-1) - j;
92 } else {
93 pixelnumber = j;
94 }
95 leds[pixelnumber] = color;
96 }
97}
98
99// Fire2012 with programmable Color Palette
100//
101// This code is the same fire simulation as the original "Fire2012",
102// but each heat cell's temperature is translated to color through a FastLED
103// programmable color palette, instead of through the "HeatColor(...)" function.
104//
105// Four different static color palettes are provided here, plus one dynamic one.
106//
107// The three static ones are:
108// 1. the FastLED built-in HeatColors_p -- this is the default, and it looks
109// pretty much exactly like the original Fire2012.
110//
111// To use any of the other palettes below, just "uncomment" the corresponding code.
112//
113// 2. a gradient from black to red to yellow to white, which is
114// visually similar to the HeatColors_p, and helps to illustrate
115// what the 'heat colors' palette is actually doing,
116// 3. a similar gradient, but in blue colors rather than red ones,
117// i.e. from black to blue to aqua to white, which results in
118// an "icy blue" fire effect,
119// 4. a simplified three-step gradient, from black to red to white, just to show
120// that these gradients need not have four components; two or
121// three are possible, too, even if they don't look quite as nice for fire.
122//
123// The dynamic palette shows how you can change the basic 'hue' of the
124// color palette every time through the loop, producing "rainbow fire".
125
126void setup() {
127 delay(3000); // sanity delay
128 FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
129 FastLED.setBrightness( BRIGHTNESS );
130
131 // This first palette is the basic 'black body radiation' colors,
132 // which run from black to red to bright yellow to white.
134
135 // These are other ways to set up the color palette for the 'fire'.
136 // First, a gradient from black to red to yellow to white -- similar to HeatColors_p
137 // gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);
138
139 // Second, this palette is like the heat colors, but blue/aqua instead of red/yellow
140 // gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
141
142 // Third, here's a simpler, three-step gradient, from black to red to white
143 // gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);
144 Serial.println("setup");
145}
146
147void loop()
148{
149 // Add entropy to random number generator; we use a lot of it.
151
152 // Fourth, the most sophisticated: this one sets up a new palette every
153 // time through the loop, based on a hue that changes every time.
154 // The palette is a gradient from black, to a dark color based on the hue,
155 // to a light color based on the hue, to white.
156 //
157 // static uint8_t hue = 0;
158 // hue++;
159 // CRGB darkcolor = CHSV(hue,255,192); // pure hue, three-quarters brightness
160 // CRGB lightcolor = CHSV(hue,128,255); // half 'whitened', full brightness
161 // gPal = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);
162
163
164 Fire2012WithPalette(); // run simulation frame, using palette colors
165
166 FastLED.show(); // display this frame
167 FastLED.delay(1000 / FRAMES_PER_SECOND);
168}
void setup()
void loop()
#define COLOR_ORDER
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
#define LED_PIN
#define BRIGHTNESS
int y
Definition simple.h:93
#define CHIPSET
#define FRAMES_PER_SECOND
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
#define COOLING
Definition Fire2012.ino:72
#define SPARKING
Definition Fire2012.ino:77
bool gReverseDirection
Definition Fire2012.ino:16
CRGBPalette16 gPal
void Fire2012WithPalette()
uint8_t heat[NUM_LEDS]
Definition Fire2023.h:100
CRGB ColorFromPalette(const CRGBPalette16 &pal, fl::u8 index, fl::u8 brightness, TBlendType blendType)
@ TypicalLEDStrip
Typical values for SMD5050 LEDs.
Definition color.h:15
fl::CRGB CRGB
Definition crgb.h:25
const TProgmemRGBPalette16 HeatColors_p
Approximate "black body radiation" palette, akin to the FastLED HeatColor() function.
LIB8STATIC void random16_add_entropy(fl::u16 entropy) FL_NOEXCEPT
Add entropy into the random number generator.
Definition random8.h:110
LIB8STATIC fl::u16 random16() FL_NOEXCEPT
Generate a 16-bit random number.
Definition random8.h:63
LIB8STATIC fl::u8 random8() FL_NOEXCEPT
Generate an 8-bit random number.
Definition random8.h:53
#define Serial
Definition serial.h:304