FastLED 3.10.10
Loading...
Searching...
No Matches
Sailboat.ino
Go to the documentation of this file.
1
5
6// Ported from github.com/zackees/sailboat-lights (FastLED 3.6.0 era).
7// The effect logic lives in PerlinParticlePunch (Fx1d); this sketch wires up
8// hardware, audio detector callbacks, and color palettes.
9
10// @filter: (mem is large)
11
12#include <FastLED.h>
13
14#if defined(FL_IS_TEENSY)
15// Keep fbuild's library scanner aware of PJRC Audio sources for Teensy.
16#include <Audio.h>
17#endif
18
19#include "fl/channels/config.h"
22#include "fl/fx/fx_engine.h"
23#include "fl/ui/ui.h"
24
25
26// ---------------------------------------------------------------------------
27// Configuration
28// ---------------------------------------------------------------------------
29#define NUM_LEDS 200
30#define DATA_PIN 3
31
32#define BRIGHTNESS 255
33#define COLOR_ORDER RGB
34
35// I2S pins for INMP441 microphone (adjust for your board)
36#define I2S_WS 7
37#define I2S_SD 8
38#define I2S_CLK 4
39
40// ---------------------------------------------------------------------------
41// Globals
42// ---------------------------------------------------------------------------
47fl::UICheckbox enableAmbient("Ambient Particles", true);
48fl::UICheckbox enableMeteors("Beat Meteors", true);
49fl::UICheckbox enableTimeWarp("Noise Time-Warp", false);
50// Vibe bass is self-normalizing: ~1.0 = song average.
51// meteorThreshold: bass must exceed this multiple of average to spawn meteor.
52fl::UISlider meteorThreshold("Meteor Threshold", 1.5f, 1.5f, 6.0f, 0.1f);
53// ambientThreshold: bass above this spawns ambient particles.
54fl::UISlider ambientThreshold("Ambient Threshold", 1.0f, 0.5f, 3.0f, 0.1f);
55fl::UISlider dragSlider("Particle Drag", 0.06f, 0.0f, 1.0f, 0.01f);
56fl::UISlider speedSlider("Particle Speed", 2.6f, 0.1f, 3.0f, 0.1f);
57fl::UISlider ambientTrailSlider("Ambient Trail", 217.0f, 0.0f, 255.0f, 1.0f);
58fl::UISlider meteorTrailSlider("Meteor Trail", 171.0f, 0.0f, 255.0f, 1.0f);
59fl::UISlider ambientDecaySlider("Ambient Fade", 0.955f, 0.90f, 1.0f, 0.005f);
60fl::UISlider minVelocitySlider("Min Velocity", 0.01f, 0.01f, 0.5f, 0.01f);
61fl::UISlider debrisDecaySlider("Debris Fade", 0.96f, 0.80f, 1.0f, 0.01f);
62fl::UISlider debrisVelDecaySlider("Debris Drag", 0.95f, 0.85f, 1.0f, 0.01f);
64
66
67// Perlin time-warp state (decays each frame in loop())
69
70// Screen map: LEDs follow the main sail hypotenuse with natural sag (catenary)
72 fl::ScreenMap(NUM_LEDS, 0.15f, [](int index, fl::vec2f &pt_out) {
73 float t = float(index) / float(NUM_LEDS - 1);
74 // Straight line: lower-left (15, 5) → upper-right (95, 95)
75 // Y-up coordinate system: high y = top of screen
76 pt_out.x = 15.0f + t * 80.0f;
77 float straight_y = 5.0f + t * 90.0f;
78 // Sag: catenary droop, subtract to bulge downward visually
79 float sag = 15.0f * fl::sin(t * FL_M_PI);
80 pt_out.y = straight_y - sag;
81 });
82
83// ---------------------------------------------------------------------------
84// Arduino setup / loop
85// ---------------------------------------------------------------------------
86void setup() {
87 Serial.begin(115200);
88 //FastLED.setExclusiveDriver(fl::Bus::SPI);
92 fl::ClocklessChipset chipset(DATA_PIN, timing);
94 config.setScreenMap(screenMap);
95 // Link only the platform's default clockless driver for add(cfg).
96 FastLED.enable<fl::Bus::AUTO>();
97 FastLED.add(config);
98 FastLED.setBrightness(BRIGHTNESS);
99
100 // Group UI elements
101 enableAmbient.setGroup("Features");
102 enableMeteors.setGroup("Features");
103 enableTimeWarp.setGroup("Features");
104
105 meteorThreshold.setGroup("Audio Detection");
106 ambientThreshold.setGroup("Audio Detection");
107
108 dragSlider.setGroup("Physics");
109 speedSlider.setGroup("Physics");
110 minVelocitySlider.setGroup("Physics");
111
112 ambientTrailSlider.setGroup("Trails");
113 meteorTrailSlider.setGroup("Trails");
114 ambientDecaySlider.setGroup("Trails");
115 debrisDecaySlider.setGroup("Trails");
116 debrisVelDecaySlider.setGroup("Trails");
117
118 // Blue-white palettes
119 CRGBPalette16 bluePalette(CRGB(0, 0, 40), CRGB(0, 40, 120),
120 CRGB(100, 160, 255), CRGB(255, 255, 255));
121 sailboatFx.setNoisePalette(bluePalette);
122 sailboatFx.setAmbientPalette(bluePalette);
123 sailboatFx.setMeteorGradient(CRGB(255, 255, 255), // white-hot head
124 CRGB(140, 180, 255), // light blue mid
125 CRGB(0, 40, 120)); // deep blue tail
126
127 // Register effect with engine
128 fxEngine.addFx(sailboatFx);
129
130 // --- Wire up audio detector callbacks ---
131 // Uses the Vibe detector which self-normalizes bass levels:
132 // getVibeBass() ~1.0 = average for current song
133 // >1.0 = louder than average, <1.0 = quieter
134 // This approach works across all music volumes and styles.
135 auto audio = FastLED.add(audio_ui);
136 fxEngine.setAudio(audio); // delivers AudioBatch to effects via DrawContext
137 if (audio) {
138 // Called every frame with full vibe data — we do threshold
139 // checks here for both ambient and meteor spawning.
140 audio->onVibeLevels(
141 [&](const fl::audio::detector::VibeLevels &levels) {
142 float bass = levels.bass; // self-normalizing, ~1.0 avg
143
144 // Ambient: spawn on bass spikes above ambient threshold
145 if (enableAmbient && levels.bassSpike &&
146 bass > ambientThreshold.value()) {
147 // Scale intensity: map bass from threshold..threshold*2
148 // to 0.3..1.0
149 float thresh = ambientThreshold.value();
150 float intensity =
151 fl::clamp((bass - thresh) / thresh, 0.3f, 1.0f);
152 int count = 1 + int(intensity * 2.0f); // 1-3 particles
153 for (int i = 0; i < count; i++) {
154 sailboatFx.spawnAmbient(intensity);
155 }
156 }
157
158 // Meteor: spawn only on very strong bass hits
159 if (enableMeteors && levels.bassSpike &&
160 bass > meteorThreshold.value()) {
161 float thresh = meteorThreshold.value();
162 // Map bass from threshold..threshold*2 to 0.4..1.0
163 float intensity =
164 fl::clamp((bass - thresh) / thresh, 0.4f, 1.0f);
165 sailboatFx.spawnMeteor(intensity);
166 if (enableTimeWarp)
167 noiseTimeMultiplier = 1.0f + intensity * 2.0f;
168 }
169 });
170
171 // Drop detector: biggest moments (has built-in 2s cooldown)
172 audio->onDropImpact([&](float impact) {
173 if (enableMeteors)
174 sailboatFx.spawnMeteor(impact);
175 if (enableTimeWarp)
176 noiseTimeMultiplier = 2.0f + impact;
177 });
178 }
179}
180
181void loop() {
182 //FL_WARN("Loop");
183 //delay(10);
184 // Decay time-warp back to normal
185 noiseTimeMultiplier *= 0.95f;
186 if (noiseTimeMultiplier < 1.01f)
187 noiseTimeMultiplier = 1.0f;
188 sailboatFx.setTimeMultiplier(noiseTimeMultiplier);
189
190 // Update physics params from sliders
191 float drag = 0.99f - dragSlider.value() * 0.19f;
192 sailboatFx.setDrag(drag);
193 sailboatFx.setSpeed(speedSlider.value());
194 sailboatFx.setAmbientTrailIntensity(u8(ambientTrailSlider));
195 sailboatFx.setMeteorTrailIntensity(u8(meteorTrailSlider));
196 sailboatFx.setAmbientBrightnessDecay(ambientDecaySlider.value());
197 sailboatFx.setMinVelocity(minVelocitySlider.value());
198 sailboatFx.setDebrisBrightnessDecay(debrisDecaySlider.value());
199 sailboatFx.setDebrisVelocityDecay(debrisVelDecaySlider.value());
200
201 // Draw current effect and show
202 fxEngine.draw(millis(), leds);
203 FastLED.show();
204 //delay(10);
205}
CRGB leds[1]
#define COLOR_ORDER
fl::UIAudio audio("Audio Input")
fl::FxEngine fxEngine(NUM_LEDS)
#define NUM_LEDS
#define BRIGHTNESS
#define I2S_CLK
#define I2S_SD
#define I2S_WS
#define DATA_PIN
Definition ClientReal.h:82
fl::UIAudio audio_ui("Audio Input")
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
fl::UISlider speedSlider("Particle Speed", 2.6f, 0.1f, 3.0f, 0.1f)
fl::UICheckbox enableMeteors("Beat Meteors", true)
fl::UISlider ambientTrailSlider("Ambient Trail", 217.0f, 0.0f, 255.0f, 1.0f)
fl::UISlider minVelocitySlider("Min Velocity", 0.01f, 0.01f, 0.5f, 0.01f)
fl::FxEngine fxEngine(NUM_LEDS)
fl::audio::Config audioConfig
Definition Sailboat.ino:44
fl::UICheckbox enableAmbient("Ambient Particles", true)
void setup()
Definition Sailboat.ino:86
fl::UISlider ambientDecaySlider("Ambient Fade", 0.955f, 0.90f, 1.0f, 0.005f)
fl::UICheckbox enableTimeWarp("Noise Time-Warp", false)
fl::UIAudio audio_ui("Audio Input", audioConfig)
fl::UISlider debrisVelDecaySlider("Debris Drag", 0.95f, 0.85f, 1.0f, 0.01f)
float noiseTimeMultiplier
Definition Sailboat.ino:68
fl::UISlider dragSlider("Particle Drag", 0.06f, 0.0f, 1.0f, 0.01f)
fl::UISlider meteorTrailSlider("Meteor Trail", 171.0f, 0.0f, 255.0f, 1.0f)
fl::UISlider debrisDecaySlider("Debris Fade", 0.96f, 0.80f, 1.0f, 0.01f)
fl::UISlider meteorThreshold("Meteor Threshold", 1.5f, 1.5f, 6.0f, 0.1f)
fl::PerlinParticlePunch sailboatFx(NUM_LEDS)
fl::UISlider ambientThreshold("Ambient Threshold", 1.0f, 0.5f, 3.0f, 0.1f)
void loop()
Definition Sailboat.ino:181
Runtime chipset timing configuration for clockless LED drivers.
Manages and renders multiple visual effects (Fx) for LED strips.
Definition fx_engine.h:33
static Config CreateInmp441(int pin_ws, int pin_sd, int pin_clk, AudioChannel channel, u16 sample_rate=44100ul, int i2s_num=0) FL_NO_EXCEPT
Definition input.h:153
fl::ScreenMap screenMap
Definition Corkscrew.h:101
static uint32_t t
Definition Luminova.h:55
@ TypicalLEDStrip
Typical values for SMD5050 LEDs.
Definition color.h:15
fl::CRGB CRGB
Definition crgb.h:25
#define FL_M_PI
Definition math.h:34
unsigned char u8
Definition stdint.h:132
enable_if< is_fixed_point< T >::value, T >::type sin(T angle) FL_NO_EXCEPT
constexpr ChipsetTimingConfig makeTimingConfig() FL_NO_EXCEPT
Convert compile-time CHIPSET type to runtime timing config.
constexpr enable_if< is_fixed_point< T >::value, T >::type clamp(T x, T lo, T hi) FL_NO_EXCEPT
vec2< float > vec2f
Definition geometry.h:333
@ AUTO
Definition bus.h:32
Audio-reactive perlin noise background with ambient particles and beat meteor overlay.
void setScreenMap(const fl::ScreenMap &map) FL_NO_EXCEPT
Set screen map for JS canvas visualization.
Definition config.h:255
Configuration for a single LED channel.
Definition config.h:167
Optional channel configuration parameters All fields have sensible defaults and can be overridden as ...
Definition options.h:93
Clockless chipset configuration (single data pin)
Definition config.h:33
value_type y
Definition geometry.h:191
value_type x
Definition geometry.h:190
#define Serial
Definition serial.h:314
Aggregator header for the fl/ui/ family of per-element UI types.