Guest User

new fireworks

a guest
Jan 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. #define stripPin 6
  4. #define LED_TYPE    WS2811
  5. #define COLOR_ORDER GRB
  6. #define stripLength    88
  7. #define BRIGHTNESS  128
  8.  
  9. byte shell1 = 1;
  10. byte shell2 = 0;
  11. int shell1pos = 0;
  12. int shell2pos = 0;
  13. uint8_t shell1start = random(10,80);
  14. uint8_t shell2start = random(10,80);
  15. uint8_t shell1hue = random(255);
  16. uint8_t shell2hue = random(255);
  17. uint8_t tailLength = 8;
  18.  
  19. CRGB leds[stripLength];
  20.  
  21. void setup() {
  22.   Serial.begin(9600);
  23.   randomSeed(analogRead(0));
  24.   delay(3000); // 3 second delay for recovery
  25.   FastLED.setMaxPowerInVoltsAndMilliamps(5, 1500);
  26.  
  27.   // tell FastLED about the LED strip configurations
  28.   FastLED.addLeds<LED_TYPE, stripPin, COLOR_ORDER>(leds, stripLength);
  29.  
  30.   // set master brightness control
  31.   FastLED.setBrightness(BRIGHTNESS);
  32. }
  33.  
  34. void loop() {
  35.   fireworks();
  36. }
  37.  
  38. void fireworks(){
  39.  
  40.   //shell 1
  41.     if(shell1==1){
  42.      
  43.       //light master shells in opposite directions at position if greater than 0 and less than total length
  44.       if((shell1start+shell1pos)<stripLength) leds[(shell1start+shell1pos)] = CHSV(shell1hue, 255,255);
  45.       if((shell1start-shell1pos)>0) leds[(shell1start-shell1pos)] = CHSV(shell1hue, 255,255);
  46.  
  47.       //lights secondary shell at halfway point
  48.       int halfWay1 = stripLength-shell1start;
  49.       if((shell1start+shell1pos)>halfWay1) shell2=1;
  50.      
  51.       //creates tail
  52.       for (int t=1; t<=tailLength; t++){
  53.         int front = shell1start+shell1pos-t;
  54.         int back = shell1start-shell1pos+t;
  55.        
  56.         //if tail positions would overlap initial start position, don't show
  57.         if(front>shell1start && front<stripLength) leds[front] = CHSV(shell1hue-40,255,random(40));
  58.         if(back<shell1start && back>0) leds[back] = CHSV(shell1hue-40,255,random(40));
  59.       }
  60.  
  61.       //if shell has reached the end of BOTH sides of strip, reset color and pick new position
  62.       if((shell1start+shell1pos)+tailLength>=stripLength && (shell1start-shell1pos)-tailLength<=0){
  63.         shell1start = random(10,80);
  64.         shell1pos = 0;
  65.         shell1hue = random(255);
  66.         shell1=0;
  67.       }else{
  68.         //increase position
  69.       shell1pos++;
  70.       }
  71.     }
  72.    
  73.   //shell2
  74.     if(shell2==1){
  75.      
  76.       //light master shells in opposite directions at position if greater than 0 and less than total length
  77.       if((shell2start+shell2pos)<stripLength) leds[(shell2start+shell2pos)] = CHSV(shell2hue, 255,255);
  78.       if((shell2start-shell2pos)>0) leds[(shell2start-shell2pos)] = CHSV(shell2hue, 255,255);
  79.  
  80.       //lights secondary shell at halfway point
  81.       int halfWay2 = stripLength-shell2start;
  82.       if((shell2start+shell1pos)>halfWay2) shell1=1;
  83.      
  84.       //creates tail
  85.       for (int t=1; t<=tailLength; t++){
  86.         int front = shell2start+shell2pos-t;
  87.         int back = shell2start-shell2pos+t;
  88.        
  89.         //if tail positions would overlap initial start position, don't show
  90.         if(front>shell2start && front<stripLength) leds[front] = CHSV(shell2hue-40,255,random(40));
  91.         if(back<shell2start && back>0) leds[back] = CHSV(shell2hue-40,255,random(40));
  92.       }
  93.  
  94.       //if shell has reached the end of BOTH sides of strip, reset color and pick new position
  95.       if((shell2start+shell2pos)+tailLength>=stripLength && (shell2start-shell2pos)-tailLength<=0){
  96.         shell2start = random(10,80);
  97.         shell2pos = 0;
  98.         shell2hue = random(255);
  99.         shell2=0;
  100.       }else{
  101.         //increase position
  102.       shell2pos++;
  103.       }
  104.     }
  105.  
  106.     //display everything
  107.     FastLED.show();
  108.     FastLED.delay(30);
  109.  
  110.     //reset everything to black to prep for next position
  111.     for(int j=0;j<stripLength;j++){leds[j] = CRGB::Black;}
  112.  
  113.     //sometimes there's a kink where not enough time is given to reset any given shell. this is a slam fix
  114.     if(shell2==0 && shell1==0)shell1=1;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment