Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 2D Fire2012 with Palette
- *
- * By: Mark Kriegsman
- *
- * Small fix by: Andrew Tuline
- *
- */
- #include "FastLED.h"
- #define LED_DT 12
- #define LED_CK 11
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812
- #define BRIGHTNESS 255
- #define FRAMES_PER_SECOND 60
- // Params for width and height
- const uint8_t kMatrixWidth = 8;
- const uint8_t kMatrixHeight = 8;
- // Param for different pixel layouts
- const bool kMatrixSerpentineLayout = true;
- #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
- //#define NUM_LEDS 8
- struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
- CRGBPalette16 gPal;
- void setup() {
- delay(1000); // sanity delay
- Serial.begin(57600);
- // FastLED.addLeds<CHIPSET, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);
- FastLED.addLeds<CHIPSET, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
- FastLED.setBrightness( BRIGHTNESS );
- gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Orange, CRGB::Yellow);
- // gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
- // gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);
- }
- void loop()
- {
- random16_add_entropy(random());
- Fire2012WithPalettexy();
- //Fire2012WithPalette();
- FastLED.show();
- FastLED.delay(20);
- // FastLED.delay(1000 / FRAMES_PER_SECOND);
- }
- // COOLING: How much does the air cool as it rises?
- // Less cooling = taller flames. More cooling = shorter flames.
- // Default 55, suggested range 20-100
- #define COOLING 40
- // 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 60
- void Fire2012WithPalettexy() {
- // Array of temperature readings at each simulation cell
- static byte heat[kMatrixWidth][kMatrixHeight];
- for (int mw = 0; mw < kMatrixWidth; mw++) { // Move along the width of the flame
- // Step 1. Cool down every cell a little
- for (int i = 0; i < kMatrixHeight; i++) {
- heat[mw][i] = qsub8( heat[mw][i], random16(0, ((COOLING * 10) / kMatrixHeight) + 2));
- }
- // Step 2. Heat from each cell drifts 'up' and diffuses a little
- for (int k = kMatrixHeight - 1; k >= 2; k--) {
- heat[mw][k] = (heat[mw][k - 1] + heat[mw][k - 2] + heat[mw][k - 2] ) / 3;
- }
- // Step 3. Randomly ignite new 'sparks' of heat near the bottom
- if (random8(0,255) < SPARKING ) {
- int y = random8(3);
- heat[mw][y] = qadd8( heat[mw][y], random8(160,255) );
- }
- // Step 4. Map from heat cells to LED colors
- for (int j = 0; j < kMatrixHeight; j++) {
- byte colorindex = scale8( heat[mw][j], 240);
- leds[ XY(j, mw)] = ColorFromPalette( gPal, colorindex);
- }
- } // for mw
- } // Fire2012WithPalette()
- uint16_t XY( uint8_t x, uint8_t y)
- {
- uint16_t i;
- if( kMatrixSerpentineLayout == false) {
- i = (y * kMatrixWidth) + x;
- }
- if( kMatrixSerpentineLayout == true) {
- if( y & 0x01) {
- // Odd rows run backwards
- uint8_t reverseX = (kMatrixWidth - 1) - x;
- i = (y * kMatrixWidth) + reverseX;
- } else {
- // Even rows run forwards
- i = (y * kMatrixWidth) + x;
- }
- }
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement