Created
May 25, 2020 00:10
-
-
Save mcattani/1c8bdc7c87ebc0e07c0757ba25e66a7d to your computer and use it in GitHub Desktop.
Take on Me en Buzzer para Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//A-ha! | |
//by GeneralSpud | |
// For this to work, we need the pitches library | |
#include "pitches.h" | |
// Two things need to be created: the array for the notes of the melody (in order) | |
// and the duration of each (think of it like sheet music in two parts) | |
// BOTH ARRAYS MUST BE THE SAME SIZE! | |
// The melody array | |
int melody[] = { | |
NOTE_FS5, NOTE_FS5, NOTE_D5, NOTE_B4, NOTE_B4, NOTE_E5, | |
NOTE_E5, NOTE_E5, NOTE_GS5, NOTE_GS5, NOTE_A5, NOTE_B5, | |
NOTE_A5, NOTE_A5, NOTE_A5, NOTE_E5, NOTE_D5, NOTE_FS5, | |
NOTE_FS5, NOTE_FS5, NOTE_E5, NOTE_E5, NOTE_FS5, NOTE_E5 | |
}; | |
// The note duration, 8 = 8th note, 4 = quarter note, etc. | |
int durations[] = { | |
8, 8, 8, 4, 4, 4, | |
4, 5, 8, 8, 8, 8, | |
8, 8, 8, 4, 4, 4, | |
4, 5, 8, 8, 8, 8 | |
}; | |
// determine the length of the arrays to use in the loop iteration | |
int songLength = sizeof(melody)/sizeof(melody[0]); | |
void setup() { | |
//We don't need anything here | |
} | |
void loop() { | |
// Iterate through both arrays | |
// Notice how the iteration variable thisNote is created in the parenthesis | |
// The for loop stops when it is equal to the size of the melody array | |
for (int thisNote = 0; thisNote < songLength; thisNote++){ | |
// determine the duration of the notes that the computer understands | |
// divide 1000 by the value, so the first note lasts for 1000/8 milliseconds | |
int duration = 1000/ durations[thisNote]; | |
tone(8, melody[thisNote], duration); | |
// pause between notes | |
int pause = duration * 1.3; | |
delay(pause); | |
// stop the tone | |
noTone(8); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment