Skip to content

Commit c736efb

Browse files
add fire pattern
1 parent 5422c8e commit c736efb

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

fastled_patterns/Fire.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Fire.cpp
3+
*/
4+
5+
#include <Fire.h>
6+
7+
/**
8+
* Random colored speckles that blink in and fade smoothly.
9+
*/
10+
Fire::Fire(CRGB* neopixels, uint16_t numLEDS) {
11+
_neopixels = neopixels;
12+
_numLEDS = numLEDS;
13+
}
14+
15+
void Fire::loop() {
16+
// Array of temperature readings at each simulation cell
17+
uint8_t heat[_numLEDS];
18+
19+
// Step 1. Cool down every cell a little
20+
for (int i = 0; i < _numLEDS; i++) {
21+
heat[i] = qsub8(heat[i], random8(0, ((COOLING * 10) / _numLEDS) + 2));
22+
}
23+
24+
// Step 2. Heat from each cell drifts 'up' and diffuses a little
25+
for (int k= _numLEDS - 1; k >= 2; k--) {
26+
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
27+
}
28+
29+
// Step 3. Randomly ignite new 'sparks' of heat near the bottom
30+
if (random8() < SPARKING) {
31+
int y = random8(7);
32+
heat[y] = qadd8(heat[y], random8(160, 255) );
33+
}
34+
35+
// Step 4. Map from heat cells to LED colors
36+
for (int j = 0; j < _numLEDS; j++) {
37+
CRGB color = HeatColor(heat[j]);
38+
int pixelnumber;
39+
if (gReverseDirection) {
40+
pixelnumber = (_numLEDS - 1) - j;
41+
} else {
42+
pixelnumber = j;
43+
}
44+
_neopixels[pixelnumber] = color;
45+
}
46+
}

fastled_patterns/Fire.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Fire.h
3+
*/
4+
#ifndef Fire_h
5+
#define Fire_h
6+
7+
#include "Arduino.h"
8+
#include <FastLED.h>
9+
10+
// COOLING: How much does the air cool as it rises?
11+
// Less cooling = taller flames. More cooling = shorter flames.
12+
// Default 50, suggested range 20-100
13+
#define COOLING 55
14+
15+
// SPARKING: What chance (out of 255) is there that a new spark will be lit?
16+
// Higher chance = more roaring fire. Lower chance = more flickery fire.
17+
// Default 120, suggested range 50-200.
18+
#define SPARKING 120
19+
20+
class Fire {
21+
public:
22+
Fire(CRGB* neopixels, uint16_t numLEDS);
23+
void loop();
24+
25+
private:
26+
CRGB* _neopixels;
27+
uint16_t _numLEDS;
28+
29+
bool gReverseDirection = false;
30+
};
31+
32+
#endif

0 commit comments

Comments
 (0)