กลับไปหน้ารวมไฟล์
happy-birthday-lights-and-sounds-ea668e-en.md

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!

button_led_basic_interaction_1772681969235.png

Multi-Array Struct Execution

You cannot use three separate arrays (melody[], durations[], ledPattern[]) and hope they line up perfectly; the timing will always crash.

  1. 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!
  1. The loop() iterates exclusively through song[i].
  2. In one massive execution block, the Arduino fires tone(buzzerPin, song[i].freq), instantly triggers the LEDs: digitalWrite(LED1, song[i].ledStatus1), then mathematically delays delay(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 while loop 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!)

ข้อมูล Frontmatter ดั้งเดิม

title: "Happy Birthday: Lights and Sounds"
description: "Choreographed chiptune arrays! Construct a massively complex, sequentially timed polyphonic sound-and-light-show matrix, perfectly syncing the mathematical duration of musical notes to physical LED explosions."
category: "Audio & Sound"
difficulty: "Intermediate"