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