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*/
10
11// Perlin noise fire procedure
12// 16x16 rgb led matrix demo
13// Yaroslaw Turbin, 22.06.2020
14// https://vk.com/ldirko
15// https://www.reddit.com/user/ldirko/
16// https://www.reddit.com/r/FastLED/comments/hgu16i/my_fire_effect_implementation_based_on_perlin/
17// Based on the code found at: https://editor.soulmatelights.com/gallery/1229-
18
19// idea in make perlin noise with time offset X and Z coord
20// this automatic scroll fire pattern
21// and distort fire noise.
22// then substract Y based coodrd value to shift
23// fire color (not brightness) in palette.
24// this fadeout color from bottom matrix to up.
25// this need some palette tweak for good looking fire color
26
27#include "FastLED.h"
28#include "fl/ui.h"
29#include "fl/xymap.h"
30#include "fx/time.h"
31
32using namespace fl;
33
34#define HEIGHT 100
35#define WIDTH 100
36#define SERPENTINE true
37#define BRIGHTNESS 255
38
40
41UISlider scaleXY("Scale", 20, 1, 100, 1);
42UISlider speedY("SpeedY", 1, 1, 6, .1);
43UISlider invSpeedZ("Inverse SpeedZ", 20, 1, 100, 1);
44UISlider brightness("Brightness", 255, 0, 255, 1);
45UINumberField palette("Palette", 0, 0, 2);
46
48
50 // define fire palette
51 0, 0, 0, 0, // black
52 32, 255, 0, 0, // red
53 190, 255, 255, 0, // yellow
54 255, 255, 255, 255 // white
55};
56
57DEFINE_GRADIENT_PALETTE(electricGreenFirePal){
58 0, 0, 0, 0, // black
59 32, 0, 70, 0, // dark green
60 190, 57, 255, 20, // electric neon green
61 255, 255, 255, 255 // white
62};
63
64DEFINE_GRADIENT_PALETTE(electricBlueFirePal) {
65 0, 0, 0, 0, // Black
66 32, 0, 0, 70, // Dark blue
67 128, 20, 57, 255, // Electric blue
68 255, 255, 255, 255 // White
69};
70
72
73void setup() {
74 Serial.begin(115200);
75 FastLED.addLeds<NEOPIXEL, 3>(leds, HEIGHT * WIDTH).setScreenMap(xyMap);
76 FastLED.setCorrection(TypicalLEDStrip);
77}
78
79uint8_t getPaletteIndex(uint32_t millis32, int i, int j, uint32_t y_speed) {
80 // get palette index
81 uint16_t scale = scaleXY.as<uint16_t>();
82 uint16_t x = i * scale;
83 uint32_t y = j * scale + y_speed;
84 uint16_t z = millis32 / invSpeedZ.as<uint16_t>();
85 uint16_t noise16 = inoise16(x << 8, y << 8, z << 8);
86 uint8_t noise_val = noise16 >> 8;
87 int8_t subtraction_factor = abs8(j - (WIDTH - 1)) * 255 / (WIDTH - 1);
88 return qsub8(noise_val, subtraction_factor);
89}
90
92 // get palette
93 switch (palette) {
94 case 0:
95 return firepal;
96 case 1:
97 return electricGreenFirePal;
98 case 2:
99 return electricBlueFirePal;
100 default:
101 return firepal;
102 }
103}
104
105void loop() {
106 FastLED.setBrightness(brightness);
107 CRGBPalette16 myPal = getPalette();
108 uint32_t now = millis();
109 timeScale.setScale(speedY);
110 uint32_t y_speed = timeScale.update(now);
111 for (int i = 0; i < HEIGHT; i++) {
112 for (int j = 0; j < WIDTH; j++) {
113 uint8_t palette_index = getPaletteIndex(now, i, j, y_speed);
114 CRGB c = ColorFromPalette(myPal, palette_index, BRIGHTNESS);
115 int index = xyMap((HEIGHT - 1) - i, (WIDTH - 1) - j);
116 leds[index] = c;
117 }
118 }
119 FastLED.show();
120}
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
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:58
central include file for FastLED, defines the CFastLED class/object
uint32_t z[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:80
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:81
UINumberField palette("Palette", 0, 0, 2)
TimeScale timeScale(0, 1.0f)
UISlider speedY("SpeedY", 1.3, 1, 6,.1)
XYMap xyMap(HEIGHT, WIDTH, SERPENTINE)
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)
TimeScale 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()
UISlider scale("Scale", 4,.1, 4,.1)
int y_speed
RGB color palette with 16 discrete values.
LED controller for WS2812 LEDs with GRB color order.
Definition FastLED.h:138
#define DEFINE_GRADIENT_PALETTE(X)
Defines a static RGB palette very compactly using a series of connected color gradients.
Definition colorutils.h:69
@ 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:466
CRGB ColorFromPalette(const CRGBPalette16 &pal, uint8_t index, uint8_t brightness, TBlendType blendType)
Get a color from a palette.
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:54