FastLED 3.7.8
Loading...
Searching...
No Matches
Fire2012.ino
Go to the documentation of this file.
1
4
5#include <FastLED.h>
6
7#define LED_PIN 5
8#define COLOR_ORDER GRB
9#define CHIPSET WS2811
10#define NUM_LEDS 30
11
12#define BRIGHTNESS 200
13#define FRAMES_PER_SECOND 60
14
15bool gReverseDirection = false;
16
17CRGB leds[NUM_LEDS];
18
19void setup() {
20 delay(3000); // sanity delay
21 FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
22 FastLED.setBrightness( BRIGHTNESS );
23}
24
25void loop()
26{
27 // Add entropy to random number generator; we use a lot of it.
28 // random16_add_entropy( random());
29
30 Fire2012(); // run simulation frame
31
32 FastLED.show(); // display this frame
33 FastLED.delay(1000 / FRAMES_PER_SECOND);
34}
35
36
37// Fire2012 by Mark Kriegsman, July 2012
38// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
40// This basic one-dimensional 'fire' simulation works roughly as follows:
41// There's a underlying array of 'heat' cells, that model the temperature
42// at each point along the line. Every cycle through the simulation,
43// four steps are performed:
44// 1) All cells cool down a little bit, losing heat to the air
45// 2) The heat from each cell drifts 'up' and diffuses a little
46// 3) Sometimes randomly new 'sparks' of heat are added at the bottom
47// 4) The heat from each cell is rendered as a color into the leds array
48// The heat-to-color mapping uses a black-body radiation approximation.
49//
50// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
51//
52// This simulation scales it self a bit depending on NUM_LEDS; it should look
53// "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
54//
55// I recommend running this simulation at anywhere from 30-100 frames per second,
56// meaning an interframe delay of about 10-35 milliseconds.
57//
58// Looks best on a high-density LED setup (60+ pixels/meter).
59//
60//
61// There are two main parameters you can play with to control the look and
62// feel of your fire: COOLING (used in step 1 above), and SPARKING (used
63// in step 3 above).
64//
65// COOLING: How much does the air cool as it rises?
66// Less cooling = taller flames. More cooling = shorter flames.
67// Default 50, suggested range 20-100
68#define COOLING 55
69
70// SPARKING: What chance (out of 255) is there that a new spark will be lit?
71// Higher chance = more roaring fire. Lower chance = more flickery fire.
72// Default 120, suggested range 50-200.
73#define SPARKING 120
74
75
76void Fire2012()
77{
78// Array of temperature readings at each simulation cell
79 static uint8_t heat[NUM_LEDS];
80
81 // Step 1. Cool down every cell a little
82 for( int i = 0; i < NUM_LEDS; i++) {
83 heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
84 }
85
86 // Step 2. Heat from each cell drifts 'up' and diffuses a little
87 for( int k= NUM_LEDS - 1; k >= 2; k--) {
88 heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
89 }
90
91 // Step 3. Randomly ignite new 'sparks' of heat near the bottom
92 if( random8() < SPARKING ) {
93 int y = random8(7);
94 heat[y] = qadd8( heat[y], random8(160,255) );
95 }
96
97 // Step 4. Map from heat cells to LED colors
98 for( int j = 0; j < NUM_LEDS; j++) {
99 CRGB color = HeatColor( heat[j]);
100 int pixelnumber;
101 if( gReverseDirection ) {
102 pixelnumber = (NUM_LEDS-1) - j;
103 } else {
104 pixelnumber = j;
105 }
106 leds[pixelnumber] = color;
107 }
108}
109
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:21
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:154
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
@ TypicalLEDStrip
Typical values for SMD5050 LEDs.
Definition color.h:20
CRGB HeatColor(uint8_t temperature)
Approximates a "black body radiation" spectrum for a given "heat" level.
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
LIB8STATIC uint8_t random8()
Generate an 8-bit random number.
Definition random8.h:40
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:25