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