Created
March 19, 2018 01:43
-
-
Save samguyer/a3f557ebb41d57fbeac557eddb548b66 to your computer and use it in GitHub Desktop.
Multipe flame Fire2012
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FastLED.h> | |
#define FLAME_1_PIN 3 | |
#define FLAME_1_SIZE 60 | |
#define FLAME_2_PIN 4 | |
#define FLAME_2_SIZE 24 | |
#define FLAME_3_PIN 5 | |
#define FLAME_3_SIZE 51 | |
#define COLOR_ORDER GRB | |
#define CHIPSET WS2812 | |
#define BRIGHTNESS 220 | |
#define FRAMES_PER_SECOND 60 | |
// -- Color palette for the flames | |
CRGBPalette16 gPal; | |
// SPARKING: What chance (out of 255) is there that a new spark will be lit? | |
// Higher chance = more roaring fire. Lower chance = more flickery fire. | |
// Default 120, suggested range 50-200. | |
#define SPARKING 150 | |
// -------------------------------------------------------------------------- | |
// Flame class | |
template<int NUM_LEDS, int LED_PIN> | |
class Flame | |
{ | |
private: | |
CRGB leds[NUM_LEDS]; | |
byte heat[NUM_LEDS]; | |
public: | |
Flame() | |
{ | |
for (int i = 0; i < NUM_LEDS; i++) { | |
heat[i] = 0; | |
leds[i] = CRGB::Black; | |
} | |
} | |
void init() | |
{ | |
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); | |
pinMode(LED_PIN, OUTPUT); | |
digitalWrite(LED_PIN, LOW); | |
} | |
void render(int percent) | |
{ | |
// Fire2012 by Mark Kriegsman, July 2012 | |
// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY | |
// | |
// This basic one-dimensional 'fire' simulation works roughly as follows: | |
// There's a underlying array of 'heat' cells, that model the temperature | |
// at each point along the line. Every cycle through the simulation, | |
// four steps are performed: | |
// 1) All cells cool down a little bit, losing heat to the air | |
// 2) The heat from each cell drifts 'up' and diffuses a little | |
// 3) Sometimes randomly new 'sparks' of heat are added at the bottom | |
// 4) The heat from each cell is rendered as a color into the leds array | |
// The heat-to-color mapping uses a black-body radiation approximation. | |
// | |
// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot). | |
// COOLING: How much does the air cool as it rises? | |
// Less cooling = taller flames. More cooling = shorter flames. | |
// Default 55, suggested range 20-100 | |
int cooling = 100 - ((percent * 2) / 3); | |
int cur_size = (NUM_LEDS * percent + 1) / 100; | |
// Step 1. Cool down every cell a little | |
for( int i = 0; i < cur_size; i++) { | |
heat[i] = qsub8( heat[i], random8(0, ((cooling * 10) / cur_size) + 2)); | |
} | |
// Step 2. Heat from each cell drifts 'up' and diffuses a little | |
for( int k= cur_size - 1; k >= 2; k--) { | |
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3; | |
} | |
// Step 3. Randomly ignite new 'sparks' of heat near the bottom | |
if( random8() < SPARKING ) { | |
int y = random8(7); | |
heat[y] = qadd8( heat[y], random8(100,150) ); | |
} | |
// Step 4. Map from heat cells to LED colors | |
for( int j = 0; j < cur_size; j++) { | |
// Scale the heat value from 0-255 down to 0-240 | |
// for best results with color palettes. | |
byte colorindex = scale8( heat[j], 170); | |
CRGB color = ColorFromPalette( gPal, colorindex); | |
leds[j] = color; | |
} | |
for (int j = cur_size; j < NUM_LEDS; j++) { | |
leds[j] = CRGB::Black; | |
} | |
} | |
}; | |
// -------------------------------------------------------------------------- | |
// Main logic | |
// Shelf has three separate flame strips | |
Flame<FLAME_1_SIZE, FLAME_1_PIN> flame_1; | |
Flame<FLAME_2_SIZE, FLAME_2_PIN> flame_2; | |
Flame<FLAME_3_SIZE, FLAME_3_PIN> flame_3; | |
void setup() | |
{ | |
delay(500); | |
Serial.begin(9600); | |
FastLED.setBrightness( BRIGHTNESS ); | |
// This first palette is the basic 'black body radiation' colors, | |
// which run from black to red to bright yellow to white. | |
gPal = HeatColors_p; | |
// These are other ways to set up the color palette for the 'fire'. | |
// First, a gradient from black to red to yellow to white -- similar to HeatColors_p | |
// gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White); | |
// Second, this palette is like the heat colors, but blue/aqua instead of red/yellow | |
// gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White); | |
// Third, here's a simpler, three-step gradient, from black to red to white | |
// gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White); | |
flame_1.init(); | |
flame_2.init(); | |
flame_3.init(); | |
FastLED.show(); | |
} | |
void loop() | |
{ | |
int percent = 100; | |
flame_1.render(percent); | |
flame_2.render(percent); | |
flame_3.render(percent); | |
FastLED.show(); | |
delay(1000/FRAMES_PER_SECOND); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment