Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FastLED.h"
- #define NUM_LEDS 120
- #define MAX_SPARKS 20
- #define SPARKS_LOW_LIMIT 5 // Avoid starting dots too close to the strips edge
- #define SPARKS_HIGH_LIMIT 115 // Avoid starting dots too close to the strips edge
- #define NEWSPARKCOUNTER 10
- CRGB leds[NUM_LEDS];
- uint16_t position_up[MAX_SPARKS];
- uint16_t position_down[MAX_SPARKS];
- CRGB color[MAX_SPARKS];
- void CreateNewSparks() {
- for ( uint8_t x = 0; x < MAX_SPARKS; x++) {
- if (position_up[x] == NUM_LEDS && position_down[x] == NUM_LEDS) { // Will only be true if both sparks of that position are available
- position_up[x] = random16(SPARKS_LOW_LIMIT, SPARKS_HIGH_LIMIT);
- position_down[x] = position_up[x] - 1;
- uint8_t select_color = random8(6); // Only 6 basic colors chosen for simplicity
- switch (select_color) {
- case 0:
- color[x] = CRGB(255, 0, 0); // Red
- break;
- case 1:
- color[x] = CRGB(0, 255, 0); // Green
- break;
- case 2:
- color[x] = CRGB(0, 0, 255); // Blue
- break;
- case 3:
- color[x] = CRGB(255, 255, 0); // Yellow
- break;
- case 4:
- color[x] = CRGB(255, 0, 255); // Purple
- break;
- case 5:
- color[x] = CRGB(0, 255, 255); // Cyan
- break;
- default:
- color[x] = CRGB(255, 255, 255); // White
- break;
- }
- return; // Break out of for loop if we have found one available object
- } // End of if test
- } // End of for loop
- } // End of CreateNewSparks function
- void MoveAllSparks() {
- for ( uint8_t x = 0; x < MAX_SPARKS; x++) {
- if (position_up[x] < NUM_LEDS) {
- position_up[x]++;
- if (position_up[x] != NUM_LEDS) leds[position_up[x]] += color[x];
- }
- if (position_down[x] > 0 && position_down[x] != NUM_LEDS) {
- position_down[x]--;
- leds[position_down[x]] += color[x];
- if (position_down[x] == 0) position_down[x] = NUM_LEDS;
- }
- } // End of MoveSparks function
- }
- void FadeAllSparks() {
- uint8_t fadeby = 20; // Change value to create a longer or shorter trail for sparks
- for ( uint8_t x = 0; x < NUM_LEDS; x++) {
- leds[x] -= CRGB( fadeby, fadeby, fadeby);
- }
- }
- void setup() {
- FastLED.addLeds<NEOPIXEL, 8>(leds, NUM_LEDS);
- FastLED.setBrightness(128);
- for (uint16_t x = 0; x < MAX_SPARKS; x++) { // Clear all sparks positions
- position_up[x] = NUM_LEDS;
- position_down[x] = NUM_LEDS;
- }
- }
- void loop() {
- static uint8_t launchcounter = 0;
- launchcounter++;
- if (launchcounter >= NEWSPARKCOUNTER) {
- CreateNewSparks();
- launchcounter = 0;
- }
- MoveAllSparks();
- FastLED.show();
- FastLED.delay(10);
- FadeAllSparks();
- }
Advertisement
Add Comment
Please, Sign In to add comment