Fire2012: simple fire simulation source code
Here’s “Fire2012”, a simple fire simulation for FastLED.
Source code: http://pastebin.com/xYEpxqgq
Full source code is 150 lines; simulation itself is about 30. Compiled size for AVR is 3,968 bytes.
It’s a one-dimensional simulation designed for a single strip or string of LEDs, not a two-dimensional matrix. It’s the code for the “fire” portion of “Five Elements”, as seen in this video: http://youtu.be/knWiGsmgycY
It illustrates the basic idea of using one array of values to represent the ‘heat’ at each point in the simulation, and then using a color mapping function to figure out what the right color for each ‘temperature’ should be. It includes the new function “HeatColor” which will eventually be included in the FastLED library itself.
If you get it up and running, please post video or photos!
This will only compile on the 2.1 version of FastLED btw… thanks for the share!
I think the only “2.1” feature is the use of FastLED.delay(…), which you can replace with plain old “delay(…)”, as noted in the source code itself.
Please let me know if you find any other dependencies; I’d like to update the code so everyone can run it if there are any other inadvertent dependencies.
I just pushed an update to the FastLED2.1 tree that adds a version, so you can say:
#if defined(FASTLED_VERSION) && (FASTLED_VERSION > 2001000)
FastLED.delay(10);
#else
delay(10);
#endif
I’ll update the code accordingly!
I’m not sure why I didn’t put something in there sooner. I wonder if I can also have it print a #warning message so when people build the version number they’re building with gets put into their build output?
Code updated. It now compiles and runs fine with FastLED v2.0, and v2.1.
Interesting things are afoot. I can compile the code, with the ‘stock’ LED chipsets in the setup(), however when I change it to this:
#define LED_PIN 5
#define COLOR_ORDER RBG
#define CHIPSET UCS1903
#define NUM_LEDS 50
I get stuff like this in the error message:
C:\Users\BONJUR~1\AppData\Local\Temp/ccaSElqe.s: Assembler messages:
C:\Users\BONJUR~1\AppData\Local\Temp/ccaSElqe.s:1194: Error: symbol L_906' is already defined ... C:\Users\BONJUR~1\AppData\Local\Temp/ccaSElqe.s:4416: Error: symbol L_2422’ is already defined
Can you turn on verbose output in the arduio environment and provide the full output? Also - what platform are you compiling for? (When submitting “things don’t work” type comments/reports - the more information you give, the better - I can never remember who is working with what platforms, and even if I could, many of us work with many platforms simultaneously - I’ve got a digix, an uno, a teensy 3, a teensy 3.1, and a trinket all on my desk Right Now[tm])
Arduino IDE 1.0.5-r2
Arduino Micro board
UCS1903 (non hi speed mode)
FastLED 2.1
Ok - pull the latest FastLED2.1 tree - should fix what you have.
Issue resolved, thank you. What was going on?
Typo in some little used asm code 
@Mark_Kriegsman I have adapted this code to be my template for new LED projects from now on. Thanks to you and @Daniel_Garcia for making this sh@t easy!
For those watching at home, that line should be
#if defined(FASTLED_VERSION) && (FASTLED_VERSION >= 2001000)
(Note the greater-than-or-equal vs. the original just-plain-greater-than.)
Code updated.
And Jon, you’re very welcome. Can’t wait to see what you build with it all.
Thank you Mark! That’s a great job! I’ll try thise code on weekend. How about other “Elements” of “Five”? Can you rewrite it for the new lib? I want combine all your effects and other in one code and make “big mood lamp” with BT control. Main part of my “lamp” is already done. Now i need only you code. Thanks in advance!
I’ll see if I can clean up and post some of the other effects; they’re going to come out one at a time, as I have time, but I’m happy to share them. (Candidly, I think the ‘fire’ might be the best of them!)
Amusingly(?), all of the original code for Five Elements was written in the PRE-“FastLED Version 2” days. It was written for the older “FastSPI_LED (Version 1)”, and so it does a LOT of things ‘by hand’ that are now much faster and easier – and provided by the library, often in optimized assembly language.
As I look at some of this code, it’s a welcome reminder of how far we’ve come. For example: There was no CRGB class with any methods on it before, so if you wanted to ‘add’ some color (“newcolor”) to an existing pixel (“leds[i]”), I’d have to do this:
int temp_red = leds[i].r + newcolor.r;
if( temp_red > 255 ) temp_red = 255;
int temp_green = leds[i].g + newcolor.g;
if( temp_green > 255 ) temp_green = 255;
int temp_blue = leds[i].b + newcolor.b;
if( temp_blue > 255 ) temp_blue = 255;
leds[i].r = temp_red;
leds[i].g = temp_green;
leds[i].b = temp_blue;
Now, with FastLED Version 2., here’s the equivalent code:
leds[i] += new color; // simple!
And not only is the source much cleaner and easier to write and maintain, but the underlying generated machine code is significantly smaller and faster, too.
Not only does FastLED make your animations run faster, but it helps you code faster, too.