FastLED 3.9.15
Loading...
Searching...
No Matches
FireMatrix.ino
Go to the documentation of this file.
1
2
3/*
4This demo is best viewed using the FastLED compiler.
5Install: pip install fastled
6Run: fastled <this sketch directory>
7This will compile and preview the sketch in the browser, and enable
8all the UI elements you see below.
9
10OVERVIEW:
11This sketch creates a fire effect using Perlin noise on a matrix of LEDs.
12The fire appears to move upward with colors transitioning from black at the bottom
13to white at the top, with red and yellow in between (for the default palette).
14*/
15
16// Perlin noise fire procedure
17// 16x16 rgb led matrix demo
18// Yaroslaw Turbin, 22.06.2020
19// https://vk.com/ldirko
20// https://www.reddit.com/user/ldirko/
21// https://www.reddit.com/r/FastLED/comments/hgu16i/my_fire_effect_implementation_based_on_perlin/
22// Based on the code found at: https://editor.soulmatelights.com/gallery/1229-
23
24// HOW THE FIRE EFFECT WORKS:
25// 1. We use Perlin noise with time offset for X and Z coordinates
26// to create a naturally scrolling fire pattern that changes over time
27// 2. We distort the fire noise to make it look more realistic
28// 3. We subtract a value based on Y coordinate to shift the fire color in the palette
29// (not just brightness). This creates a fade-out effect from the bottom of the matrix to the top
30// 4. The palette is carefully designed to give realistic fire colors
31
32#include "FastLED.h" // Main FastLED library for controlling LEDs
33#include "fl/ui.h" // UI components for the FastLED web compiler (sliders, etc.)
34#include "fl/xymap.h" // Mapping between 1D LED array and 2D coordinates
35#include "fx/time.h" // Time manipulation utilities
36
37using namespace fl; // Use the FastLED namespace for convenience
38
39// Matrix dimensions - this defines the size of our virtual LED grid
40#define HEIGHT 100 // Number of rows in the matrix
41#define WIDTH 100 // Number of columns in the matrix
42#define SERPENTINE true // Whether the LED strip zigzags back and forth (common in matrix layouts)
43#define BRIGHTNESS 255 // Maximum brightness level (0-255)
44
45// TimeWarp helps control animation speed - it tracks time and allows speed adjustments
46TimeWarp timeScale(0, 1.0f); // Initialize with 0 starting time and 1.0 speed multiplier
47
48// UI Controls that appear in the FastLED web compiler interface:
49UISlider scaleXY("Scale", 20, 1, 100, 1); // Controls the size of the fire pattern
50UISlider speedY("SpeedY", 1, 1, 6, .1); // Controls how fast the fire moves upward
51UISlider invSpeedZ("Inverse SpeedZ", 20, 1, 100, 1); // Controls how fast the fire pattern changes over time (higher = slower)
52UISlider brightness("Brightness", 255, 0, 255, 1); // Controls overall brightness
53UINumberField palette("Palette", 0, 0, 2); // Selects which color palette to use (0=fire, 1=green, 2=blue)
54
55// Array to hold all LED color values - one CRGB struct per LED
57
58// Color palettes define the gradient of colors used for the fire effect
59// Each entry has the format: position (0-255), R, G, B
60
62 // Traditional fire palette - transitions from black to red to yellow to white
63 0, 0, 0, 0, // black (bottom of fire)
64 32, 255, 0, 0, // red (base of flames)
65 190, 255, 255, 0, // yellow (middle of flames)
66 255, 255, 255, 255 // white (hottest part/tips of flames)
67};
68
69DEFINE_GRADIENT_PALETTE(electricGreenFirePal){
70 // Green fire palette - for a toxic/alien look
71 0, 0, 0, 0, // black (bottom)
72 32, 0, 70, 0, // dark green (base)
73 190, 57, 255, 20, // electric neon green (middle)
74 255, 255, 255, 255 // white (hottest part)
75};
76
77DEFINE_GRADIENT_PALETTE(electricBlueFirePal) {
78 // Blue fire palette - for a cold/ice fire look
79 0, 0, 0, 0, // Black (bottom)
80 32, 0, 0, 70, // Dark blue (base)
81 128, 20, 57, 255, // Electric blue (middle)
82 255, 255, 255, 255 // White (hottest part)
83};
84
85// Create a mapping between 1D array positions and 2D x,y coordinates
87
88void setup() {
89 Serial.begin(115200); // Initialize serial communication for debugging
90
91 // Initialize the LED strip:
92 // - NEOPIXEL is the LED type
93 // - 3 is the data pin number (for real hardware)
94 // - setScreenMap connects our 2D coordinate system to the 1D LED array
95 FastLED.addLeds<NEOPIXEL, 3>(leds, HEIGHT * WIDTH).setScreenMap(xyMap);
96
97 // Apply color correction for more accurate colors on LED strips
98 FastLED.setCorrection(TypicalLEDStrip);
99}
100
101uint8_t getPaletteIndex(uint32_t millis32, int i, int j, uint32_t y_speed) {
102 // This function calculates which color to use from our palette for each LED
103
104 // Get the scale factor from the UI slider (controls the "size" of the fire)
105 uint16_t scale = scaleXY.as<uint16_t>();
106
107 // Calculate 3D coordinates for the Perlin noise function:
108 uint16_t x = i * scale; // X position (horizontal in matrix)
109 uint32_t y = j * scale + y_speed; // Y position (vertical) + movement offset
110 uint16_t z = millis32 / invSpeedZ.as<uint16_t>(); // Z position (time dimension)
111
112 // Generate 16-bit Perlin noise value using these coordinates
113 // The << 8 shifts values left by 8 bits (multiplies by 256) to use the full 16-bit range
114 uint16_t noise16 = inoise16(x << 8, y << 8, z << 8);
115
116 // Convert 16-bit noise to 8-bit by taking the high byte (>> 8 shifts right by 8 bits)
117 uint8_t noise_val = noise16 >> 8;
118
119 // Calculate how much to subtract based on vertical position (j)
120 // This creates the fade-out effect from bottom to top
121 // abs8() ensures we get a positive value
122 // The formula maps j from 0 to WIDTH-1 to a value from 255 to 0
123 int8_t subtraction_factor = abs8(j - (WIDTH - 1)) * 255 / (WIDTH - 1);
124
125 // Subtract the factor from the noise value (with underflow protection)
126 // qsub8 is a "saturating subtraction" - it won't go below 0
127 return qsub8(noise_val, subtraction_factor);
128}
129
130CRGBPalette16 getPalette() {
131 // This function returns the appropriate color palette based on the UI selection
132 switch (palette) {
133 case 0:
134 return firepal; // Traditional orange/red fire
135 case 1:
136 return electricGreenFirePal; // Green "toxic" fire
137 case 2:
138 return electricBlueFirePal; // Blue "cold" fire
139 default:
140 return firepal; // Default to traditional fire if invalid value
141 }
142}
143
144void loop() {
145 // The main program loop that runs continuously
146
147 // Set the overall brightness from the UI slider
148 FastLED.setBrightness(brightness);
149
150 // Get the selected color palette
151 CRGBPalette16 myPal = getPalette();
152
153 // Get the current time in milliseconds
154 uint32_t now = millis();
155
156 // Update the animation speed from the UI slider
157 timeScale.setSpeed(speedY);
158
159 // Calculate the current y-offset for animation (makes the fire move)
160 uint32_t y_speed = timeScale.update(now);
161
162 // Loop through every LED in our matrix
163 for (int i = 0; i < HEIGHT; i++) {
164 for (int j = 0; j < WIDTH; j++) {
165 // Calculate which color to use from our palette for this LED
166 uint8_t palette_index = getPaletteIndex(now, i, j, y_speed);
167
168 // Get the actual RGB color from the palette
169 // BRIGHTNESS ensures we use the full brightness range
170 CRGB c = ColorFromPalette(myPal, palette_index, BRIGHTNESS);
171
172 // Convert our 2D coordinates (i,j) to the 1D array index
173 // We use (HEIGHT-1)-i and (WIDTH-1)-j to flip the coordinates
174 // This makes the fire appear to rise from the bottom
175 int index = xyMap((HEIGHT - 1) - i, (WIDTH - 1) - j);
176
177 // Set the LED color in our array
178 leds[index] = c;
179 }
180 }
181
182 // Send the color data to the actual LEDs
183 FastLED.show();
184}
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
int y
Definition Audio.ino:72
#define WIDTH
Definition Audio.ino:33
int x
Definition Audio.ino:71
#define HEIGHT
Definition Audio.ino:32
XYMap xyMap(WIDTH, HEIGHT, false)
UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f)
#define SERPENTINE
Definition Blur2d.ino:16
#define BRIGHTNESS
Definition Blur.ino:8
CFastLED FastLED
Global LED strip management instance.
central include file for FastLED, defines the CFastLED class/object
uint32_t z[NUM_LAYERS]
Definition Fire2023.ino:84
UINumberField palette("Palette", 0, 0, 2)
UISlider speedY("SpeedY", 1.3, 1, 6,.1)
TimeWarp timeScale(0, 1.0f)
UISlider scaleXY("Scale", 8, 1, 100, 1)
UISlider invSpeedZ("Inverse SpeedZ", 20, 1, 100, 1)
UISlider brightness("Brightness", 255, 0, 255, 1)
CRGBPalette16 getPalette()
UINumberField palette("Palette", 0, 0, 2)
TimeWarp timeScale(0, 1.0f)
UISlider scaleXY("Scale", 20, 1, 100, 1)
void setup()
UISlider speedY("SpeedY", 1, 1, 6,.1)
uint8_t getPaletteIndex(uint32_t millis32, int i, int j, uint32_t y_speed)
XYMap xyMap(HEIGHT, WIDTH, SERPENTINE)
UISlider invSpeedZ("Inverse SpeedZ", 20, 1, 100, 1)
UISlider brightness("Brightness", 255, 0, 255, 1)
void loop()
int y_speed
LED controller for WS2812 LEDs with GRB color order.
Definition FastLED.h:155
#define DEFINE_GRADIENT_PALETTE(X)
Defines a static RGB palette very compactly using a series of connected color gradients.
Definition colorutils.h:112
@ TypicalLEDStrip
Typical values for SMD5050 LEDs.
Definition color.h:19
LIB8STATIC_ALWAYS_INLINE int8_t abs8(int8_t i)
Take the absolute value of a signed 8-bit uint8_t.
Definition math8.h:500
LIB8STATIC_ALWAYS_INLINE uint8_t qsub8(uint8_t i, uint8_t j)
Subtract one byte from another, saturating at 0x00.
Definition math8.h:103
uint16_t inoise16(uint32_t x, uint32_t y, uint32_t z, uint32_t t)
16-bit, fixed point implementation of Perlin's noise.
Definition noise.cpp:440
CRGB ColorFromPalette(const CRGBPalette16 &pal, uint8_t index, uint8_t brightness, TBlendType blendType)
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55