#define Pin_buzzer 13
// Jingle Bells
int melody = {
659, 659, 659,
659, 659, 659,
659, 784, 523, 587,
659,
698, 698, 698, 698,
698, 659, 659, 659, 659,
659, 587, 587, 659,
587, 784
};
int tempo[] = {
8, 8, 4,
8, 8, 4,
8, 8, 8, 8,
2,
8, 8, 8, 8,
8, 8, 8, 16, 16,
8, 8, 8, 8,
4, 4
};
void setup(void) {
pinMode(13, OUTPUT); // Buzzer
}
void loop() {
song_func();
}
int song = 0;
void song_func() {
int size = sizeof(melody) / sizeof(int);
for (int Note = 0; Note < size; Note++) {
int duration_note = 1000 / tempo[Note]; // To calculate the duration we divide the seconds by the type of the note
buzz(Pin_Buzzer, melody[Note], duration_note); // We call the buzz function
delay(duration_note * 1.30); // Break between each notes.
buzz(Pin_Buzzer, 0, duration_note); // Stop the music
}
}
void buzz(int pin_buzzer, long frequence, long length) {
long wainting_time = 1000000 / frequency / 2; // We caculate the value between each transition
long numCycles = frequency * length / 1000;
for (long i = 0; i < numCycles; i++) {
digitalWrite(pin_buzzer, HIGH); // We play the music
delayMicroseconds(temps_attente);
digitalWrite(pin_buzzer, LOW); // We stop the music
delayMicroseconds(wainting_time);
}
}