jedimasta

myFireworks

May 29th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN     7 //6 for testing, 7 for production
  4. #define COLOR_ORDER GRB
  5. #define CHIPSET     WS2811
  6. #define NUM_LEDS    300 //88 for testing, 300 for prodction
  7. #define BRIGHTNESS  50
  8. #define FRAMES_PER_SECOND 30
  9.  
  10. bool gReverseDirection = false;
  11.  
  12. int lifespan = 30;
  13. int totalShells = 0;
  14. const long interval = 1000 / FRAMES_PER_SECOND;
  15. unsigned long previousMillis = 0;
  16.  
  17.  
  18. CRGB leds[NUM_LEDS];
  19.  
  20. class FireworkShell {
  21.     int stringPos;    //origin of the blast
  22.     int age;          //how long the blast has been on the string
  23.     int maxLife;      //how old this one can get
  24.     uint8_t hue;       //color of the shell
  25.  
  26.   public:
  27.     FireworkShell(int xPos, int howOld, int deathAge, uint8_t shellColor) {
  28.       stringPos = xPos;
  29.       age = howOld;
  30.       maxLife = deathAge;
  31.       hue = shellColor;
  32.     }
  33.  
  34.     void Update() {
  35.  
  36.       if (age < maxLife) {
  37.         int newPosR = stringPos + age;
  38.         int newPosL = stringPos - age;
  39.         if (newPosR < NUM_LEDS) leds[newPosR] +=  CHSV(hue, 255, 255);
  40.         if (newPosL >= 0) leds[newPosL] +=  CHSV(hue, 255, 255);
  41.  
  42.         if (age > 0) {
  43.  
  44.  
  45.           int tailR = newPosR - 1;
  46.           int tailL = newPosL + 1;
  47.           if (tailR > 0 && tailR < NUM_LEDS) {
  48.             leds[tailR] = CHSV(hue+23, random(0, 128), random(0, 200));
  49.           }
  50.           if (tailL > 0 && tailL < NUM_LEDS) {
  51.             leds[tailL] = CHSV(hue+23, random(0, 128), random(0, 200));
  52.           }
  53.  
  54.  
  55.  
  56.         }
  57.  
  58.       }
  59.       if (age < maxLife + 20) {
  60.         age++;
  61.       } else {
  62.         age = 0;
  63.         hue=random(0,213);
  64.         maxLife = random(5, lifespan);
  65.         stringPos =  random(0, NUM_LEDS);
  66.         //Serial.println(maxLife);
  67.       }
  68.     }
  69. };
  70.  
  71. FireworkShell shell1(random(0, NUM_LEDS), 0, random(5, lifespan), 171);
  72. FireworkShell shell2(random(0, NUM_LEDS), 0, random(5, lifespan), 0);
  73. FireworkShell shell3(random(0, NUM_LEDS), 0, random(5, lifespan), 85);
  74. FireworkShell shell4(random(0, NUM_LEDS), 0, random(5, lifespan), 85);
  75. FireworkShell shell5(random(0, NUM_LEDS), 0, random(5, lifespan), 0);
  76.  
  77.  
  78. void setup() {
  79.   Serial.begin(9600);
  80.   delay(3000); // sanity delay
  81.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  82.   FastLED.setBrightness( BRIGHTNESS );
  83.   FastLED.setMaxPowerInVoltsAndMilliamps(5, 1500);
  84.   randomSeed(analogRead(0));
  85. };
  86.  
  87.  
  88. void loop() {
  89.   EVERY_N_MILLISECONDS(20) {
  90.     fadeToBlackBy(leds, NUM_LEDS, 30);
  91.     shell1.Update();
  92.     shell2.Update();
  93.     shell3.Update();
  94.     shell4.Update();
  95.     shell5.Update();
  96.     FastLED.show();
  97.   };
  98. }
Advertisement
Add Comment
Please, Sign In to add comment