Skip to content

Instantly share code, notes, and snippets.

@aurimasniekis
Created May 1, 2019 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aurimasniekis/983c8a634f997d6e53b9b6488572266b to your computer and use it in GitHub Desktop.
Save aurimasniekis/983c8a634f997d6e53b9b6488572266b to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include "PPMInput.h"
#include <FastLED.h>
#define NUM_LEDS 70
#define DATA_PIN 0
#define BRIGHTNESS 200
#define FRAMES_PER_SECOND 60
bool gReverseDirection = false;
CRGB leds[NUM_LEDS];
PPMInput *ch1;
CRGBPalette16 gPal;
void setup() {
ch1 = new PPMInput(PPM1, 1);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
gPal = RainbowColors_p;
}
void loop() {
Serial.print("CH1: ");
Serial.println(ch1->latestValidChannelValue(1));
delay(100);
if (ch1->isReverse()) {
fill_solid(leds, NUM_LEDS, CRGB::Red);
} else if (ch1->isForward()) {
random16_add_entropy(random());
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
} else {
if (digitalRead(3) == HIGH) {
leds[0] = CRGB::Red;
leds[0].fadeLightBy(220);
fill_solid(leds, NUM_LEDS, leds[0]);
} else {
fill_solid(leds, NUM_LEDS, CRGB::Black);
}
}
FastLED.show();
}
// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames. More cooling = shorter flames.
// Default 55, suggested range 20-100
#define COOLING 55
// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire. Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 120
void Fire2012WithPalette() {
// Array of temperature readings at each simulation cell
static byte heat[NUM_LEDS];
// Step 1. Cool down every cell a little
for (int i = 0; i < NUM_LEDS; i++) {
heat[i] = qsub8(heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
}
// 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' of heat near the bottom
if (random8() < SPARKING) {
int y = random8(7);
heat[y] = qadd8(heat[y], random8(160, 255));
}
// Step 4. Map from heat cells to LED colors
for (int j = 0; j < NUM_LEDS; j++) {
// Scale the heat value from 0-255 down to 0-240
// for best results with color palettes.
byte colorindex = scale8(heat[j], 240);
CRGB color = ColorFromPalette(gPal, colorindex);
int pixelnumber;
if (gReverseDirection) {
pixelnumber = (NUM_LEDS - 1) - j;
} else {
pixelnumber = j;
}
leds[pixelnumber] = color;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment