/*
Baby Elephant Walk
Connect a piezo buzzer or speaker to pin 11 or select a new pin.
More songs available at https://github.com/robsoncouto/arduino-songs
Robson Couto, 2019
Arduino | coding-help
Akithmari — 5/31/25 at 10:06 AM
Using tone() function. Its just making noise.
Its the same code that i used last year. Anybody know why?
(I'll upload the code after this)
*/
#include "pitches.h"
// change this to make the song slower or faster
int tempo = 132;
// change this to whichever pin you want to use
int buzzer = 11;
// notes of the melody followed by the duration.
// a 4 means a quarter note, 8 an eighth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted notes,
// so -4 means a dotted quarter note, that is, a quarter plus an eighth!!
/*int melody[] = { // Akithmari melody
// Baris 1 // 2142 dotted half, 1071 dotted quarter, 714 quarter, 357 eighth,
NOTE_G4, 4, NOTE_C5, -4, NOTE_G4, 8, NOTE_G4, 4, NOTE_F4, 4, NOTE_F4, -4, NOTE_E4, 8, NOTE_E4, 4, NOTE_E4, 4,
NOTE_F4, 4, NOTE_E4, 4, NOTE_D4, 4, NOTE_C4, 4, NOTE_D4, -2,
// Baris 2
NOTE_E4, 8, NOTE_F4, 8, NOTE_G4, 4, NOTE_G4, 4, NOTE_G4, 4, NOTE_F4, 8, NOTE_E4, 8, NOTE_A4, 4, NOTE_B4, 4,
NOTE_C5, 4, NOTE_A4, 4, NOTE_G4, 4, NOTE_E4, 4, NOTE_F4, 4, NOTE_A4, 4, NOTE_G4, -2
};
*/
///*
int melody[] = {
// Baby Elephant Walk
// Score available at https://musescore.com/user/7965776/scores/1862611
NOTE_C4, -8, NOTE_E4, 16, NOTE_G4, 8, NOTE_C5, 8, NOTE_E5, 8, NOTE_D5, 8, NOTE_C5, 8, NOTE_A4, 8,
NOTE_FS4, 8, NOTE_G4, 8, REST, 4, REST, 2,
NOTE_C4, -8, NOTE_E4, 16, NOTE_G4, 8, NOTE_C5, 8, NOTE_E5, 8, NOTE_D5, 8, NOTE_C5, 8, NOTE_A4, 8,
NOTE_G4, -2, NOTE_A4, 8, NOTE_DS4, 1,
NOTE_A4, 8,
NOTE_E4, 8, NOTE_C4, 8, REST, 4, REST, 2,
NOTE_C4, -8, NOTE_E4, 16, NOTE_G4, 8, NOTE_C5, 8, NOTE_E5, 8, NOTE_D5, 8, NOTE_C5, 8, NOTE_A4, 8,
NOTE_FS4, 8, NOTE_G4, 8, REST, 4, REST, 4, REST, 8, NOTE_G4, 8,
NOTE_D5, 4, NOTE_D5, 4, NOTE_B4, 8, NOTE_G4, 8, REST, 8, NOTE_G4, 8,
NOTE_C5, 4, NOTE_C5, 4, NOTE_AS4, 16, NOTE_C5, 16, NOTE_AS4, 16, NOTE_G4, 16, NOTE_F4, 8, NOTE_DS4, 8,
NOTE_FS4, 4, NOTE_FS4, 4, NOTE_F4, 16, NOTE_G4, 16, NOTE_F4, 16, NOTE_DS4, 16, NOTE_C4, 8, NOTE_G4, 8,
NOTE_AS4, 8, NOTE_C5, 8, REST, 4, REST, 2,
};
//*/
// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int notes = sizeof(melody) / sizeof(melody[0]) / 2;
// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
void setup() {
// iterate over the notes of the melody.
// Remember, the array is twice the number of notes (notes + durations)
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
// calculates the duration of each note
divider = melody[thisNote + 1];
if (divider > 0) {
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(buzzer, melody[thisNote], noteDuration * 0.9);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
// stop the waveform generation before the next note.
noTone(buzzer);
}
}
void loop() {
// no need to repeat the melody.
}