Advertisement
atuline

2D Fire2012 with Palette

Nov 10th, 2016
945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. /* 2D Fire2012 with Palette
  2.  *  
  3.  *  By: Mark Kriegsman
  4.  *  
  5.  *  Small fix by: Andrew Tuline
  6.  *  
  7.  */
  8.  
  9.  
  10. #include "FastLED.h"
  11.  
  12. #define LED_DT 12
  13. #define LED_CK 11
  14. #define COLOR_ORDER GRB
  15. #define CHIPSET    WS2812
  16.  
  17.  
  18. #define BRIGHTNESS  255
  19. #define FRAMES_PER_SECOND 60
  20.  
  21.  
  22. // Params for width and height
  23. const uint8_t kMatrixWidth = 8;
  24. const uint8_t kMatrixHeight = 8;
  25. // Param for different pixel layouts
  26. const bool    kMatrixSerpentineLayout = true;
  27.  
  28.  
  29. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  30. //#define NUM_LEDS    8
  31.  
  32. struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
  33.  
  34. CRGBPalette16 gPal;
  35.  
  36.  
  37. void setup() {
  38.   delay(1000); // sanity delay
  39.   Serial.begin(57600);
  40. //  FastLED.addLeds<CHIPSET, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);
  41.   FastLED.addLeds<CHIPSET, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  42.     FastLED.setBrightness( BRIGHTNESS );
  43.  
  44.   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Orange, CRGB::Yellow);
  45.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua,  CRGB::White);
  46.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);
  47. }
  48.  
  49. void loop()
  50. {
  51.   random16_add_entropy(random());
  52.  
  53.   Fire2012WithPalettexy();
  54.   //Fire2012WithPalette();
  55.  
  56.   FastLED.show();
  57.   FastLED.delay(20);
  58. //  FastLED.delay(1000 / FRAMES_PER_SECOND);
  59. }
  60.  
  61. // COOLING: How much does the air cool as it rises?
  62. // Less cooling = taller flames.  More cooling = shorter flames.
  63. // Default 55, suggested range 20-100
  64. #define COOLING  40
  65.  
  66. // SPARKING: What chance (out of 255) is there that a new spark will be lit?
  67. // Higher chance = more roaring fire.  Lower chance = more flickery fire.
  68. // Default 120, suggested range 50-200.
  69. #define SPARKING 60
  70.  
  71.  
  72.  
  73. void Fire2012WithPalettexy() {
  74.  
  75. // Array of temperature readings at each simulation cell
  76.   static byte heat[kMatrixWidth][kMatrixHeight];
  77.  
  78.   for (int mw = 0; mw < kMatrixWidth; mw++) {        // Move along the width of the flame
  79.  
  80.   // Step 1.  Cool down every cell a little
  81.     for (int i = 0; i < kMatrixHeight; i++) {
  82.       heat[mw][i] = qsub8( heat[mw][i],  random16(0, ((COOLING * 10) / kMatrixHeight) + 2));
  83.     }
  84.  
  85.     // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  86.     for (int k = kMatrixHeight - 1; k >= 2; k--) {
  87.       heat[mw][k] = (heat[mw][k - 1] + heat[mw][k - 2] + heat[mw][k - 2] ) / 3;
  88.     }
  89.    
  90.     // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  91.     if (random8(0,255) < SPARKING ) {
  92.       int y = random8(3);
  93.       heat[mw][y] = qadd8( heat[mw][y], random8(160,255) );
  94.     }
  95.  
  96.     // Step 4.  Map from heat cells to LED colors
  97.     for (int j = 0; j < kMatrixHeight; j++) {
  98.       byte colorindex = scale8( heat[mw][j], 240);
  99.       leds[ XY(j, mw)] = ColorFromPalette( gPal, colorindex);
  100.     }
  101.  
  102.   } // for mw
  103. } // Fire2012WithPalette()
  104.  
  105.  
  106.  
  107.  
  108.  
  109. uint16_t XY( uint8_t x, uint8_t y)
  110. {
  111.  
  112.   uint16_t i;
  113.  
  114.   if( kMatrixSerpentineLayout == false) {
  115.     i = (y * kMatrixWidth) + x;
  116.   }
  117.  
  118.   if( kMatrixSerpentineLayout == true) {
  119.     if( y & 0x01) {
  120.       // Odd rows run backwards
  121.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  122.       i = (y * kMatrixWidth) + reverseX;
  123.     } else {
  124.       // Even rows run forwards
  125.       i = (y * kMatrixWidth) + x;
  126.     }
  127.   }
  128.   return i;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement