Skip to content

Instantly share code, notes, and snippets.

@rafitc
Last active January 1, 2022 10:16
Show Gist options
  • Select an option

  • Save rafitc/0ec1f94dface256ba5a3d98759d6713e to your computer and use it in GitHub Desktop.

Select an option

Save rafitc/0ec1f94dface256ba5a3d98759d6713e to your computer and use it in GitHub Desktop.
#include "FastLED.h"
#define NUM_LEDS 300
CRGB leds[NUM_LEDS];
#define PIN 4
uint8_t defaultSpeed = 5;
uint8_t defaultBright = 3;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 40;
void setup() {
Serial.begin(9600);
Serial.println("ready...");
delay(10);
FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}
void loop() {
Fire(55, 120, defaultSpeed);
}
void showStrip() {
FastLED.show();
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
}
void setAll(byte red, byte green, byte blue) {
for (int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}
void Fire(int Cooling, int Sparking, int SpeedDelay) {
static byte heat[NUM_LEDS];
int cooldown;
// Step 1. Cool down every cell a little
for ( int i = 0; i < NUM_LEDS; i++) {
cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
if (cooldown > heat[i]) {
heat[i] = 0;
} else {
heat[i] = heat[i] - cooldown;
}
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for ( int k = NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
}
// Step 3. Randomly ignite new 'sparks' near the bottom
if ( random(255) < Sparking ) {
int y = random(7);
heat[y] = heat[y] + random(160, 255);
//heat[y] = random(160,255);
}
// Step 4. Convert heat to LED colors
for ( int j = 0; j < NUM_LEDS; j++) {
setPixelHeatColor(j, heat[j] );
}
showStrip();
delay(SpeedDelay);
}
void setPixelHeatColor (int Pixel, byte temperature) {
// Scale 'heat' down from 0-255 to 0-191
byte t192 = round((temperature / 255.0) * 191);
// calculate ramp up from
byte heatramp = t192 & 0x3F; // 0..63
heatramp <<= 2; // scale up to 0..252
//dimm to the desired brightness
float mod = defaultBright / 100.0;
// figure out which third of the spectrum we're in:
if ( t192 > 0x80) { // hottest
setPixel(Pixel, 255 * mod, 255 * mod, heatramp * mod);
} else if ( t192 > 0x40 ) { // middle
setPixel(Pixel, 255 * mod, heatramp * mod, 0);
} else { // coolest
setPixel(Pixel, heatramp * mod, 0, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment