Advertisement
Guest User

Untitled

a guest
Jan 1st, 2024
1,155
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 1 0
  1. /*  1D Fireworks by Daniel Westhof https://www.Daniel-Westhof.de
  2.  *  Based on original code by Carl Rosendahl: https://www.anirama.com/1000leds/1d-fireworks/
  3.  *  
  4.  *  What I changed:
  5.  *  Added an LED to mime a fuse.
  6.  *  Added a button to "light" the fuse.
  7.  *  
  8.  *  The code is configured to run on a D1-Mini Board and a WS2812B LED-strip with 300 LEDs.
  9.  *  If you want to use more or less LEDs, you might want to to change the gravity value as well.
  10.  */
  11.  
  12. #include <FastLED.h>
  13.  
  14. #define NUM_LEDS 300
  15. #define DATA_PIN 5
  16. #define BUTTON_PIN 13
  17. #define LED_PIN 15
  18. #define NUM_SPARKS NUM_LEDS/2 // max number (could be NUM_LEDS / 2)
  19.  
  20. CRGB leds[NUM_LEDS]; // sets up block of memory
  21.  
  22. float sparkPos[NUM_SPARKS];
  23. float sparkVel[NUM_SPARKS];
  24. float sparkCol[NUM_SPARKS];
  25. float flarePos;
  26.  
  27. float gravity = -.008; // m/s/s
  28.  
  29. void setup() {
  30.   Serial.begin(115200);
  31.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  32.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  33.   pinMode(LED_PIN, OUTPUT);
  34. }
  35.  
  36. void loop() {  
  37.  
  38.  
  39.   digitalWrite(LED_PIN, HIGH);
  40.   Serial.println("Wait");
  41.   while (digitalRead(BUTTON_PIN) == HIGH) {
  42.   delay(100);
  43.   }
  44.  
  45.   for (int i = 30; i > 0; i--) {
  46.     digitalWrite(LED_PIN, LOW);
  47.     delay(50);
  48.     digitalWrite(LED_PIN, HIGH);
  49.     delay(50);
  50.     Serial.println("zzzzzz");
  51.   }
  52.  
  53.   Serial.println("BOOM");
  54.   digitalWrite(LED_PIN, LOW);
  55.  
  56.   // send up flare
  57.   flare();
  58.  
  59.   // explode
  60.   explodeLoop();
  61. }
  62.  
  63.  
  64. /*
  65.  * Send up a flare
  66.  *
  67.  */
  68. void flare() {
  69.  
  70.   flarePos = 0;
  71.   float flareVel = float(random16(180, 195)) / 100; // trial and error to get reasonable range
  72.   float brightness = 1;
  73.  
  74.   // initialize launch sparks
  75.   for (int i = 0; i < 5; i++) {
  76.     sparkPos[i] = 0; sparkVel[i] = (float(random8()) / 255) * (flareVel / 5); // random around 20% of flare velocity
  77.   sparkCol[i] = sparkVel[i] * 1000; sparkCol[i] = constrain(sparkCol[i], 0, 255);
  78.   }
  79.   // launch
  80. // FastLED.clear();
  81.   while (flareVel >= -.2) {
  82.     // sparks
  83.     for (int i = 0; i < 5; i++) {
  84.       sparkPos[i] += sparkVel[i];
  85.       sparkPos[i] = constrain(sparkPos[i], 0, 120);
  86.       sparkVel[i] += gravity;
  87.       sparkCol[i] += -.8;
  88.       sparkCol[i] = constrain(sparkCol[i], 0, 255);
  89.       leds[int(sparkPos[i])] = HeatColor(sparkCol[i]);
  90.       leds[int(sparkPos[i])] %= 50; // reduce brightness to 50/255
  91.     }
  92.    
  93.     // flare
  94.     leds[int(flarePos)] = CHSV(0, 0, int(brightness * 255));
  95.     FastLED.show();
  96.     delay(5);
  97.     FastLED.clear();
  98.     flarePos += flareVel;
  99.     flarePos = constrain(flarePos, 0, NUM_LEDS-1);
  100.     flareVel += gravity;
  101.     brightness *= .98;
  102.   }
  103. }
  104.  
  105. /*
  106.  * Explode!
  107.  *
  108.  * Explosion happens where the flare ended.
  109.  * Size is proportional to the height.
  110.  */
  111.  
  112.  
  113. void explodeLoop() {
  114.   int nSparks = flarePos / 2; // works out to look about right
  115.    
  116.   // initialize sparks
  117.   for (int i = 0; i < nSparks; i++) {
  118.     sparkPos[i] = flarePos; sparkVel[i] = (float(random16(0, 20000)) / 10000.0) - 1.0; // from -1 to 1
  119.     sparkCol[i] = abs(sparkVel[i]) * 500; // set colors before scaling velocity to keep them bright
  120.     sparkCol[i] = constrain(sparkCol[i], 0, 255);
  121.     sparkVel[i] *= flarePos / NUM_LEDS; // proportional to height
  122.   }
  123.   sparkCol[0] = 255; // this will be our known spark
  124.   float dying_gravity = gravity;
  125.   float c1 = 120;
  126.   float c2 = 50;
  127.  
  128.  
  129.   while(sparkCol[0] > c2/128) { // as long as our known spark is lit, work with all the sparks
  130.     delay(0);
  131.     FastLED.clear();
  132.    
  133.     for (int i = 0; i < nSparks; i++) {
  134.       sparkPos[i] += sparkVel[i];
  135.       sparkPos[i] = constrain(sparkPos[i], 0, NUM_LEDS-1);
  136.       sparkVel[i] += dying_gravity;
  137.       sparkCol[i] *= .975;
  138.       sparkCol[i] = constrain(sparkCol[i], 0, 255); // red cross dissolve
  139.      
  140.       if(sparkCol[i] > c1) { // fade white to yellow
  141.         leds[int(sparkPos[i])] = CRGB(255, 255, (255 * (sparkCol[i] - c1)) / (255 - c1));
  142.       }
  143.       else if (sparkCol[i] < c2) { // fade from red to black
  144.         leds[int(sparkPos[i])] = CRGB((255 * sparkCol[i]) / c2, 0, 0);
  145.       }
  146.       else { // fade from yellow to red
  147.         leds[int(sparkPos[i])] = CRGB(255, (255 * (sparkCol[i] - c2)) / (c1 - c2), 0);
  148.       }
  149.  
  150.      
  151.     }
  152.     dying_gravity *= .99; // as sparks burn out they fall slower
  153.     FastLED.show();
  154.    
  155.   }
  156.  
  157.   delay(5);
  158.   FastLED.clear();
  159.   FastLED.show();
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement