ldirko

perlin fire

Jun 22nd, 2020 (edited)
1,527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Perlin noise fire procedure
  2. //16x16 rgb led matrix demo
  3. //Yaroslaw Turbin, 22.06.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. //idea in make perlin noise with time offset X and Z coord
  9. //this automatic scroll fire pattern    
  10. //and distort fire noise.
  11. //then substract Y based coodrd value to shift
  12. //fire color (not brightness) in palette.
  13. //this fadeout color from bottom matrix to up.
  14. //this need some palette tweak for good looking fire color
  15.  
  16.  
  17. #include "FastLED.h"
  18.  
  19. // Matrix size
  20. #define NUM_ROWS 16
  21. #define NUM_COLS 16
  22. #define NUM_LEDS NUM_ROWS * NUM_COLS
  23.  
  24. // LEDs pin
  25. #define DATA_PIN 3
  26.  
  27. // LED brightness
  28. #define BRIGHTNESS 255
  29.  
  30.  
  31. // Define the array of leds
  32. CRGB leds[NUM_LEDS];
  33.  
  34. DEFINE_GRADIENT_PALETTE( firepal ) {    // define fire palette
  35.    0,     0,  0,  0,   //black
  36.   32,   255,  0,  0,  // red
  37.   190, 255, 255, 0, //yellow
  38.   255,   255, 255, 255  // white
  39. };
  40.  
  41. CRGBPalette16 myPal = firepal;
  42.  
  43.  
  44. void setup() {
  45.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  46.   FastLED.setBrightness(BRIGHTNESS);
  47. }
  48.  
  49.  
  50. void loop() {
  51.  
  52.   int  a = millis();
  53.   for (int i = 0; i < NUM_COLS; i++) {
  54.     for (int j = 0; j < NUM_ROWS; j++) {
  55.       leds[XY(i,j)] = ColorFromPalette (myPal, qsub8 (inoise8 (i * 60 , j * 60+ a , a /3),
  56.       abs8(j - (NUM_ROWS-1)) * 255 / (NUM_ROWS-1)), BRIGHTNESS);   
  57.                                                                    
  58.  
  59.     }                                                              
  60.   }
  61.  
  62.  
  63.   FastLED.delay(5);
  64.  
  65. }
  66.  
  67. uint8_t XY (uint8_t x, uint8_t y) { return (y * NUM_COLS + x);}     //simple function to find led number in led matrix,
  68.                                                                     //change this to your routine
  69.                                                                     //or generate XY function for your matrix there:
  70.                                                                     //https://macetech.github.io/FastLED-XY-Map-Generator/
Add Comment
Please, Sign In to add comment