FastLED 3.9.15
Loading...
Searching...
No Matches
FireCylinder.ino
Go to the documentation of this file.
1
2/*
3This demo is best viewed using the FastLED compiler.
4Install: pip install fastled
5Run: fastled <this sketch directory>
6This will compile and preview the sketch in the browser, and enable
7all the UI elements you see below.
8*/
9
10// Perlin noise fire procedure
11// 16x16 rgb led cylinder demo
12// Exactly the same as the FireMatrix example, but with a cylinder, meaning that the x=0
13// and x = len-1 are connected.
14// This also showcases the inoise16(x,y,z,t) function which handles 3D+time noise effects.
15// Keep in mind that a cylinder is embedded in a 3D space with time being used to add
16// additional noise to the effect.
17
18#include "FastLED.h"
19#include "fl/ui.h"
20#include "fl/xymap.h"
21#include "fx/time.h"
22
23using namespace fl;
24
25#define HEIGHT 100
26#define WIDTH 100
27#define SERPENTINE true
28#define BRIGHTNESS 255
29
30UITitle title("FireCylinder Demo");
31UIDescription description("This Fire demo wraps around the cylinder. It uses Perlin noise to create a fire effect.");
32
33
35
36UISlider scaleXY("Scale", 8, 1, 100, 1);
37UISlider speedY("SpeedY", 1.3, 1, 6, .1);
38UISlider scaleX("ScaleX", .3, 0.1, 3, .01);
39UISlider invSpeedZ("Inverse SpeedZ", 20, 1, 100, 1);
40UISlider brightness("Brightness", 255, 0, 255, 1);
41UINumberField palette("Palette", 0, 0, 2);
42
44
46 // define fire palette
47 0, 0, 0, 0, // black
48 32, 255, 0, 0, // red
49 190, 255, 255, 0, // yellow
50 255, 255, 255, 255 // white
51};
52
53DEFINE_GRADIENT_PALETTE(electricGreenFirePal){
54 0, 0, 0, 0, // black
55 32, 0, 70, 0, // dark green
56 190, 57, 255, 20, // electric neon green
57 255, 255, 255, 255 // white
58};
59
60DEFINE_GRADIENT_PALETTE(electricBlueFirePal){
61 0, 0, 0, 0, // Black
62 32, 0, 0, 70, // Dark blue
63 128, 20, 57, 255, // Electric blue
64 255, 255, 255, 255 // White
65};
66
68
69void setup() {
70 Serial.begin(115200);
71 FastLED.addLeds<NEOPIXEL, 3>(leds, HEIGHT * WIDTH).setScreenMap(xyMap);
72 FastLED.setCorrection(TypicalLEDStrip);
73}
74
75uint8_t getPaletteIndex(uint32_t millis32, int width, int max_width, int height, int max_height,
76 uint32_t y_speed) {
77 // get palette index
78 uint16_t scale = scaleXY.as<uint16_t>();
79 float xf = (float)width / (float)max_width;
80 uint8_t x = (uint8_t)(xf * 255);
81 uint32_t cosx = cos8(x);
82 uint32_t sinx = sin8(x);
83 float trig_scale = scale * scaleX.value();
84 cosx *= trig_scale;
85 sinx *= trig_scale;
86 uint32_t y = height * scale + y_speed; // swapped: now using height
87 uint16_t z = millis32 / invSpeedZ.as<uint16_t>();
88
89 uint16_t noise16 = inoise16(cosx << 8, sinx << 8, y << 8, 0);
90 uint8_t noise_val = noise16 >> 8;
91 int8_t subtraction_factor = abs8(height - (max_height - 1)) * 255 /
92 (max_height - 1); // swapped: now using height
93 return qsub8(noise_val, subtraction_factor);
94}
96 // get palette
97 switch (palette) {
98 case 0:
99 return firepal;
100 case 1:
101 return electricGreenFirePal;
102 case 2:
103 return electricBlueFirePal;
104 default:
105 return firepal;
106 }
107}
108
109void loop() {
110 FastLED.setBrightness(brightness);
111 CRGBPalette16 myPal = getPalette();
112 uint32_t now = millis();
113 timeScale.setScale(speedY);
114 uint32_t y_speed = timeScale.update(now);
115 for (int width = 0; width < WIDTH; width++) {
116 for (int height = 0; height < HEIGHT; height++) {
117 uint8_t palette_index =
118 getPaletteIndex(now, width, WIDTH, height, HEIGHT, y_speed);
119 CRGB c = ColorFromPalette(myPal, palette_index, BRIGHTNESS);
120 int index = xyMap((WIDTH - 1) - width, (HEIGHT - 1) - height);
121 leds[index] = c;
122 }
123 }
124 FastLED.show();
125}
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
CRGBPalette16 getPalette()
UINumberField palette("Palette", 0, 0, 2)
TimeScale timeScale(0, 1.0f)
UIDescription description("This Fire demo wraps around the cylinder. It uses Perlin noise to create a fire effect.")
UISlider speedY("SpeedY", 1.3, 1, 6,.1)
void setup()
XYMap xyMap(HEIGHT, WIDTH, SERPENTINE)
uint8_t getPaletteIndex(uint32_t millis32, int width, int max_width, int height, int max_height, uint32_t y_speed)
UISlider scaleXY("Scale", 8, 1, 100, 1)
UISlider invSpeedZ("Inverse SpeedZ", 20, 1, 100, 1)
UISlider scaleX("ScaleX",.3, 0.1, 3,.01)
UITitle title("FireCylinder Demo")
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.
LIB8STATIC uint8_t cos8(uint8_t theta)
Fast 8-bit approximation of cos(x).
Definition trig8.h:266
#define sin8
Platform-independent alias of the fast sin implementation.
Definition trig8.h:216
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