Skip to content

Instantly share code, notes, and snippets.

@atuline
Created September 11, 2017 23:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atuline/0ece6782d4df93a3cc277502e92657fd to your computer and use it in GitHub Desktop.
Save atuline/0ece6782d4df93a3cc277502e92657fd to your computer and use it in GitHub Desktop.
Mark Kriegsman's Fire2012WithPalettes has been modified to support multiple HSV's.
/* fire2012withhuepalettes
By: Mark Kriegsman
Modified by: Andrew Tuline
Date: July, 2015
This is fire2012withpalette that's been modifed to create hue based fire palettes.
*/
#include "FastLED.h" // FastLED library. Please use the latest development version.
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
// Fixed definitions cannot change on the fly.
#define LED_DT 12
#define LED_CK 11
#define COLOR_ORDER BGR
#define LED_TYPE APA102
#define NUM_LEDS 60
#define BRIGHTNESS 255
#define FRAMES_PER_SECOND 60
struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
CRGBPalette16 currentPalette;
CRGBPalette16 targetPalette;
TBlendType currentBlending; // NOBLEND or LINEARBLEND
int thisdelay = 60;
void setup() {
Serial.begin(57600);
delay(1000); // sanity delay
LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2801 or APA102
// LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2812
FastLED.setBrightness(BRIGHTNESS); // You can change the overall brightness on the fly, i.e. with a potentiometer.
set_max_power_in_volts_and_milliamps(5, 1200); // This is used by the power management functionality and is currently set at 5V, 500mA.
currentPalette = CRGBPalette16(CRGB::Black);
targetPalette = CRGBPalette16(CRGB::Black); // Used for smooth transitioning.
currentBlending = LINEARBLEND;
} // setup()
void loop() {
ChangePalettePeriodically();
EVERY_N_MILLISECONDS(thisdelay) {
Fire2012WithPalette(); // run simulation frame, using palette colors
}
EVERY_N_MILLISECONDS(100) {
uint8_t maxChanges = 24;
nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
}
// FastLED.show();
FastLED.show(); // display this frame
} // loop()
uint8_t cooling = 100; // Less cooling = taller flames. More cooling = shorter flames. Default 55, suggested range 20-100.
uint8_t sparking = 120; // Higher chance = more roaring fire. Lower chance = more flickery fire. Default 120, suggested range 50-200.
void Fire2012WithPalette() {
static byte heat[NUM_LEDS]; // Array of temperature readings at each simulation cell
for( int i = 0; i < NUM_LEDS; i++) { // Step 1. Cool down every cell a little
heat[i] = qsub8( heat[i], random8(0, ((cooling * 10) / NUM_LEDS) + 2));
}
for( int k= NUM_LEDS - 1; k >= 2; k--) { // Step 2. Heat from each cell drifts 'up' and diffuses a little
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
}
if( random8() < sparking ) { // Step 3. Randomly ignite new 'sparks' of heat near the bottom
int y = random8(NUM_LEDS/6);
heat[y] = qadd8( heat[y], random8(160,255) );
}
for( int j = 0; j < NUM_LEDS; j++) { // Step 4. Map from heat cells to LED colors
byte colorindex = scale8( heat[j], 240); // Scale the heat value from 0-255 down to 0-240 for best results with color palettes.
leds[j] = ColorFromPalette( currentPalette, colorindex);
}
} // Fire2012WithPalette()
void ChangePalettePeriodically() {
uint8_t secondHand = (millis() / 1000) % 10;
static uint8_t lastSecond = 99;
if( lastSecond != secondHand) {
lastSecond = secondHand;
switch(secondHand) {
case 0: SetupPalette(random8()); currentBlending = LINEARBLEND; break;
case 10: break;
}
}
} // ChangePalettePeriodically()
/*
* targetPalette = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Orange, CRGB::Yellow);
*
*/
// Creating a decent fire palette from a hue. This is based on saturation and brightness values from LavaColors_p.
/*
void SetupPalette(uint8_t palhue) {
targetPalette = CRGBPalette16(CHSV(palhue, 255, 0), CHSV(palhue, 255, 32), CHSV(palhue, 255, 0), CHSV(palhue, 255, 32),
CHSV(palhue, 255, 32), CHSV(palhue, 255, 64), CHSV(palhue, 255, 32), CHSV(palhue, 255, 64),
CHSV(palhue, 255, 64), CHSV(palhue, 255, 64), CHSV(palhue, 255, 128), CHSV(palhue, 240, 192),
CHSV(palhue, 0, 255), CHSV(palhue, 255, 255), CHSV(palhue, 255, 255), CHSV(palhue, 255, 255));
} // SetupPalette()
*/
void SetupPalette(uint8_t palhue) {
/* targetPalette = CRGBPalette16(CHSV(palhue, 255, 16), CHSV(palhue, 255, 32), CHSV(palhue, 255, 16), CHSV(palhue, 255, 32),
CHSV(palhue, 255, 32), CHSV(palhue, 255, 64), CHSV(palhue, 255, 32), CHSV(palhue, 255, 64),
CHSV(palhue, 255, 64), CHSV(palhue, 255, 64), CHSV(palhue, 255, 128), CHSV(palhue, 240, 160),
CHSV(palhue, 255, 128), CHSV(palhue, 255, 64), CHSV(palhue, 255, 32), CHSV(palhue, 255, 16));
*/
/* targetPalette = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Orange, CRGB::Yellow);
*
*/
targetPalette = CRGBPalette16( CHSV(palhue, 255, 0), CHSV(palhue, 255, 160), CHSV(palhue, 255, 224), CHSV(palhue, 255, 255));
/* targetPalette = CRGBPalette16(CHSV(palhue, 255, 0), CHSV(palhue, 255, 0), CHSV(palhue, 255, 4), CHSV(palhue, 255, 8),
CHSV(palhue, 255, 16), CHSV(palhue, 255, 32), CHSV(palhue, 255, 64), CHSV(palhue, 255, 128),
CHSV(palhue, 255, 128), CHSV(palhue, 255, 164), CHSV(palhue, 255, 192), CHSV(palhue, 240, 208),
CHSV(palhue, 255, 192), CHSV(palhue, 255, 192), CHSV(palhue, 255, 255), CHSV(palhue, 255, 255));
*/
} // SetupPalette()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment