/* * Sailor Moon Wand LED Controller * -------------------------------- * MCU: ATtiny1604 * Core: megaTinyCore * * LED connections: * * Crown -> PA4 * Gem 1 -> PA5 * Gem 2 -> PB2 * Gem 3 -> PB1 * Gem 4 -> PB0 * Symbol -> PA3 * * Operation: * * On power-up: * * Crown -> Gem1 -> Gem2 -> Gem3 -> Gem4 -> Symbol * * Each LED remains illuminated after it turns on. * * After startup, Crown and Symbol remain continuously on. * * Gems 1-4 perform a continuous rotating brightness dip: * * Gem1 dims * Gem1 begins recovering while Gem2 dims * Gem2 begins recovering while Gem3 dims * Gem3 begins recovering while Gem4 dims * Gem4 begins recovering while Gem1 dims * * The wand is powered directly by the physical button, * so no button handling is required in software. */ // ============================================================ // PIN DEFINITIONS // ============================================================ const uint8_t CrownLED = PIN_PA4; const uint8_t Gem1LED = PIN_PA5; const uint8_t Gem2LED = PIN_PB2; const uint8_t Gem3LED = PIN_PB1; const uint8_t Gem4LED = PIN_PB0; const uint8_t SymbolLED = PIN_PA3; // Array makes it easy to iterate through the four animated gems. const uint8_t gemLEDs[4] = { Gem1LED, Gem2LED, Gem3LED, Gem4LED }; // ============================================================ // ANIMATION SETTINGS // ============================================================ // Time between each LED illuminating during startup. const uint16_t startupDelay = 250; // Delay between animation updates. // // Smaller = faster rotation. // Larger = slower rotation. // // 5-10ms should be a reasonable starting range. const uint8_t animationStepDelay = 3; // Lowest brightness reached by a gem. // // 0 = completely off at bottom of fade // 255 = no animation // // Starting at 35 so the gems never quite go completely dark. const uint8_t minimumBrightness = 35; // ============================================================ // SETUP // ============================================================ void setup() { // Configure all LED control pins as outputs. pinMode(CrownLED, OUTPUT); pinMode(Gem1LED, OUTPUT); pinMode(Gem2LED, OUTPUT); pinMode(Gem3LED, OUTPUT); pinMode(Gem4LED, OUTPUT); pinMode(SymbolLED, OUTPUT); // Make absolutely sure everything starts OFF. digitalWrite(CrownLED, LOW); analogWrite(Gem1LED, 0); analogWrite(Gem2LED, 0); analogWrite(Gem3LED, 0); analogWrite(Gem4LED, 0); digitalWrite(SymbolLED, LOW); // Small pause after power-up. // // This gives the MCU/power supply a moment to settle // and makes the beginning of the animation visually distinct. delay(200); // Run the startup sequence once. startupAnimation(); } // ============================================================ // MAIN LOOP // ============================================================ void loop() { rotatingGemAnimation(); } // ============================================================ // STARTUP ANIMATION // ============================================================ void startupAnimation() { // Crown first. digitalWrite(CrownLED, HIGH); delay(startupDelay); // Then each gem sequentially. analogWrite(Gem1LED, 255); delay(startupDelay); analogWrite(Gem2LED, 255); delay(startupDelay); analogWrite(Gem3LED, 255); delay(startupDelay); analogWrite(Gem4LED, 255); delay(startupDelay); // Symbol comes on last. digitalWrite(SymbolLED, HIGH); delay(startupDelay); } // ============================================================ // ROTATING GEM ANIMATION // ============================================================ void rotatingGemAnimation() { /* * "phase" represents our position through one complete * animation cycle. * * Because it's uint8_t, it automatically wraps: * * 254 * 255 * 0 * 1 * * which gives us an endlessly repeating animation. */ static uint8_t phase = 0; /* * Calculate brightness for each of the four gems. * * Each gem is shifted by 64 counts: * * 256 / 4 = 64 * * Therefore they're exactly 1/4 of an animation cycle * apart from each other. */ for (uint8_t gem = 0; gem < 4; gem++) { uint8_t gemPhase = phase + (gem * 64); uint8_t brightness = calculateBrightness(gemPhase); analogWrite(gemLEDs[gem], brightness); } // Advance animation by one step. phase++; // Controls overall animation speed. delay(animationStepDelay); } // ============================================================ // BRIGHTNESS CALCULATION // ============================================================ uint8_t calculateBrightness(uint8_t phase) { /* * Generate a triangular brightness waveform. * * * Bright * * 255 --------\ /-------- * \ / * \ / * \ / * \______/ * * minimumBrightness * * * First half of cycle: * brightness decreases * * Second half: * brightness increases */ uint8_t depth; if (phase < 128) { // Fade downward. depth = phase * 2; } else { // Fade upward. depth = (255 - phase) * 2; } /* * Scale the waveform between: * * minimumBrightness -> 255 * * rather than necessarily going completely dark. */ uint16_t brightnessRange = 255 - minimumBrightness; uint8_t brightness = 255 - ((uint16_t)depth * brightnessRange / 255); return brightness; }