int led_pin1 =2;
int led_pin2 =3;
int buzzer_pin = 11;
struct MusicStruct { //generating tones
int A = 550;
int As = 582;
int B = 617;
int C = 654;
int Cs = 693;
int D = 734;
int Ds = 777;
int E = 824;
int F = 873;
int Fs = 925;
int G = 980;
int Gs = 1003;
int A2 = 1100;
int A2s = 1165;
int B2 = 1234;
int C3 = 1308;
int C3s = 1385;
int D3 = 1555;
}Music;
struct LengthStruct {
float half = 0.5;
float one = 1.0;
float one_half = 1.5;
float two = 2.0;
float two_half = 2.5;
}Length;
int tempo = 400;
void setup() {
pinMode(led_pin1, OUTPUT); // set led as output
pinMode(led_pin2, OUTPUT);
pinMode(buzzer_pin, OUTPUT);
digitalWrite(led_pin1,HIGH); // and conditions for led
delay(1000);
digitalWrite(led_pin2,LOW);
delay(1000);
digitalWrite(led_pin2,HIGH);
delay(1000);
digitalWrite(led_pin1,LOW);
delay(1000);
}
void setTone(int pin, int note, int duration) {
tone(pin, note, duration);
delay(duration);
noTone(pin);
}
void loop() { //putting the buzzer conditions
setTone(buzzer_pin, Music.B, tempo * Length.one);
setTone(buzzer_pin, Music.E, tempo * Length.one_half);
setTone(buzzer_pin, Music.G, tempo * Length.half);
setTone(buzzer_pin, Music.F, tempo * Length.one);
setTone(buzzer_pin, Music.E, tempo * Length.two);
setTone(buzzer_pin, Music.B2, tempo * Length.one);
setTone(buzzer_pin, Music.A2, tempo * Length.two_half);
setTone(buzzer_pin, Music.Fs, tempo * Length.two_half);
setTone(buzzer_pin, Music.E, tempo * Length.one_half);
setTone(buzzer_pin, Music.G, tempo * Length.half);
setTone(buzzer_pin, Music.F, tempo * Length.one);
setTone(buzzer_pin, Music.Ds, tempo * Length.two);
setTone(buzzer_pin, Music.F, tempo * Length.one);
setTone(buzzer_pin, Music.B, tempo * Length.two_half);
delay(5000);
}
EXPANDED TECHNICAL DETAILS
Cinematic Audio Synthesis
This project recreates the magical atmosphere of the Harry Potter "Hedwig's Theme" using the Arduino's precise PWM timing and frequency-to-note mapping.
- Polyphonic Frequency Management: The Arduino uses the
tone()function to oscillate a piezo buzzer or high-impedance speaker. The complex, chromatic melody of the Harry Potter theme is stored as a series of constant integer arrays representing frequencies (e.g., $B3, E4, G4$) and rhythmic durations. - Tempo Control Logic: The firmware calculates the exact "Inter-Note" delay based on the song's triple-meter signature (3/4 time), ensuring the signature "Swing" of the orchestral original is preserved in the 8-bit digital output.
Visual Interaction
- Glowing "Magic" Feedback: (Advanced version) Synchronizes a series of Blue or White flickering LEDs with the melody's pitch; higher notes result in brighter pulses, creating a cohesive audiovisual experience.