Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fire 'Simulator' @fotoshopist 2020
- // When you 1st run this, don't panic if you see nothing. It takes a bit for it to "catch fire"
- #include "FastLED.h"
- const uint8_t MatrixHeight = 8;
- const uint8_t MatrixWidth = 32;
- const bool SerpentineLayout = true; //For LEDs that are wired back and forth rather than row by row
- #define DATA_PIN 15
- #define SEED_PIN 32 //Any unused analog pin
- #define LED_TYPE WS2812B
- #define COLOR_ORDER GRB
- #define NUM_LEDS (MatrixWidth * MatrixHeight)
- #define BRIGHTNESS 100
- //Color Palette(s)
- DEFINE_GRADIENT_PALETTE( fire_pal ) {
- 0, 0, 0, 0, //black
- 80, 255, 0, 0, //red
- 230, 255, 255, 0, //bright yellow
- 255, 255, 255, 40 //full white
- };
- DEFINE_GRADIENT_PALETTE( ice_pal ) {
- 0, 0, 0, 0, //black
- 80, 0, 0, 255, //Dark Blue
- 230, 0, 255, 255, //bright Blueish
- 255, 20, 255, 255 //full white
- };
- DEFINE_GRADIENT_PALETTE( emrld_pal ) {
- 0, 0, 0, 0, //black
- 80, 0, 255, 0, //Dark green
- 230, 100, 255, 100, //bright Greenish
- 255, 50, 255, 50 //full white
- };
- CRGBArray<NUM_LEDS> ledset;
- byte iMap[NUM_LEDS]; //array for intesity values
- void setup() {
- random16_set_seed(analogRead(SEED_PIN)); //read an unused pins floating voltage to use as our RND seed
- FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(ledset, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(BRIGHTNESS);
- delay(1000); //start up delay
- }
- void loop()
- {
- Fire();
- FastLED.delay(40); // Adjust speed of fire(40-55 looks good)
- }
- //Flames based on reducing average
- void Fire() {
- //nth Row for map
- for (int row = (MatrixHeight - 1); row >= 1; row--) {
- for (int col = (MatrixWidth - 2); col >= 1; col--) {
- int newcol = ((iMap[XY(row - 1, col - 1)] + iMap[XY(row - 1, col)] + iMap[XY(row - 1, col + 1)]) / 3) / random8(1, 5);
- iMap[XY(row, col)] = newcol;
- }
- }
- // Creation Row
- // If one of the LEDs on either side is already 'on fire' then there is a higher chance of ignition
- // If not, it has a 'very' low chance to ignite
- byte catchOn = 50; // Chance to catch on becuase cell beside us is on fire - higher number less chance (0-254) default: 50
- byte ignite = 253; // Chance to spontaneously ignight - higher number less chance (0-254) default: 253
- byte chgchance = 245; // higher number less chance (0-254) default: 245
- for (int i = 0; i <= (MatrixWidth - 1); i++) {
- byte docreate = random8();
- if (docreate > chgchance) {
- byte rCol = random8();
- if (i >= 1 && i <= (MatrixWidth - 2)) {
- if (iMap[XY(0, i - 1)] > 0 || iMap[XY(0, i + 1)] > 0) {
- if (rCol > catchOn) {
- rCol = random8(250, 255);
- } else {
- rCol = 0;
- }
- } else {
- if (rCol > ignite) {
- rCol = 255;
- } else {
- rCol = 0;
- }
- }
- } else {
- if (rCol > ignite) {
- rCol = 255;
- } else {
- rCol = 0;
- }
- }
- iMap[XY(0, i)] = rCol;
- }
- }
- //apply the colors to the CRGB array
- CRGBPalette256 curPal = fire_pal; //select the pallet to use
- for (int i = 0; i <= (NUM_LEDS - 1); i++) {
- ledset[i] = ColorFromPalette(curPal, iMap[i]);
- }
- }
- //Turn string of LEDs to coordinates X/Y
- uint16_t XY( uint8_t x, uint8_t y) {
- uint16_t i;
- if ( SerpentineLayout == false) {
- i = (y * MatrixHeight) + x;
- } else {
- if ( y & 0x01) {
- uint8_t reverseX = (MatrixHeight - 1) - x;
- i = (y * MatrixHeight) + reverseX;
- } else {
- i = (y * MatrixHeight) + x;
- }
- }
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment