ldirko

Fire speedup with buffer scroll

Sep 1st, 2020 (edited)
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. //Perlin noise fire procedure
  2. //16x16 rgb led matrix demo
  3. //Yaroslaw Turbin, 01.09.2020
  4. //https://vk.com/ldirko
  5. //https://www.reddit.com/user/ldirko/
  6. //https://www.reddit.com/r/FastLED/comments/hgu16i/my_fire_effect_implementation_based_on_perlin/
  7.  
  8. //speedup my old fire procedure https://pastebin.com/jSSVSRi6
  9. //idea is precalculate noise table in textrure,
  10. //then fake scroll it with shift offset
  11. //like in digital rain procedure https://pastebin.com/1yymjFxR
  12.  
  13. #include "FastLED.h"
  14.  
  15. // Matrix size
  16. #define NUM_ROWS 16
  17. #define NUM_COLS 16
  18. #define NUM_LEDS NUM_ROWS * NUM_COLS
  19.  
  20. // LEDs pin
  21. #define DATA_PIN 3
  22.  
  23. // LED brightness
  24. #define BRIGHTNESS 255
  25.  
  26. // Define the array of leds
  27. CRGB leds[NUM_LEDS];
  28. #define NOISE_HEIGHT NUM_COLS*4
  29. byte noises [NUM_COLS*NOISE_HEIGHT]; //precalculated noise table
  30. byte colorfade [NUM_ROWS]; //simple colorfade table for speedup
  31.  
  32. DEFINE_GRADIENT_PALETTE( firepal ) { // define fire palette
  33. 0, 0, 0, 0, //black
  34. 32, 255, 0, 0, // red
  35. 190, 255, 255, 0, //yellow
  36. 255, 255, 255, 255 // white
  37. };
  38.  
  39. CRGBPalette16 myPal = firepal;
  40. byte a = 0;
  41.  
  42. void setup() {
  43.  
  44. FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  45. FastLED.setBrightness(BRIGHTNESS);
  46.  
  47. for (int i = 0; i < NUM_COLS; i++) {
  48. for (int j = 0; j < (NOISE_HEIGHT); j++) {
  49. noises [j*NUM_COLS+i] = inoise8 (i * 60 , j * 60 ); //init noise buffer
  50. }}
  51. for (int j = 0; j < NUM_ROWS; j++) {
  52. colorfade [j]= abs8(j - (NUM_ROWS-1)) * 255 / (NUM_ROWS-1); // init colorfade table
  53. }
  54.  
  55. }//setup
  56.  
  57. void loop() {
  58.  
  59. for (int i = 0; i < NUM_COLS; i++) {
  60. for (int j = 0; j < NUM_ROWS; j++) {
  61. int index = (j+a+random8(2))%(NOISE_HEIGHT)*NUM_COLS; //roll index in noise buffer
  62. leds[XY(i,j)] = ColorFromPalette (myPal, qsub8(noises [i+index], colorfade [j]), BRIGHTNESS);
  63. }}
  64. FastLED.delay(20);
  65. a++;
  66.  
  67. }//loop
  68.  
  69. uint16_t XY (uint8_t x, uint8_t y) { return (y * NUM_COLS + x);} //simple function to find led number in led matrix,
  70. //change this to your routine
  71. //or generate XY function for your matrix there:
  72. //https://macetech.github.io/FastLED-XY-Map-Generator/
Add Comment
Please, Sign In to add comment