Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pitches.h"
- // notes in the melody: 0, = REST
- int melody[] = {
- NOTE_D4, NOTE_FS4, NOTE_A5, NOTE_D4,
- NOTE_G4, NOTE_B5, NOTE_B5, NOTE_A5,
- NOTE_FS4, NOTE_G4, NOTE_A5, NOTE_D4,
- NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4,
- NOTE_E4, NOTE_D4,
- NOTE_G4, NOTE_A5, NOTE_B5, NOTE_A5,
- NOTE_G4, NOTE_FS4, NOTE_FS4, NOTE_E4,
- NOTE_FS4, NOTE_G4, NOTE_A5, NOTE_D4, NOTE_G4, NOTE_FS4, NOTE_G4,
- NOTE_FS4, NOTE_E4, NOTE_D4,
- NOTE_CS5, NOTE_D5, NOTE_E5, NOTE_A4,
- NOTE_D5, NOTE_E5, NOTE_FS5,
- NOTE_CS5, NOTE_D5, NOTE_E5, NOTE_A4, NOTE_D5, NOTE_CS5, NOTE_D5,
- NOTE_CS5, NOTE_B4, NOTE_A4,
- NOTE_A4, NOTE_C5, NOTE_D5, NOTE_FS4,
- NOTE_G4, NOTE_B4, NOTE_B4, NOTE_A4,
- NOTE_D5, NOTE_C5, NOTE_D5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5,
- NOTE_D5, NOTE_C5, NOTE_D5
- };
- // note durations: 4 = quarter note, 8 = eighth note, etc.:
- int noteDurations[] = {
- 4, 4, 4, 4, /**/ 4, 4, 4, 4, /**/ 8, 8, 8, 8, 4, 8, 8,/**/ 4, 4, 2,
- 4, 4, 4, 4, /**/ 4, 4, 4, 4, /**/ 8, 8, 8, 8, 4, 8, 8,/**/ 4, 4, 2,
- 4, 4, 4, 4, /**/ 4, 4, 2, /**/ 8, 8, 8, 8, 4, 8, 8, /**/ 4, 4, 2,
- 4, 4, 4, 4, /**/ 4, 4, 4, 4, /**/ 8, 8, 8, 8, 8, 8, 8, 8, /**/ 4, 4, 2
- };
- void setup() {
- int numSize = 0; //this tells us the size of a int
- int numNotes = sizeof(noteDurations)/sizeof(numSize);
- // iterate over the notes of the melody:
- for (int thisNote = 0; thisNote < numNotes; thisNote++) {
- // to calculate the note duration, take one second
- // divided by the note type.
- //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
- int noteDuration = 1000/noteDurations[thisNote];
- tone(8, melody[thisNote],noteDuration);
- // to distinguish the notes, set a minimum time between them.
- // the note's duration + 30% seems to work well:
- int pauseBetweenNotes = noteDuration * 1.30;
- delay(pauseBetweenNotes);
- // stop the tone playing:
- noTone(8);
- }
- }
- void loop() {
- // no need to repeat the hymn.
- }
Advertisement
Add Comment
Please, Sign In to add comment