Ap11e & VeraRayne
VeraRayne VeraRayne
Hey Ap11e, I’ve been dreaming about turning a room into a living poem with fog, lights, and a little algorithm—think you could help me code the perfect mist dance?
Ap11e Ap11e
Sure thing. If you’re wiring a fog machine to a micro‑controller and running an LED strip in the same box, here’s a minimal Arduino sketch that syncs the two with a simple algorithm. It keeps a phase counter, ramps the fog pump PWM up and down, and slowly cycles the LED strip through a rainbow. Feel free to tweak the timing or add a sensor later. ```cpp // Hardware: // Fog machine pump -> PWM pin 5 (0–255) // WS2812b LED strip -> pin 6 (neopixel) // (Optional) Ambient light sensor -> A0 #include <Adafruit_NeoPixel.h> #define FOG_PIN 5 #define LED_PIN 6 #define NUM_LEDS 30 Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); int fogPWM = 0; // 0‑255 int fogDir = 1; // 1 for rising, -1 for falling int phase = 0; // 0‑255, drives both fog and color void setup() { pinMode(FOG_PIN, OUTPUT); strip.begin(); strip.show(); // Initialize all pixels to 'off' } void loop() { // Update phase phase = (phase + 2) % 256; // 2 is the step size; slow/fast // Fog PWM: triangle wave 0‑255 fogPWM = map(phase, 0, 255, 0, 255); analogWrite(FOG_PIN, fogPWM); // LED rainbow for (int i = 0; i < NUM_LEDS; i++) { // Hue offset per LED gives a moving rainbow uint16_t hue = (phase + i * 10) % 256; uint32_t color = strip.ColorHSV(hue * 360); strip.setPixelColor(i, color); } strip.show(); delay(20); // ~50 Hz refresh } ``` **What it does** 1. `phase` runs from 0 to 255 and drives both the fog intensity and the LED hue offset. 2. The fog pump PWM rises and falls smoothly, giving that gentle mist‑dance effect. 3. The LED strip cycles through a rainbow that lags slightly behind the fog, creating a sync that feels organic. **Next steps** - Add a potentiometer on A0 to let the user pick the fog speed. - Hook up a light sensor to dim the LEDs when the room gets bright. - Replace the static hue step with a Perlin‑noise function for more organic colour shifts. Happy hacking!