Choreographed Arrays: Happy Birthday Lights & Sounds
Playing a song with a buzzer is simple array traversal. The Happy Birthday Lights and Sounds project violently ramps up the complexity by requiring the programmer to synchronize multiple output domains simultaneously. You must ensure that the microscopic C++ execution of blasting a 1000-millisecond musical note exactly correlates with an LED fading up, hitting its peak exactly on the downbeat!

Multi-Array Struct Execution
You cannot use three separate arrays (melody[], durations[], ledPattern[]) and hope they line up perfectly; the timing will always crash.
- You must build a unified, monolithic C++ Struct.
struct Beat {
int freq; // e.g. 262 (C4 note)
int dur; // e.g. 250 (Quarter note)
byte ledStatus1; // e.g. HIGH or LOW
byte ledStatus2; // e.g. HIGH or LOW
};
Beat song[25] = { ... }; // Massive song database!
- The
loop()iterates exclusively throughsong[i]. - In one massive execution block, the Arduino fires
tone(buzzerPin, song[i].freq), instantly triggers the LEDs:digitalWrite(LED1, song[i].ledStatus1), then mathematically delaysdelay(song[i].dur * 1.3).
Eliminating delay() for True Synthesizer Fading
If you want the LED to softly "Fade" (analogWrite()) perfectly into the beat instead of just instantly turning on, delay() will completely destroy the code.
- You must use raw
millis()tracking. - While the Buzzer is screaming the 400ms musical note, the empty
whileloop aggressively runs an analog PWM dimming curve, causing the NeoPixel to swell up precisely before the note stops! - This guarantees a flawlessly synchronized, cinematic audio-visual hardware presentation!
Chiptune Synchronization Hardware
- Arduino Uno/Nano (Standard form factor execution).
- Standard Piezoelectric Passive Buzzer (To generate the frequency).
- Arrays of Red, Green, and Blue 5mm LEDs, or specifically addressable WS2812Bs.
- (Note: Ensure the LEDs and Buzzer are placed on completely separate hardware timers! If the Buzzer is on Pin 9 (Timer 1), and you use
analogWrite()on Pin 10 (Timer 1), the PWM frequency will mathematically obliterate the tone frequency!)