Files
cube/cube.ino
T
mikeandClaude Opus 5.5 55b94475a5 Firmware, iOS-App und Gehäuse für den 5x5x5-LED-Würfel
ESP32-S3-Firmware mit 18 Effekten, Mikrofon (Klatschen, Beat, Spektrum)
und REST-API, Testprogramme, SwiftUI-Fernbedienung und OpenSCAD-Sockel.
Die WLAN-Zugangsdaten (secrets.h) sind nicht enthalten.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 15:00:43 +01:00

141 lines
5.0 KiB
Arduino
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 5x5x5 RGB-LED-Würfel – Demo
//
// Die Effekte (effects.cpp) laufen nacheinander in Endlosschleife und werden
// weich überblendet – jeweils innerhalb ihrer Kategorie (normal oder Musik). Taster 1: nächster Effekt, Taster 2: Helligkeit.
// Klatschen löst in jedem Effekt einen Blitz aus (audio.cpp).
// Steuerung per REST-API im WLAN: http://cube.local/api/... (web.cpp)
#include "cube.h"
CRGB leds[NUM_LEDS];
const uint8_t LEVELS[] = {BRIGHTNESS / 2, BRIGHTNESS, BRIGHTNESS * 2};
uint8_t brightnessLevel = 1;
uint8_t brightness, highlight;
bool autoAdvance = true;
void setBaseBrightness(uint8_t b) {
brightness = b;
highlight = min(255, b * 4);
}
void setLevel(uint8_t l) {
brightnessLevel = l % sizeof(LEVELS);
setBaseBrightness(LEVELS[brightnessLevel]);
}
struct Effect {
const char *id; // feste Kennung für die API
const char *name;
void (*render)(uint32_t now);
uint32_t ms;
bool music; // Musik-Visualisierung (reagiert auf das Mikrofon)
};
const Effect EFFECTS[] = {
{"gewitter", "Gewitter", renderStorm, 40000, false},
{"feuerwerk", "Feuerwerk", renderFireworks, 25000, false},
{"wuerfel", "Würfel im Würfel", renderShells, 20000, false},
{"feuer", "Feuer", renderFire, 20000, false},
{"matrix", "Matrix", renderMatrix, 20000, false},
{"regenbogen", "Regenbogen", renderRainbow, 20000, false},
{"flummis", "Flummis", renderBounce, 20000, false},
{"lavalampe", "Lavalampe", renderLava, 25000, false},
{"sinuswelle", "Sinuswelle", renderWave, 21000, false},
{"schnee", "Schnee", renderSnow, 25000, false},
{"doppelhelix", "Doppelhelix", renderHelix, 20000, false},
{"glitzer", "Glitzer", renderGlitter, 20000, false},
{"spektrum", "Spektrum", renderSpectrum, 30000, true},
{"puls", "Puls", renderPulse, 30000, true},
{"schockwelle", "Schockwelle", renderShockwave, 30000, true},
{"klangschichten", "Klangschichten", renderSoundLayers, 30000, true},
{"klangwelle", "Klangwelle", renderSoundWave, 30000, true},
{"disko", "Disko", renderDisco, 30000, true},
};
const uint8_t NUM_EFFECTS = sizeof(EFFECTS) / sizeof(EFFECTS[0]);
const uint32_t FADE_MS = 500;
uint8_t current = 0;
uint32_t effectStart = 0;
void startEffect(uint8_t i, uint32_t now) {
current = i % NUM_EFFECTS;
effectStart = now;
resetEffects();
FastLED.clear();
Serial.printf("Effekt: %s\n", EFFECTS[current].name);
}
// Nächster Effekt derselben Kategorie (Musik bzw. normale Effekte)
uint8_t nextEffect(uint8_t i) {
uint8_t n = i;
do n = (n + 1) % NUM_EFFECTS;
while (EFFECTS[n].music != EFFECTS[i].music);
return n;
}
uint8_t numEffects() { return NUM_EFFECTS; }
bool effectIsMusic(uint8_t i) { return EFFECTS[i % NUM_EFFECTS].music; }
const char *effectId(uint8_t i) { return EFFECTS[i % NUM_EFFECTS].id; }
const char *effectName(uint8_t i) { return EFFECTS[i % NUM_EFFECTS].name; }
uint8_t currentEffect() { return current; }
void selectEffect(uint8_t i) { startEffect(i, millis()); }
// Taster gegen GND mit Entprellung; pressed() meldet jeden Druck einmal.
struct Button {
uint8_t pin;
bool down = false;
uint32_t changed = 0;
bool pressed(uint32_t now) {
bool d = digitalRead(pin) == LOW;
if (d == down || now - changed < 30) return false;
down = d;
changed = now;
return d;
}
};
Button button1{BUTTON1_PIN}, button2{BUTTON2_PIN};
void setup() {
Serial.begin(115200);
Serial.setTxTimeoutMs(0); // Ausgaben nie blockieren, wenn am USB niemand mitliest
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
setLevel(1);
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, MAX_MILLIAMPS);
#ifdef RGB_BUILTIN
rgbLedWrite(RGB_BUILTIN, 0, 0, 0); // RGB-LED auf dem Board aus
#endif
random16_set_seed(esp_random());
audioBegin();
webBegin();
}
void loop() {
// Webseite zuerst: ihre Aktionen (Blitz, Effektwahl) nutzen millis() und
// dürfen nicht nach dem Zeitpunkt "now" dieses Frames liegen.
webUpdate(millis());
uint32_t now = millis();
if (button1.pressed(now)) startEffect(nextEffect(current), now);
if (button2.pressed(now)) {
setLevel(brightnessLevel + 1);
Serial.printf("Helligkeit: %u\n", brightness);
}
if (autoAdvance && now - effectStart >= EFFECTS[current].ms) startEffect(nextEffect(current), now);
audioUpdate(now);
if (audioClap()) triggerLightning(now);
EFFECTS[current].render(now);
drawSpecials(now);
// Ein- und Ausblenden beim Effektwechsel
uint32_t t = now - effectStart;
uint32_t edge = autoAdvance ? min(t, EFFECTS[current].ms - t) : t; // ohne Autowechsel kein Ausblenden
FastLED.setBrightness(edge < FADE_MS ? edge * 255 / FADE_MS : 255);
FastLED.show();
delay(16);
}