/* Play "He's A Pirate" / "Pirates of the Caribbean" Theme Song
* By Xitang Zhao 2016.06.27
* Youtube in Action: https://youtu.be/sjPAj1lXgtk
* or TikTok in Action: https://www.tiktok.com/@tipstorylearn/video/6943019804261502213
*
* INSTRUCTION: Hook up Pin 10 to the positive side of a buzzer or a microphone, hook up
* any resistor to the negative side of a buzzer (to limit current & volume & to protect the pin),
* and then connect the other end of the resistor to ground pin. Upload the sketch and enjoy!
*
* Don't have an Arduino right now? No worry. You can test it vitually on my TinkerCAD circuit:
* https://www.tinkercad.com/things/f9QN4skaguI-play-pirates-of-the-caribbean-theme-song-on-buzzer
*
* To learn the science of buzzer, Arduino tone library, and a step by step walkthrough of how I made this
* Visit my learning note on TipStory: https://www.tipstory.org/learning/h2lUMccm5MeuSds
*
* Last updated: 2021.03.25
* ---------------------
* Credits:
*
* Music notes of the song obtained from Easy Music (Great website)
* Link: http://easymusic.altervista.org/recorder-hes-a-pirate-pirates-of-caribbean-sheet-music-guitar-chords/
*
* Musicnotes's "How to Read Sheet Music" Guide
* Link: http://www.musicnotes.com/blog/2014/04/11/how-to-read-sheet-music/
*
* Code inspired by Chapter 5 of Jeremy Blum's book "Exploring Arduino"
* Link: http://www.exploringarduino.com/content/ch5/
*
* Music notes' frequencies obtained from arduino website and Tone Library
* Link: https://www.arduino.cc/en/Tutorial/toneMelody
* Link: https://github.com/bhagman/Tone
*
*/
// Define pin 10 for buzzer, you can use any other digital pins (Pin 0-13)
const int buzzer = 10;
// Change to 0.5 for a slower version of the song, 1.25 for a faster version
const float songSpeed = 1.0;
// Defining frequency of each music note
#define C4 262
#define D4 294
#define E4 330
#define F4 349
#define G4 392
#define A4 440
#define B4 494
#define C5 523
#define D5 587
#define E5 659
#define F5 698
#define G5 784
#define A5 880
#define B5 988
// Music notes of the song, 0 is a rest/pulse
int notes[] = {
E4, G4, A4, A4, 0,
A4, B4, C5, C5, 0,
C5, D5, B4, B4, 0,
A4, G4, A4, 0,
E4, G4, A4, A4, 0,
A4, B4, C5, C5, 0,
C5, D5, B4, B4, 0,
A4, G4, A4, 0,
E4, G4, A4, A4, 0,
A4, C5, D5, D5, 0,
D5, E5, F5, F5, 0,
E5, D5, E5, A4, 0,
A4, B4, C5, C5, 0,
D5, E5, A4, 0,
A4, C5, B4, B4, 0,
C5, A4, B4, 0,
A4, A4,
//Repeat of first part
A4, B4, C5, C5, 0,
C5, D5, B4, B4, 0,
A4, G4, A4, 0,
E4, G4, A4, A4, 0,
A4, B4, C5, C5, 0,
C5, D5, B4, B4, 0,
A4, G4, A4, 0,
E4, G4, A4, A4, 0,
A4, C5, D5, D5, 0,
D5, E5, F5, F5, 0,
E5, D5, E5, A4, 0,
A4, B4, C5, C5, 0,
D5, E5, A4, 0,
A4, C5, B4, B4, 0,
C5, A4, B4, 0,
//End of Repeat
E5, 0, 0, F5, 0, 0,
E5, E5, 0, G5, 0, E5, D5, 0, 0,
D5, 0, 0, C5, 0, 0,
B4, C5, 0, B4, 0, A4,
E5, 0, 0, F5, 0, 0,
E5, E5, 0, G5, 0, E5, D5, 0, 0,
D5, 0, 0, C5, 0, 0,
B4, C5, 0, B4, 0, A4};
// Durations (in ms) of each music note of the song
// Quarter Note is 250 ms when songSpeed = 1.0
int durations[] = {
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 125, 250, 125,
125, 125, 250, 125, 125,
250, 125, 250, 125,
125, 125, 250, 125, 125,
125, 125, 375, 375,
250, 125,
//Rpeat of First Part
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 125, 250, 125,
125, 125, 250, 125, 125,
250, 125, 250, 125,
125, 125, 250, 125, 125,
125, 125, 375, 375,
//End of Repeat
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 125, 125, 125, 375,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 500,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 125, 125, 125, 375,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 500};
void setup()
{
const int totalNotes = sizeof(notes) / sizeof(int);
// Loop through each note
for (int i = 0; i < totalNotes; i++)
{
const int currentNote = notes[i];
float wait = durations[i] / songSpeed;
// Play tone if currentNote is not 0 frequency, otherwise pause (noTone)
if (currentNote != 0)
{
tone(buzzer, notes[i], wait); // tone(pin, frequency, duration)
}
else
{
noTone(buzzer);
}
// delay is used to wait for tone to finish playing before moving to next loop
delay(wait);
}
}
void loop()
{
// Nothing in loop. Music only plays once.
// You can click reset on Arduino to replay the song.
}