FastLED 3.9.12
Loading...
Searching...
No Matches
FxNoiseRing.ino
1
2
12
13#include <Arduino.h>
14
15#include <stdio.h>
16
17#include "fl/json.h"
18#include "fl/math_macros.h"
19#include "fl/warn.h"
20#include "noisegen.h"
21#include "fl/screenmap.h"
22#include "fl/slice.h"
23#include "fl/ui.h"
24#include "FastLED.h"
25#include "sensors/pir.h"
26#include "timer.h"
27
28#define LED_PIN 2
29#define COLOR_ORDER GRB
30#define NUM_LEDS 250
31#define PIN_PIR 0
32
33#define PIR_LATCH_MS 60000 // how long to keep the PIR sensor active after a trigger
34#define PIR_RISING_TIME 1000 // how long to fade in the PIR sensor
35#define PIR_FALLING_TIME 1000 // how long to fade out the PIR sensor
36
37using namespace fl;
38
39CRGB leds[NUM_LEDS];
40
41UISlider brightness("Brightness", 1, 0, 1);
42UISlider scale("Scale", 4, .1, 4, .1);
43UISlider timeBitshift("Time Bitshift", 5, 0, 16, 1);
44UISlider timescale("Time Scale", 1, .1, 10, .1);
45PirAdvanced pir(PIN_PIR, PIR_LATCH_MS, PIR_RISING_TIME, PIR_FALLING_TIME);
46UICheckbox useDither("Use Binary Dither", true);
47
48Timer timer;
49float current_brightness = 0;
50
51CLEDController* controller = nullptr;
52
53void handleSerialDither() {
54 if (Serial.available()) {
55 char input = Serial.read();
56 if (input == '0') {
57 useDither = false;
58 } else if (input == '1') {
59 useDither = true;
60 } else {
61 FASTLED_WARN("Invalid dither input. Use 0 or 1");
62 }
63 }
64}
65
66void setup() {
67 Serial.begin(115200);
68 ScreenMap xyMap = ScreenMap::Circle(NUM_LEDS, 2.0, 2.0);
69 controller = &FastLED.addLeds<WS2811, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS)
70 .setCorrection(TypicalLEDStrip)
71 .setDither(DISABLE_DITHER)
72 .setScreenMap(xyMap);
73 FastLED.setBrightness(brightness);
74 pir.activate(millis()); // Activate the PIR sensor on startup.
75}
76
77void loop() {
78 handleSerialDither(); // Add this line at start of loop()
79
80 controller->setDither(useDither ? BINARY_DITHER : DISABLE_DITHER);
82 FASTLED_WARN("loop");
83 }
84 uint8_t bri = pir.transition(millis());
85 FastLED.setBrightness(bri * brightness.as<float>());
86 uint32_t now = millis();
87 double angle_offset = double(now) / 32000.0 * 2 * M_PI;
88 now = (now << timeBitshift.as<int>()) * timescale.as<double>();
89 // go in circular formation and set the leds
90 for (int i = 0; i < NUM_LEDS; i++) {
91 float angle = i * 2 * M_PI / NUM_LEDS + angle_offset;
92 float x = cos(angle);
93 float y = sin(angle);
94 x *= 0xffff * scale.as<double>();
95 y *= 0xffff * scale.as<double>();
96 uint16_t noise = inoise16(x, y, now);
97 uint16_t noise2 = inoise16(x, y, 0xfff + now);
98 uint16_t noise3 = inoise16(x, y, 0xffff + now);
99 noise3 = noise3 >> 8;
100 int16_t noise4 = map(noise3, 0, 255, -64, 255);
101 if (noise4 < 0) {
102 noise4 = 0;
103 }
104 leds[i] = CHSV(noise >> 8, MAX(128, noise2 >> 8), noise4);
105 }
106 FastLED.show();
107}
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:45
central include file for FastLED, defines the CFastLED class/object
void setBrightness(uint8_t scale)
Set the global brightness scaling.
Definition FastLED.h:719
void show(uint8_t scale)
Update all our controllers with the current led colors, using the passed in brightness.
Definition FastLED.cpp:94
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:79
Base definition for an LED controller.
CLEDController & setDither(uint8_t ditherMode=BINARY_DITHER)
Set the dithering mode for this controller to use.
Definition timer.h:5
WS2811 controller class.
Definition FastLED.h:238
#define BINARY_DITHER
Enable dithering using binary dithering (only option)
Definition dither_mode.h:13
#define DISABLE_DITHER
Disable dithering.
Definition dither_mode.h:11
@ TypicalLEDStrip
Typical values for SMD5050 LEDs.
Definition color.h:19
uint16_t inoise16(uint32_t x, uint32_t y, uint32_t z)
16-bit, fixed point implementation of Perlin's noise.
Definition noise.cpp:384
#define EVERY_N_SECONDS(N)
Checks whether to execute a block of code every N seconds.
Definition lib8tion.h:1283
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Noise generation classes.
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54