Guest User

Fireworks_single_strip

a guest
Jan 15th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. #define NUM_LEDS 120
  4. #define MAX_SPARKS 20
  5. #define SPARKS_LOW_LIMIT 5 // Avoid starting dots too close to the strips edge
  6. #define SPARKS_HIGH_LIMIT 115 // Avoid starting dots too close to the strips edge
  7. #define NEWSPARKCOUNTER 10
  8.  
  9. CRGB leds[NUM_LEDS];
  10.  
  11. uint16_t position_up[MAX_SPARKS];
  12. uint16_t position_down[MAX_SPARKS];
  13. CRGB color[MAX_SPARKS];
  14.  
  15.  
  16. void CreateNewSparks() {
  17.  
  18. for ( uint8_t x = 0; x < MAX_SPARKS; x++) {
  19.  
  20. if (position_up[x] == NUM_LEDS && position_down[x] == NUM_LEDS) { // Will only be true if both sparks of that position are available
  21.  
  22. position_up[x] = random16(SPARKS_LOW_LIMIT, SPARKS_HIGH_LIMIT);
  23. position_down[x] = position_up[x] - 1;
  24.  
  25. uint8_t select_color = random8(6); // Only 6 basic colors chosen for simplicity
  26.  
  27. switch (select_color) {
  28.  
  29. case 0:
  30. color[x] = CRGB(255, 0, 0); // Red
  31. break;
  32. case 1:
  33. color[x] = CRGB(0, 255, 0); // Green
  34. break;
  35. case 2:
  36. color[x] = CRGB(0, 0, 255); // Blue
  37. break;
  38. case 3:
  39. color[x] = CRGB(255, 255, 0); // Yellow
  40. break;
  41. case 4:
  42. color[x] = CRGB(255, 0, 255); // Purple
  43. break;
  44. case 5:
  45. color[x] = CRGB(0, 255, 255); // Cyan
  46. break;
  47.  
  48. default:
  49. color[x] = CRGB(255, 255, 255); // White
  50. break;
  51. }
  52.  
  53. return; // Break out of for loop if we have found one available object
  54.  
  55. } // End of if test
  56. } // End of for loop
  57. } // End of CreateNewSparks function
  58.  
  59.  
  60. void MoveAllSparks() {
  61.  
  62. for ( uint8_t x = 0; x < MAX_SPARKS; x++) {
  63.  
  64. if (position_up[x] < NUM_LEDS) {
  65. position_up[x]++;
  66. if (position_up[x] != NUM_LEDS) leds[position_up[x]] += color[x];
  67. }
  68.  
  69. if (position_down[x] > 0 && position_down[x] != NUM_LEDS) {
  70. position_down[x]--;
  71. leds[position_down[x]] += color[x];
  72. if (position_down[x] == 0) position_down[x] = NUM_LEDS;
  73. }
  74. } // End of MoveSparks function
  75. }
  76.  
  77.  
  78. void FadeAllSparks() {
  79.  
  80. uint8_t fadeby = 20; // Change value to create a longer or shorter trail for sparks
  81.  
  82. for ( uint8_t x = 0; x < NUM_LEDS; x++) {
  83.  
  84. leds[x] -= CRGB( fadeby, fadeby, fadeby);
  85. }
  86. }
  87.  
  88.  
  89. void setup() {
  90.  
  91. FastLED.addLeds<NEOPIXEL, 8>(leds, NUM_LEDS);
  92. FastLED.setBrightness(128);
  93.  
  94. for (uint16_t x = 0; x < MAX_SPARKS; x++) { // Clear all sparks positions
  95. position_up[x] = NUM_LEDS;
  96. position_down[x] = NUM_LEDS;
  97. }
  98. }
  99.  
  100. void loop() {
  101.  
  102. static uint8_t launchcounter = 0;
  103.  
  104. launchcounter++;
  105.  
  106. if (launchcounter >= NEWSPARKCOUNTER) {
  107. CreateNewSparks();
  108. launchcounter = 0;
  109. }
  110.  
  111. MoveAllSparks();
  112.  
  113. FastLED.show();
  114. FastLED.delay(10);
  115.  
  116. FadeAllSparks();
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment