FastLED 3.9.15
Loading...
Searching...
No Matches
FxNoiseRing.ino
Go to the documentation of this file.
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 // Color order matters for a real device, web-compiler will ignore this.
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
40
41// These sliders and checkboxes are dynamic when using the FastLED web compiler.
42// When deployed to a real device these elements will always be the default value.
43UISlider brightness("Brightness", 1, 0, 1);
44UISlider scale("Scale", 4, .1, 4, .1);
45UISlider timeBitshift("Time Bitshift", 5, 0, 16, 1);
46UISlider timescale("Time Scale", 1, .1, 10, .1);
47// This PIR type is special because it will bind to a pin for a real device,
48// but also provides a UIButton when run in the simulator.
50UICheckbox useDither("Use Binary Dither", true);
51
54
55// Save a pointer to the controller so that we can modify the dither in real time.
57
58
59void setup() {
60 Serial.begin(115200);
61 // ScreenMap is purely something that is needed for the sketch to correctly
62 // show on the web display. For deployements to real devices, this essentially
63 // becomes a no-op.
66 .setCorrection(TypicalLEDStrip)
67 .setDither(DISABLE_DITHER)
68 .setScreenMap(xyMap);
69 FastLED.setBrightness(brightness);
70 pir.activate(millis()); // Activate the PIR sensor on startup.
71}
72
73void draw(uint32_t now) {
74 double angle_offset = double(now) / 32000.0 * 2 * M_PI;
75 now = (now << timeBitshift.as<int>()) * timescale.as<double>();
76 // go in circular formation and set the leds
77 for (int i = 0; i < NUM_LEDS; i++) {
78 // Sorry this is a little convoluted but we are applying noise
79 // in the 16-bit space and then mapping it back to 8-bit space.
80 // All the constants were experimentally determined.
81 float angle = i * 2 * M_PI / NUM_LEDS + angle_offset;
82 float x = cos(angle);
83 float y = sin(angle);
84 x *= 0xffff * scale.as<double>();
85 y *= 0xffff * scale.as<double>();
86 uint16_t noise = inoise16(x, y, now);
87 uint16_t noise2 = inoise16(x, y, 0xfff + now);
88 uint16_t noise3 = inoise16(x, y, 0xffff + now);
89 noise3 = noise3 >> 8;
90 int16_t noise4 = map(noise3, 0, 255, -64, 255);
91 if (noise4 < 0) { // Clamp negative values to 0.
92 noise4 = 0;
93 }
94 // Shift back to 8-bit space.
95 leds[i] = CHSV(noise >> 8, MAX(128, noise2 >> 8), noise4);
96 }
97}
98
99void loop() {
100 // Allow the dither to be enabled and disabled.
102 uint32_t now = millis();
103 uint8_t bri = pir.transition(now);
104 FastLED.setBrightness(bri * brightness.as<float>());
105 // Apply leds generation to the leds.
106 draw(now);
107 FastLED.show();
108}
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
#define NUM_LEDS
Definition Apa102.ino:6
#define COLOR_ORDER
#define LED_PIN
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:62
central include file for FastLED, defines the CFastLED class/object
uint8_t noise[NUM_LAYERS][WIDTH][HEIGHT]
Definition Fire2023.ino:88
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint8_t noise2[NUM_LAYERS][WIDTH][HEIGHT]
Definition Fire2023.ino:89
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
UISlider scale("Scale", 4,.1, 4,.1)
#define PIR_FALLING_TIME
void setup()
UISlider timescale("Time Scale", 1,.1, 10,.1)
Pir pir(PIN_PIR, PIR_LATCH_MS, PIR_RISING_TIME, PIR_FALLING_TIME)
#define PIN_PIR
CLEDController * controller
void draw(uint32_t now)
#define PIR_LATCH_MS
UISlider timeBitshift("Time Bitshift", 5, 0, 16, 1)
float current_brightness
Timer timer
UICheckbox useDither("Use Binary Dither", true)
#define PIR_RISING_TIME
UISlider brightness("Brightness", 1, 0, 1)
void loop()
Base definition for an LED controller.
A simple timer utility class for tracking timed events.
Definition timer.h:12
WS2811 controller class.
Definition FastLED.h:258
Definition pir.h:42
static ScreenMap Circle(int numLeds, float cm_between_leds=1.5f, float cm_led_diameter=0.5f, float completion=1.0f)
Definition screenmap.cpp:21
#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
uint16_t scale
Definition funky.cpp:83
XYMap xyMap
Definition gfx.cpp:8
@ TypicalLEDStrip
Typical values for SMD5050 LEDs.
Definition color.h:19
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:440
#define M_PI
Definition math_macros.h:67
#define MAX(a, b)
Definition math_macros.h:11
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:55