atuline

1D Fireworks for FastLED

Jul 7th, 2019
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. /* A 1D fireworks routine
  2.  *
  3.  * Found at: http://www.anirama.com/1000leds/1d-fireworks/
  4.  *  
  5.  *  Kind of fixed by: /u/AaronMickDee, /u/DeVoh and /u/Johnny5Canuck
  6.  *  
  7.  *  In this thread: https://www.reddit.com/r/FastLED/comments/c9ibnb/does_anybody_have_a_fireworks_effect_fastled/
  8.  *  
  9.  *  I didn't write this, I just hacked a couple of fixes for it. Really, it needs a complete re-write to be loop and millis() friendly.
  10.  *  
  11.  */
  12.  
  13. #include <FastLED.h>
  14. #define NUM_LEDS 120
  15. #define DATA_PIN D5
  16. CRGB leds[NUM_LEDS]; // sets up block of memory
  17.  
  18. #define NUM_SPARKS NUM_LEDS/2 // max number (could be NUM_LEDS / 2);
  19. float sparkPos[NUM_SPARKS];
  20. float sparkVel[NUM_SPARKS];
  21. float sparkCol[NUM_SPARKS];
  22. float flarePos;
  23.  
  24. float gravity = -.004; // m/s/s
  25.  
  26. void setup() {
  27.   Serial.begin(115200);
  28.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  29. }
  30.  
  31. /*
  32.  * Main Loop
  33.  */
  34. void loop() {  
  35.   // send up flare
  36.   flare();
  37.  
  38.   // explode
  39.   explodeLoop();
  40.  
  41.   // wait before sending up another
  42.   delay(random16(1000, 4000));
  43. }
  44.  
  45.  
  46. /*
  47.  * Send up a flare
  48.  *
  49.  */
  50. void flare() {
  51.  
  52.   flarePos = 0;
  53.   float flareVel = float(random16(50, 70)) / 100; // trial and error to get reasonable range
  54.   float brightness = 1;
  55.  
  56.   // initialize launch sparks
  57.   for (int i = 0; i < 5; i++) {
  58.     sparkPos[i] = 0; sparkVel[i] = (float(random8()) / 255) * (flareVel / 5); // random around 20% of flare velocity
  59.   sparkCol[i] = sparkVel[i] * 1000; sparkCol[i] = constrain(sparkCol[i], 0, 255);
  60.   }
  61.   // launch
  62. // FastLED.clear();
  63.   while (flareVel >= -.2) {
  64.     // sparks
  65.     for (int i = 0; i < 5; i++) {
  66.       sparkPos[i] += sparkVel[i];
  67.       sparkPos[i] = constrain(sparkPos[i], 0, 120);
  68.       sparkVel[i] += gravity;
  69.       sparkCol[i] += -.8;
  70.       sparkCol[i] = constrain(sparkCol[i], 0, 255);
  71.       leds[int(sparkPos[i])] = HeatColor(sparkCol[i]);
  72.       leds[int(sparkPos[i])] %= 50; // reduce brightness to 50/255
  73.     }
  74.    
  75.     // flare
  76.     leds[int(flarePos)] = CHSV(0, 0, int(brightness * 255));
  77.     FastLED.show();
  78.     delay(5);
  79.     FastLED.clear();
  80.     flarePos += flareVel;
  81.     flarePos = constrain(flarePos, 0, NUM_LEDS-1);
  82.     flareVel += gravity;
  83.     brightness *= .985;
  84.   }
  85. }
  86.  
  87. /*
  88.  * Explode!
  89.  *
  90.  * Explosion happens where the flare ended.
  91.  * Size is proportional to the height.
  92.  */
  93.  
  94.  
  95. void explodeLoop() {
  96.   int nSparks = flarePos / 2; // works out to look about right
  97.    
  98.   // initialize sparks
  99.   for (int i = 0; i < nSparks; i++) {
  100.     sparkPos[i] = flarePos; sparkVel[i] = (float(random16(0, 20000)) / 10000.0) - 1.0; // from -1 to 1
  101.     sparkCol[i] = abs(sparkVel[i]) * 500; // set colors before scaling velocity to keep them bright
  102.     sparkCol[i] = constrain(sparkCol[i], 0, 255);
  103.     sparkVel[i] *= flarePos / NUM_LEDS; // proportional to height
  104.   }
  105.   sparkCol[0] = 255; // this will be our known spark
  106.   float dying_gravity = gravity;
  107.   float c1 = 120;
  108.   float c2 = 50;
  109.  
  110.  
  111.   while(sparkCol[0] > c2/128) { // as long as our known spark is lit, work with all the sparks
  112.     delay(0);
  113.     FastLED.clear();
  114.    
  115.     for (int i = 0; i < nSparks; i++) {
  116.       sparkPos[i] += sparkVel[i];
  117.       sparkPos[i] = constrain(sparkPos[i], 0, NUM_LEDS-1);
  118.       sparkVel[i] += dying_gravity;
  119.       sparkCol[i] *= .99;
  120.       sparkCol[i] = constrain(sparkCol[i], 0, 255); // red cross dissolve
  121.      
  122.       if(sparkCol[i] > c1) { // fade white to yellow
  123.         leds[int(sparkPos[i])] = CRGB(255, 255, (255 * (sparkCol[i] - c1)) / (255 - c1));
  124.       }
  125.       else if (sparkCol[i] < c2) { // fade from red to black
  126.         leds[int(sparkPos[i])] = CRGB((255 * sparkCol[i]) / c2, 0, 0);
  127.       }
  128.       else { // fade from yellow to red
  129.         leds[int(sparkPos[i])] = CRGB(255, (255 * (sparkCol[i] - c2)) / (c1 - c2), 0);
  130.       }
  131.  
  132.      
  133.     }
  134.     dying_gravity *= .995; // as sparks burn out they fall slower
  135.     FastLED.show();
  136.    
  137.   }
  138.  
  139.   delay(5);
  140.   FastLED.clear();
  141.   FastLED.show();
  142. }
Add Comment
Please, Sign In to add comment