Advertisement
Guest User

ESP8266 code for FastLED example Fire2018_2

a guest
Oct 20th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #define FASTLED_ALLOW_INTERRUPTS 0
  2. #define INTERRUPT_THRESHOLD 1
  3. #define FASTLED_INTERRUPT_RETRY_COUNT 0
  4.  
  5. #include "FastLED.h"
  6.  
  7. // matrix size
  8. uint8_t Width  = 7;
  9. uint8_t Height = 25;
  10. uint8_t CentreX =  (Width / 2) - 1;
  11. uint8_t CentreY = (Height / 2) - 1;
  12.  
  13. // NUM_LEDS = Width * Height
  14. #define NUM_LEDS      175
  15. #define BRIGHTNESS    255
  16. #define DATA_PIN      D4
  17. #define LED_TYPE      WS2812
  18. #define COLOR_ORDER   GRB
  19.  
  20. CRGB leds[NUM_LEDS];
  21.  
  22.  
  23. // parameters and buffer for the noise array
  24. #define NUM_LAYERS 2
  25. uint32_t x[NUM_LAYERS];
  26. uint32_t y[NUM_LAYERS];
  27. uint32_t z[NUM_LAYERS];
  28. uint32_t scale_x[NUM_LAYERS];
  29. uint32_t scale_y[NUM_LAYERS];
  30.  
  31. uint8_t noise[NUM_LAYERS][16][16];
  32. uint8_t noise2[NUM_LAYERS][16][16];
  33.  
  34. uint8_t heat[256];
  35.  
  36. void setup() {
  37.  
  38.   Serial.begin(115200);
  39.   // Adjust this for you own setup. Use the hardware SPI pins if possible.
  40.   // On Teensy 3.1/3.2 the pins are 11 & 13
  41.   // Details here: https://github.com/FastLED/FastLED/wiki/SPI-Hardware-or-Bit-banging
  42.   // In case you see flickering / glitching leds, reduce the data rate to 12 MHZ or less
  43.   LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  44.   FastLED.setBrightness(BRIGHTNESS);
  45.   FastLED.setDither(DISABLE_DITHER);
  46. }
  47.  
  48. void loop() {
  49.   Fire2018_2();
  50.   //show_fps();
  51. }
  52.  
  53. // check the Serial Monitor for fps rate
  54. void show_fps() {
  55.   EVERY_N_MILLIS(100) {
  56.     Serial.println(LEDS.getFPS());
  57.   }
  58. }
  59.  
  60. // this finds the right index within a serpentine matrix
  61. uint16_t XY( uint8_t x, uint8_t y) {
  62.   uint16_t i;
  63.   if ( y & 0x01) {
  64.     uint8_t reverseX = (Width - 1) - x;
  65.     i = (y * Width) + reverseX;
  66.   } else {
  67.     i = (y * Width) + x;
  68.   }
  69.   return i;
  70. }
  71.  
  72. void Fire2018_2() {
  73.  
  74.   // some changing values
  75.   uint16_t ctrl1 = inoise16(11 * millis(), 0, 0);
  76.   uint16_t ctrl2 = inoise16(13 * millis(), 100000, 100000);
  77.   uint16_t  ctrl = ((ctrl1 + ctrl2) / 2);
  78.  
  79.   // parameters for the heatmap
  80.   uint16_t speed = 25;
  81.   x[0] = 3 * ctrl * speed;
  82.   y[0] = 20 * millis() * speed;
  83.   z[0] = 5 * millis() * speed ;
  84.   scale_x[0] = ctrl1 / 2;
  85.   scale_y[0] = ctrl2 / 2;
  86.  
  87.   //calculate the noise data
  88.   uint8_t layer = 0;
  89.   for (uint8_t i = 0; i < Width; i++) {
  90.     uint32_t ioffset = scale_x[layer] * (i - CentreX);
  91.     for (uint8_t j = 0; j < Height; j++) {
  92.       uint32_t joffset = scale_y[layer] * (j - CentreY);
  93.       uint16_t data = ((inoise16(x[layer] + ioffset, y[layer] + joffset, z[layer])) + 1);
  94.       noise[layer][i][j] = data >> 8;
  95.     }
  96.   }
  97.  
  98.   // parameters for te brightness mask
  99.   speed = 20;
  100.   x[1] = 3 * ctrl * speed;
  101.   y[1] = 20 * millis() * speed;
  102.   z[1] = 5 * millis() * speed ;
  103.   scale_x[1] = ctrl1 / 2;
  104.   scale_y[1] = ctrl2 / 2;
  105.  
  106.   //calculate the noise data
  107.   layer = 1;
  108.   for (uint8_t i = 0; i < Width; i++) {
  109.     uint32_t ioffset = scale_x[layer] * (i - CentreX);
  110.     for (uint8_t j = 0; j < Height; j++) {
  111.       uint32_t joffset = scale_y[layer] * (j - CentreY);
  112.       uint16_t data = ((inoise16(x[layer] + ioffset, y[layer] + joffset, z[layer])) + 1);
  113.       noise[layer][i][j] = data >> 8;
  114.     }
  115.   }
  116.  
  117.   // draw lowest line - seed the fire
  118.   for (uint8_t x = 0; x < Width; x++) {
  119.     heat[XY(x, 15)] =  noise[0][15 - x][7];
  120.   }
  121.  
  122.  
  123.   //copy everything one line up
  124.   for (uint8_t y = 0; y < Height - 1; y++) {
  125.     for (uint8_t x = 0; x < Width; x++) {
  126.       heat[XY(x, y)] = heat[XY(x, y + 1)];
  127.     }
  128.   }
  129.  
  130.   //dim
  131.   for (uint8_t y = 0; y < Height - 1; y++) {
  132.     for (uint8_t x = 0; x < Width; x++) {
  133.       uint8_t dim = noise[0][x][y];
  134.       // high value = high flames
  135.       dim = dim / 1.7;
  136.       dim = 255 - dim;
  137.       heat[XY(x, y)] = scale8(heat[XY(x, y)] , dim);
  138.     }
  139.   }
  140.  
  141.   for (uint8_t y = 0; y < Height; y++) {
  142.     for (uint8_t x = 0; x < Width; x++) {
  143.       // map the colors based on heatmap
  144.       leds[XY(x, y)] = CRGB( heat[XY(x, y)], 1 , 0);
  145.       // dim the result based on 2nd noise layer
  146.       leds[XY(x, y)].nscale8(noise[1][x][y]);
  147.     }
  148.   }
  149.  
  150.   FastLED.show();
  151.   delay(8);
  152.   //delay(50);
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement