I'm working with LED strips (like plenty of others) and I've created a sequence of what I call fireworks on a single strip. Each shell is created using a constructor class and updated in each iteration of the loop to allow for overlaps, random lifespans and different colors. Even though it's not a difficult prospect to type out one line to update each shell, I'd rather place a for statement inside the main loop that uses a variable to determine how many shells to update each time. My understanding is that each instance of the FireworkShell that is created needs to be placed inside of an array I can parse through, but I don't know what the correct syntax is to get that working.
Class Statement
class FireworkShell {
int stringPos; //origin of the blast
int age; //how long the blast has been on the string
int maxLife; //how old this one can get
uint8_t hue; //color of the shell
public:
FireworkShell(int xPos, int howOld, int deathAge, uint8_t shellColor) {
stringPos = xPos;
age = howOld;
maxLife = deathAge;
hue = shellColor;
}
void Update() {
if (age < maxLife) {
int newPosR = stringPos + age;
int newPosL = stringPos - age;
if (newPosR < NUM_LEDS) leds[newPosR] += CHSV(hue, 255, 255);
if (newPosL >= 0) leds[newPosL] += CHSV(hue, 255, 255);
if (age > 0) {
int tailR = newPosR - 1;
int tailL = newPosL + 1;
if (tailR > 0 && tailR < NUM_LEDS) {
leds[tailR] = CHSV(hue + 40, 255, random(50, 100));
}
if (tailL > 0 && tailL < NUM_LEDS) {
leds[tailL] = CHSV(hue + 40, 255, random(50, 100));
}
}
}
if (age < maxLife + 20) {
age++;
} else {
age = 0;
hue = random(0, 213);
maxLife = random(5, lifespan);
stringPos = random(0, NUM_LEDS);
//Serial.println(maxLife);
}
}
};
Instancing
FireworkShell shell1(random(0, NUM_LEDS), 0, random(5, lifespan), 171);
FireworkShell shell2(random(0, NUM_LEDS), 0, random(5, lifespan), 0);
etc . . .
Main loop
void loop() {
EVERY_N_MILLISECONDS(100) {
fadeToBlackBy(leds, NUM_LEDS, 30);
shell1.Update();
shell2.Update();
/* etc . . . */
FastLED.show();
};
}
I'd even love to get around writing out the creation of each instance, instead leaving them all the be created via another loop with random colors and just set a totalShells variable to feed into both the instancing and the update calls in the loop.
Would someone mind telling me how I could go about doing this or point me in the right direction?