Guest User

Untitled

a guest
Feb 3rd, 2021
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.66 KB | None | 0 0
  1. // Fire 'Simulator' @fotoshopist 2020
  2. // When you 1st run this, don't panic if you see nothing. It takes a bit for it to "catch fire"
  3.  
  4. #include "FastLED.h"
  5.  
  6. const uint8_t MatrixHeight  = 8;
  7. const uint8_t MatrixWidth = 32;
  8. const bool    SerpentineLayout = true;  //For LEDs that are wired back and forth rather than row by row
  9.  
  10. #define DATA_PIN    15
  11. #define SEED_PIN    32  //Any unused analog pin
  12. #define LED_TYPE    WS2812B
  13. #define COLOR_ORDER GRB
  14. #define NUM_LEDS    (MatrixWidth * MatrixHeight)
  15. #define BRIGHTNESS  100
  16.  
  17. //Color Palette(s)
  18. DEFINE_GRADIENT_PALETTE( fire_pal ) {
  19.   0,     0,  0,  0,    //black
  20.   80,   255,  0,  0,   //red
  21.   230,   255, 255,  0, //bright yellow
  22.   255,   255, 255, 40  //full white
  23. };
  24.  
  25. DEFINE_GRADIENT_PALETTE( ice_pal ) {
  26.   0,     0,  0,  0,    //black
  27.   80,   0,  0,  255,   //Dark Blue
  28.   230,   0, 255,  255, //bright Blueish
  29.   255,   20, 255, 255  //full white
  30. };
  31.  
  32. DEFINE_GRADIENT_PALETTE( emrld_pal ) {
  33.   0,     0,  0,  0,      //black
  34.   80,    0,  255,  0,    //Dark green
  35.   230,   100, 255, 100,  //bright Greenish
  36.   255,   50, 255, 50     //full white
  37. };
  38.  
  39. CRGBArray<NUM_LEDS> ledset;
  40. byte iMap[NUM_LEDS]; //array for intesity values
  41.  
  42. void setup() {
  43.   random16_set_seed(analogRead(SEED_PIN)); //read an unused pins floating voltage to use as our RND seed
  44.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(ledset, NUM_LEDS).setCorrection(TypicalLEDStrip);
  45.   FastLED.setBrightness(BRIGHTNESS);
  46.   delay(1000); //start up delay
  47. }
  48.  
  49. void loop()
  50. {
  51.   Fire();
  52.   FastLED.delay(40); // Adjust speed of fire(40-55 looks good)
  53. }
  54.  
  55. //Flames based on reducing average
  56. void Fire() {
  57.  
  58.   //nth Row for map
  59.   for (int row = (MatrixHeight - 1); row >= 1; row--) {
  60.     for (int col = (MatrixWidth - 2); col >= 1; col--) {
  61.       int newcol = ((iMap[XY(row - 1, col - 1)] + iMap[XY(row - 1, col)] + iMap[XY(row - 1, col + 1)]) / 3) / random8(1, 5);
  62.       iMap[XY(row, col)] = newcol;
  63.     }
  64.   }
  65.  
  66.   // Creation Row
  67.   // If one of the LEDs on either side is already 'on fire' then there is a higher chance of ignition
  68.   // If not, it has a 'very' low chance to ignite
  69.   byte catchOn = 50;    // Chance to catch on becuase cell beside us is on fire - higher number less chance (0-254) default: 50
  70.   byte ignite = 253;    // Chance to spontaneously ignight - higher number less chance (0-254) default: 253
  71.   byte chgchance = 245; // higher number less chance (0-254) default: 245
  72.  
  73.   for (int i = 0; i <= (MatrixWidth - 1); i++) {
  74.     byte docreate = random8();
  75.     if (docreate > chgchance) {
  76.       byte rCol = random8();
  77.       if (i >= 1 && i <= (MatrixWidth - 2)) {
  78.         if (iMap[XY(0, i - 1)] > 0 || iMap[XY(0, i + 1)] > 0) {
  79.           if (rCol > catchOn) {
  80.             rCol = random8(250, 255);
  81.           } else {
  82.             rCol = 0;
  83.           }
  84.         } else {
  85.           if (rCol > ignite) {
  86.             rCol = 255;
  87.           } else {
  88.             rCol = 0;
  89.           }
  90.         }
  91.       } else {
  92.         if (rCol > ignite) {
  93.           rCol = 255;
  94.         } else {
  95.           rCol = 0;
  96.         }
  97.       }
  98.       iMap[XY(0, i)] = rCol;
  99.     }
  100.   }
  101.  
  102.   //apply the colors to the CRGB array
  103.   CRGBPalette256 curPal = fire_pal; //select the pallet to use
  104.   for (int i = 0; i <= (NUM_LEDS - 1); i++) {
  105.     ledset[i] = ColorFromPalette(curPal, iMap[i]);
  106.   }
  107. }
  108.  
  109. //Turn string of LEDs to coordinates X/Y
  110. uint16_t XY( uint8_t x, uint8_t y) {
  111.   uint16_t i;
  112.   if ( SerpentineLayout == false) {
  113.     i = (y * MatrixHeight) + x;
  114.   } else {
  115.     if ( y & 0x01) {
  116.       uint8_t reverseX = (MatrixHeight - 1) - x;
  117.       i = (y * MatrixHeight) + reverseX;
  118.     } else {
  119.       i = (y * MatrixHeight) + x;
  120.     }
  121.   }
  122.  
  123.   return i;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment