Fireworks #260
kevinl7778
started this conversation in
Ideas
Fireworks
#260
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Set up stuff:
float Gravity = 0.1;
const int numModes = 4;
const int numsparks = NUM_LEDS / 15;
uint8_t fworkType[numsparks];
float fworkHeat[numsparks];
CRGB fworkColor[numsparks];
float fworkLocation[numsparks];
float fworkVelocity[numsparks];
void fireworks() {
fadeToBlackBy(ledsL, NUM_LEDS, 180);
fadeToBlackBy(ledsR, NUM_LEDS, 180);
if (sparkCount() < random(speed / 3) && random(20000) <= 2000) // randomly fire off a new one if sparkcount is zero
{ newSpark(1, CRGB(255, 40, 0), 0, 1.5 + (float)random(8) / 3.0); // was newSpark(1, CRGB(29, 48, 48), 0, 1.5 + (float)random(13)/4.0);
}
updateFireworks();
showFireworks();
// FastLED.show();
}
void updateFireworks() {
for (int i = 0; i < numsparks; i++)
{
switch (fworkType[i])
{
case 0:
break;
case 1: // rising
fworkLocation[i] += constrain(fworkVelocity[i], -1.0, 1.0);
fworkVelocity[i] -= Gravity; // gravity
if (fworkVelocity[i] < 1.0) { // Explode
fworkType[i] = 0;
int numNew = 6 + random16(6);
float newDiamter = ((float)random16(3) / 2.0) + 0.5;
CRGB newColor = CHSV(random(255), 255, 255); //firework color random 64 yealds fire color gradient
for (int j = 0; j < numNew; j++)
{
float newVel = (newDiamter / 2.0) - ((newDiamter / (float)numNew) * (float)(j));
newSpark(2, newColor, fworkLocation[i] + newVel, newVel + (float)random16(10) / 20.0);
}
}
break;
case 2: // exploding
fworkLocation[i] += fworkVelocity[i];
fworkHeat[i] -= 0.07;
if (fworkHeat[i] <= 0)
{
fworkType[i] = 0;
}
else if (fworkHeat[i] < 0.5)
{
fworkColor[i] -= CRGB(128, 128, 128);
fworkVelocity[i] /= 3;
if (random8(100) < 50)
fworkVelocity[i] += 0.05;
else
fworkVelocity[i] -= 0.05;
}
}
void newSpark(uint8_t type, CRGB newCol, int newLoc, float newVel) {
int newfirework = nextdead();
if (newfirework < 0)
{
return;
}
fworkType[newfirework] = type;
fworkHeat[newfirework] = 1.0;
fworkColor[newfirework] = newCol;
fworkLocation[newfirework] = newLoc;
fworkVelocity[newfirework] = newVel;
}
void showFireworks() {
for (int i = 0; i < numsparks; i++)
{
//Serial.print(fworkVelocity[i]); Serial.print(",");
if (fworkType[i] > 0)
{
if ((fworkLocation[i] < 0) || (fworkLocation[i] >= NUM_LEDS))
{
fworkType[i] = 0; // just kill it if it goes out of bounds.
}
//else
//{
ledsL[(int)fworkLocation[i]] += fworkColor[i];
ledsR[(int)fworkLocation[i]] += fworkColor[i];
//}
}
}
}
int sparkCount() {
int retval = 0;
for (int i = 0; i < numsparks; i++)
if (fworkType[i] > 0)
retval++;
return retval;
}
int nextdead() {
for (int i = 0; i < numsparks; i++)
{
if (fworkType[i] == 0)
return i;
}
return -1;
}
Beta Was this translation helpful? Give feedback.
All reactions