Skip to content

Instantly share code, notes, and snippets.

@caitlinsdad
Created August 11, 2018 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caitlinsdad/a6f4c6b96f3eebea65bd6a5586c0bef7 to your computer and use it in GitHub Desktop.
Save caitlinsdad/a6f4c6b96f3eebea65bd6a5586c0bef7 to your computer and use it in GitHub Desktop.
Flora Fastled Multistrip Flames
#include <FastLED.h>
#define LED_PIN1 9
#define LED_PIN2 10
#define LED_PIN3 6
#define BUTTON 11
//#define COLOR_ORDER GRB
//#define CHIPSET NEOPIXEL
#define NUM_STRIPS 3
#define NUM_LEDS 30
#define BRIGHTNESS 50
#define FRAMES_PER_SECOND 60
//=======================================
CRGBPalette16 gPal;
bool gReverseDirection = false;
CRGB leds[NUM_STRIPS][NUM_LEDS];
void setup() {
Serial.begin(9600);
delay(2000); // sanity delay
FastLED.addLeds<NEOPIXEL, LED_PIN1>(leds[0], NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds<NEOPIXEL, LED_PIN2>(leds[1], NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds<NEOPIXEL, LED_PIN3>(leds[2], NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
}
//============================================================
void loop()
{
FastLED.delay(2000);
// Add entropy to random number generator; we use a lot of it.
random16_add_entropy( random());
gPal = HeatColors_p;
for( int q=0;q<350;q++){
Fire2012(); // run simulation frame
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
gPal = CRGBPalette16( CRGB::Black, CRGB::DarkViolet, CRGB::Purple, CRGB::White );
for( int q=0;q<400;q++){
Fire2012(); // run simulation frame
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
for( int q=0;q<400;q++){
Fire2012(); // run simulation frame
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
// -------------------------------
ClearAll();
for( int q=0;q<200;q++){
Sparkle(0xff, 0xff, 0xff, 100);
Sparkle(0, 0xff, 0xff, 50);
}
}
//============================================================
// 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).
//
// This simulation scales it self a bit depending on NUM_LEDS; it should look
// "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
//
// I recommend running this simulation at anywhere from 30-100 frames per second,
// meaning an interframe delay of about 10-35 milliseconds.
//
// Looks best on a high-density LED setup (60+ pixels/meter).
//
//
// There are two main parameters you can play with to control the look and
// feel of your fire: COOLING (used in step 1 above), and SPARKING (used
// in step 3 above).
//
// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames. More cooling = shorter flames.
// Default 50, suggested range 20-100
#define COOLING 100
// 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 120
void Fire2012()
{
// Array of temperature readings at each simulation cell
static byte heat[NUM_STRIPS][NUM_LEDS];
// This outer loop will go over each strip, one at a time
for(int x = 0; x < NUM_STRIPS; x++) {
// Step 1. Cool down every cell a little
for( int i = 0; i < NUM_LEDS; i++) {
heat[x][i] = qsub8( heat[x][i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for( int k= NUM_LEDS - 1; k >= 2; k--) {
heat[x][k] = (heat[x][k - 1] + heat[x][k - 2] + heat[x][k - 2] ) / 3;
}
// Step 3. Randomly ignite new 'sparks' of heat near the bottom
if( random8() < SPARKING ) {
int y = random8(7);
heat[x][y] = qadd8(heat[x][y],random8(160,255) );
}
// Step 4. Map from heat cells to LED colors
for( int j = 0; j < NUM_LEDS; j++) {
CRGB color = ColorFromPalette( gPal, heat[x][j]);
int pixelnumber;
if( gReverseDirection ) {
pixelnumber = (NUM_LEDS-1) - j;
} else {
pixelnumber = j;
}
leds[x][pixelnumber] = color;
//leds[x][pixelnumber] = ColorFromPalette( myPal, heat[x][j]); // custom palette
}
}
}
//============================================================
void ClearAll() {
for(int xx=0; xx < NUM_STRIPS; xx++) {
for(int yy=0; yy < NUM_LEDS; yy++) {
leds[xx][yy] = CRGB::Black;
}
}
FastLED.show();
}
void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
int Strip = random(NUM_STRIPS);
int Pixel = random(NUM_LEDS);
leds[Strip][Pixel].r = red;
leds[Strip][Pixel].g = green;
leds[Strip][Pixel].b = blue;
FastLED.show();
FastLED.delay(random(SpeedDelay));
leds[Strip][Pixel] = CRGB::Black;
FastLED.show();
FastLED.delay(random(SpeedDelay));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment