#include <FastLED.h>
const int NUM_LEDS=144;
#define DATA_PIN 5
CRGB leds[NUM_LEDS]; // sets up block of memory
const int NUM_SPARKS = 40; // max number (could be NUM_LEDS / 2);
const int maxPos = (NUM_LEDS-1) * 128 ;
int sparkPos[3][NUM_SPARKS] ;
int sparkVel[3][NUM_SPARKS] ;
int sparkHeat[3][NUM_SPARKS];
int flarePos[3];
bool flareShot[3] = {false,false,false} ;
byte nSparks[3];
CRGBPalette16 gPal1;
CRGBPalette16 gPal2;
CRGBPalette16 gPal3;
DEFINE_GRADIENT_PALETTE( testp ) {
0, 0, 0,0,
32, 8, 0, 0,
64, 16,0, 0,
96, 64,32, 0,
128,128,80, 0,
160, 160,100,0,
192, 192, 192,0,
224, 255, 255, 255,
255, 200, 200, 255};
void setup() {
// Serial.begin(115200);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(255);
gPal1 = testp;
gPal2 = CRGBPalette16( CRGB::Black, CRGB::DarkBlue, CRGB::Aqua, CRGB::White);
gPal3 = CRGBPalette16( CRGB::Black, CRGB::DarkGreen, CRGB::Yellow, CRGB::White);
}
/*
* Main Loop
*/
byte nrv;
void loop() {
EVERY_N_MILLIS(10) {
// random16_add_entropy( random());
if (not flareShot[0]) shoot(0);
if (not flareShot[1]) shoot(1);
if (not flareShot[2]) shoot(2);
}
EVERY_N_MILLIS(10) {
if (flareShot[0]) doSparks(0);
if (flareShot[1]) doSparks(1);
if (flareShot[2]) doSparks(2);
}
EVERY_N_MILLIS(10) {
FastLED.show();
// FastLED.clear();
fadeToBlackBy(leds,NUM_LEDS,80);
}
}
void shoot(byte nr) {
if (random(1000) <10) {
flareShot[nr]=true;
flarePos[nr]=random(40,NUM_LEDS-40);
nSparks[nr]=30;
// initialize sparks
for (int x = 0; x < nSparks[nr]; x++) {
sparkPos[nr][x] = flarePos[nr]<<7;
sparkVel[nr][x] = random16(0, 5120)-2560; // velocitie original -1 o 1 now -255 to + 255
word sph = abs(sparkVel[nr][x])<<2;
if (sph>2550) sph=2550; // set heat before scaling velocity to keep them warm heat is 0-500 but then clamped to 255
sparkHeat[nr][x]=sph ;
}
sparkHeat[nr][0] = 5000; // this will be our known spark
}
}
void doSparks(byte nr){
for (int x = 0; x < nSparks[nr]; x++) {
sparkPos[nr][x] = sparkPos[nr][x] + (sparkVel[nr][x]>>6); // adjust speed of sparks here
sparkPos[nr][x]=constrain(sparkPos[nr][x],0,maxPos);
sparkHeat[nr][x]=scale16(sparkHeat[nr][x],128000); // adjust speed of cooldown here
CRGB color;
if (nr==0) color = ColorFromPalette( gPal1 , scale16(sparkHeat[nr][x],6600));
if (nr==1) color = ColorFromPalette( gPal2 , scale16(sparkHeat[nr][x],6600));
if (nr==2) color = ColorFromPalette( gPal3 , scale16(sparkHeat[nr][x],6600));
leds[sparkPos[nr][x]>>7]+=color;
if (sparkHeat[nr][0] < 1) {
flareShot[nr]=false; // this fireworks is done
}
}
}