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