Snippets

Lisa Fire fabric

Created by Lisa
#include <FastLED.h>

#define LED_PIN_1     9
#define LED_PIN_2     12
#define LED_PIN_3     6
#define COLOR_ORDER GRB
#define CHIPSET     WS2812
#define NUM_LEDS_1    20
#define NUM_LEDS_2    19
#define NUM_LEDS_3    20

#define BRIGHTNESS  200
#define FRAMES_PER_SECOND 20

bool gReverseDirection = false;

CRGB leds1[NUM_LEDS_1];
CRGB leds2[NUM_LEDS_2];
CRGB leds3[NUM_LEDS_3];

static byte heat1[NUM_LEDS_1];    // separate heat arrays for all 3 strips
static byte heat2[NUM_LEDS_2];
static byte heat3[NUM_LEDS_3];

void setup() {
  delay(2000); // sanity delay
  FastLED.addLeds<CHIPSET, LED_PIN_1, COLOR_ORDER>(leds1, NUM_LEDS_1).setCorrection( TypicalLEDStrip );
  FastLED.addLeds<CHIPSET, LED_PIN_2, COLOR_ORDER>(leds2, NUM_LEDS_2).setCorrection( TypicalLEDStrip );
  FastLED.addLeds<CHIPSET, LED_PIN_3, COLOR_ORDER>(leds3, NUM_LEDS_3).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );
}

void loop()
{
  // Add entropy to random number generator; we use a lot of it.
  // random16_add_entropy( random());

  Fire2012_1(); // run simulation frame
  Fire2012_2();
  Fire2012_3();
  
  FastLED.show(); // display this frame
  FastLED.delay(1000 / FRAMES_PER_SECOND);
}


// 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  45

// 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 100


void Fire2012_1()
{
// Array of temperature readings at each simulation cell
  static byte heat1[NUM_LEDS_1];

  // Step 1.  Cool down every cell a little
    for( int i = 0; i < NUM_LEDS_1; i++) {
      heat1[i] = qsub8( heat1[i],  random8(0, ((COOLING * 10) / NUM_LEDS_1) + 2));
    }
  
    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for( int k= NUM_LEDS_1 - 1; k >= 2; k--) {
      heat1[k] = (heat1[k - 1] + heat1[k - 2] + heat1[k - 2] ) / 3;
    }
    
    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
    if( random8() < SPARKING ) {
      int y = random8(7);
      heat1[y] = qadd8( heat1[y], random8(160,255) );
    }

    // Step 4.  Map from heat cells to LED colors
    for( int j = 0; j < NUM_LEDS_1; j++) {
      CRGB color = HeatColor( heat1[j]);
      int pixelnumber;
      if( gReverseDirection ) {
        pixelnumber = (NUM_LEDS_1-1) - j;
      } else {
        pixelnumber = j;
      }
      leds1[pixelnumber] = color;
    }
}
void Fire2012_2()
{
// Array of temperature readings at each simulation cell
  static byte heat2[NUM_LEDS_2];

  // Step 1.  Cool down every cell a little
    for( int i = 0; i < NUM_LEDS_2; i++) {
      heat2[i] = qsub8( heat2[i],  random8(0, ((COOLING * 10) / NUM_LEDS_2) + 2));
    }
  
    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for( int k= NUM_LEDS_2 - 1; k >= 2; k--) {
      heat2[k] = (heat2[k - 1] + heat2[k - 2] + heat2[k - 2] ) / 3;
    }
    
    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
    if( random8() < SPARKING ) {
      int y = random8(7);
      heat2[y] = qadd8( heat2[y], random8(160,255) );
    }

    // Step 4.  Map from heat cells to LED colors
    for( int j = 0; j < NUM_LEDS_2; j++) {
      CRGB color = HeatColor( heat2[j]);
      int pixelnumber;
      if( gReverseDirection ) {
        pixelnumber = (NUM_LEDS_2-1) - j;
      } else {
        pixelnumber = j;
      }
      leds2[pixelnumber] = color;
    }
}
void Fire2012_3()
{
// Array of temperature readings at each simulation cell
  static byte heat3[NUM_LEDS_3];

  // Step 1.  Cool down every cell a little
    for( int i = 0; i < NUM_LEDS_3; i++) {
      heat3[i] = qsub8( heat3[i],  random8(0, ((COOLING * 10) / NUM_LEDS_3) + 2));
    }
  
    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for( int k= NUM_LEDS_3 - 1; k >= 2; k--) {
      heat3[k] = (heat3[k - 1] + heat3[k - 2] + heat3[k - 2] ) / 3;
    }
    
    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
    if( random8() < SPARKING ) {
      int y = random8(7);
      heat3[y] = qadd8( heat3[y], random8(160,255) );
    }

    // Step 4.  Map from heat cells to LED colors
    for( int j = 0; j < NUM_LEDS_3; j++) {
      CRGB color = HeatColor( heat3[j]);
      int pixelnumber;
      if( gReverseDirection ) {
        pixelnumber = (NUM_LEDS_3-1) - j;
      } else {
        pixelnumber = j;
      }
      leds3[pixelnumber] = color;
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.