FastLED 3.9.15
Loading...
Searching...
No Matches
FxNoiseRing.ino
Go to the documentation of this file.
1// @filter: (memory is large)
2
12
13// PCH-compatible include order: Arduino.h and FastLED.h must come first
14#include <Arduino.h>
15#include "FastLED.h"
16
17// Now we can include other headers and do platform checks
18#include "fl/stl/json.h"
19#include "fl/math/math.h"
20#include "fl/math/math.h"
21#include "fl/log/log.h"
22#include "noisegen.h"
23#include "fl/math/screenmap.h"
24#include "fl/stl/span.h"
25#include "fl/ui/ui.h"
26#include "fl/sensors/pir.h"
27#include "fl/stl/sstream.h"
28#include "fl/stl/assert.h"
29#include "fl/gfx/noise/noise.h"
30
31// Defines come after all includes
32#ifndef DATA_PIN
33#define DATA_PIN 3
34#endif // DATA_PIN
35
36#define COLOR_ORDER GRB // Color order matters for a real device, web-compiler will ignore this.
37#define NUM_LEDS 250
38#define PIN_PIR 0
39
40#define PIR_LATCH_MS 60000 // how long to keep the PIR sensor active after a trigger
41#define PIR_RISING_TIME 1000 // how long to fade in the PIR sensor
42#define PIR_FALLING_TIME 1000 // how long to fade out the PIR sensor
43
45
46// These sliders and checkboxes are dynamic when using the FastLED web compiler.
47// When deployed to a real device these elements will always be the default value.
48fl::UISlider brightness("Brightness", 1, 0, 1);
49fl::UISlider scale("Scale", 4, .1, 4, .1);
50fl::UISlider timeBitshift("Time Bitshift", 5, 0, 16, 1);
51fl::UISlider timescale("Time Scale", 1, .1, 10, .1);
52// This PIR type is special because it will bind to a pin for a real device,
53// but also provides a UIButton when run in the simulator.
55fl::UICheckbox useDither("Use Binary Dither", true);
56
58
59// Save a pointer to the controller so that we can modify the dither in real time.
61
62void setup() {
63 Serial.begin(115200);
64 // ScreenMap is purely something that is needed for the sketch to correctly
65 // show on the web display. For deployements to real devices, this essentially
66 // becomes a no-op.
68 controller = &FastLED.addLeds<WS2811, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
69 .setCorrection(TypicalLEDStrip)
70 .setDither(DISABLE_DITHER)
71 .setScreenMap(xyMap);
72 FastLED.setBrightness(brightness);
73 pir.activate(fl::millis()); // Activate the PIR sensor on startup.
74}
75
76void draw(uint32_t now) {
77 double angle_offset = double(now) / 32000.0 * 2 * FL_M_PI;
78 now = (now << timeBitshift.as<int>()) * timescale.as<double>();
79
80 // get radius/zoom level from slider
81 float noise_radius = scale.as<float>();
82
83 // go in circular formation and set the leds
84 for (int i = 0; i < NUM_LEDS; i++) {
85 float angle = i * 2 * FL_M_PI / NUM_LEDS + angle_offset;
86
87 // Use the new noiseRingHSV8 function to sample three z-slices for HSV components
88 CHSV hsv = fl::noiseRingHSV8(angle, now, noise_radius);
89
90 // Apply same constraints as before: minimum saturation and adjusted value
91 hsv.s = fl::max(128u, (unsigned)hsv.s);
92 // Apply value mapping similar to original: map from 0-255 to -64-255, clamp to 0-255
93 uint16_t val = hsv.v;
94 int16_t adjusted_val = map(val, 0, 255, -64, 255);
95 if (adjusted_val < 0) {
96 adjusted_val = 0;
97 }
98 hsv.v = adjusted_val;
99
100 leds[i] = hsv;
101 }
102}
103
104void loop() {
105 // Allow the dither to be enabled and disabled.
107 uint32_t now = fl::millis();
108 uint8_t bri = pir.transition(now);
109 FastLED.setBrightness(bri * brightness.as<float>());
110 // Apply leds generation to the leds.
111 draw(now);
112
113 FastLED.show();
114}
#define COLOR_ORDER
fl::XYMap xyMap
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
#define DATA_PIN
Definition ClientReal.h:82
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
fl::UICheckbox useDither("Use Binary Dither", true)
#define PIR_FALLING_TIME
void setup()
#define PIN_PIR
CLEDController * controller
void draw(uint32_t now)
#define PIR_LATCH_MS
fl::UISlider timeBitshift("Time Bitshift", 5, 0, 16, 1)
float current_brightness
fl::Pir pir(PIN_PIR, PIR_LATCH_MS, PIR_RISING_TIME, PIR_FALLING_TIME)
fl::UISlider brightness("Brightness", 1, 0, 1)
fl::UISlider timescale("Time Scale", 1,.1, 10,.1)
fl::UISlider scale("Scale", 4,.1, 4,.1)
#define PIR_RISING_TIME
void loop()
Definition pir.h:38
static ScreenMap Circle(int numLeds, float cm_between_leds=1.5f, float cm_led_diameter=0.5f, float completion=1.0f) FL_NOEXCEPT
fl::CLEDController CLEDController
#define BINARY_DITHER
Enable dithering using binary dithering (only option)
Definition dither_mode.h:12
#define DISABLE_DITHER
Disable dithering.
Definition dither_mode.h:10
Functions to generate noise patterns on rings and spheres.
@ TypicalLEDStrip
Typical values for SMD5050 LEDs.
Definition color.h:15
fl::hsv8 CHSV
Definition chsv.h:11
CHSV noiseRingHSV8(float angle, u32 time, float radius)
Generate HSV8 (8-bit) noise for a ring pattern.
Definition noise.cpp.hpp:51
FastLED's Elegant JSON Library: fl::json
Centralized logging categories for FastLED hardware interfaces and subsystems.
#define FL_M_PI
Definition math.h:34
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
Noise generation classes.
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
#define Serial
Definition serial.h:304
Aggregator header for the fl/ui/ family of per-element UI types.