Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
First few bars of Woe Is Me (World's Smallest Violin) from Spongebob Square Pants
/*
First few bars of Woe Is Me (World's Smallest Violin) from Spongebob Square Pants
Requires pitches.h
nina@ninalp.com
*/
#include "pitches.h"
const int speakerPinMelody = 26;
// notes in the melody:
int melody[] = {
NOTE_E5, NOTE_A4,
NOTE_F5, NOTE_E5, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_G4, NOTE_G5, NOTE_F5,
NOTE_E5,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_D5,
NOTE_E5, NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5,
NOTE_A5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5
};
// note durations: 2 = half note, 4 = quarter note, 8 = eighth note, etc.:
int durations[] = {
2, 1,
2, 4, 2,
4, 2, 2, 2, 4,
1,
2, 4, 2, 4,
2, 4, 2, 4, 4,
2, 4, 4, 4, 1
};
void setup() {
pinMode(speakerPinMelody, OUTPUT);
}
void playSong() {
// iterate over the notes of the melody:
int totalNotes = int(sizeof(melody));
for (int thisNote = 0; thisNote < totalNotes; 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 / durations[thisNote];
tone(speakerPinMelody, melody[thisNote], noteDuration);
// tone(pin, 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(speakerPinMelody);
}
delay(0);
}
void loop() {
// no need to repeat the melody.
playSong();
}
@farazkaleemmalik
Copy link

farazkaleemmalik commented Aug 13, 2020

epic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment