#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 135
CRGB leds[NUM_LEDS];
uint8_t kMatrixWidth =5;
bool kMatrixSerpentineLayout=true;
uint16_t XY( uint8_t x, uint8_t y)
{
uint16_t i;
if( kMatrixSerpentineLayout == false) {
i = (y * kMatrixWidth) + x;
}
if( kMatrixSerpentineLayout == true) {
if( y & 0x01) {
// Odd rows run backwards
uint8_t reverseX = (kMatrixWidth - 1) - x;
i = (y * kMatrixWidth) + reverseX;
} else {
// Even rows run forwards
i = (y * kMatrixWidth) + x;
}
}
return i;
}
void setup() {
Serial.begin(115200);
FastLED.addLeds<NEOPIXEL, 2>(leds, NUM_LEDS);
FastLED.show();
}
// step1 create random fire seed , size: 2-4
// step2 move fire seed up
// step3 blur
#define FIRE_SEED_COUNT 3
struct FireSeed{
uint16_t totalTime;
float yAcc;
uint8_t x;
uint8_t y;
CRGB color;
uint8_t size;
uint8_t sinOffset;
};
FireSeed fss[FIRE_SEED_COUNT];
uint8_t fssIndex =0;
void createFireSeed(){
fssIndex%=FIRE_SEED_COUNT;
fss[fssIndex]= {
0,
0.00005*random(1,5),
random8(0,5),
0,
CHSV(random(20,40), 250, 255),
random(1,3),
255/random(4,5)
};
fssIndex++;
}
void upFireSeed(){
EVERY_N_MILLISECONDS(30){
for(uint8_t i=0;i<FIRE_SEED_COUNT;i++){
FireSeed fs = fss[i];
if(fs.totalTime>3000){
continue;
}
fs.totalTime+=3;
fs.y = fs.yAcc * fs.totalTime * fs.totalTime/2;
uint8_t y=map8(fs.y,0,27-fs.size);
uint8_t x=(map8(sin8(fs.y+fs.sinOffset),0,10)+fs.x)%5;
for(uint8_t j=0;j<fs.size;j++){
fs.color.fadeToBlackBy(y/2);
leds[XY(x, y+j)]= fs.color;
}
fss[i]=fs;
}
blur2d(leds, 5, 27, 30);
FastLED.show();
}
EVERY_N_MILLISECONDS(1200){
createFireSeed();
}
}
// ==================== fire end ==========
void loop() {
upFireSeed();
}