Getting an RTTTL library

There is no built-in Arduino function that plays an RTTTL-formatted tune. You can either
write your own set of functions to parse an RTTTL string, or you can simply download and
install a library from the web that does the same thing. The latter approach has the
benefit that the library has probably already been debugged.

To install a library from the web: in the Arduino IDE, open the Sketch menu, then
follow the links to

Include Library -> Manage Libraries

This brings up a separate window in which you can select libraries to install or
upgrade. In the search box, enter: RTTTL. You will likely find more than one
library available. Pick whichever one suits you, then click the link More Info,
and then click the Install button on the right.

The following code uses the NonBlockingRtttl library, which allows you to play
a tune without interfering with other operations or activities on the Arduino.
For more information on nonblocking operations, see the skills packet on
blocking and delays.

A Simple sketch comparing a pitch/frequency format to the RTTTL format

The following sketch plays one of the Super Mario Brothers theme songs using the
pitch/frequency format. It uses the NonBlockingRtttl library.

It first plays the tune using the simple pitches/tempo format and calls to
the tone() function. Then it plays the same tune in its RTTTL format.
See if you can tell the difference!

/* Arduino Mario Bros Tunes with Piezo and PWM
 * Adapted from http://www.linuxcircle.com/2013/03/31/playing-mario-bros-tune-with-arduino-and-piezo-buzzer/
 * by: Dipto Pratyaksa
 * Includes 3 versions of the theme song:
 * 1:  a pair of arrays of pitch ("pitches") and inverse duration ("tempo").
 * 2:  an RTTTL version made by computing the equivalence between the pitches/tempo arrays 
 *     and their RTTTL equivalents.
 * 3:  an RTTTL version downloaded from the web.
*/

#include <NonBlockingRtttl.h>

#define BUZZER   6
#define DEBUG

//Mario main theme pitches
int pitches[] = {
  NOTE_E6, NOTE_E6, 0, NOTE_E6,
  0, NOTE_C6, NOTE_E6, 0,
  NOTE_G6, 0, 0,  0,
  NOTE_G5, 0, 0, 0,

  NOTE_C6, 0, 0, NOTE_G5,
  0, 0, NOTE_E5, 0,
  0, NOTE_A5, 0, NOTE_B5,
  0, NOTE_AS5, NOTE_A5, 0,

  NOTE_G5, NOTE_E6, NOTE_G6,
  NOTE_A6, 0, NOTE_F6, NOTE_G6,
  0, NOTE_E6, 0, NOTE_C6,
  NOTE_D6, NOTE_B5, 0, 0,

  NOTE_C6, 0, 0, NOTE_G5,
  0, 0, NOTE_E5, 0,
  0, NOTE_A5, 0, NOTE_B5,
  0, NOTE_AS5, NOTE_A5, 0,

  NOTE_G5, NOTE_E6, NOTE_G6,
  NOTE_A6, 0, NOTE_F6, NOTE_G6,
  0, NOTE_E6, 0, NOTE_C6,
  NOTE_D6, NOTE_B5, 0, 0
};
//Mario main them tempo
int tempo[] = {
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,

  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
};

const char * mymario = "smb:d=4,o=5,b=180:"
                       "16e6,16e6,16p,16e6,"
                       "16p,16c6,16e6,16p,"
                       "16g6,16p,16p,16p,"
                       "16g5,16p,16p,16p,"
                       "16c6,16p,16p,16g5,"
                       "16p,16p,16e5,16p,"
                       "16p,16a5,16p,16b5,"
                       "16p,16a#5,16a5,16p,"
                       "16g5,32g5,16e6,32e6,16g6,32g6,"
                       "16a6,16p,16f6,16g6,"
                       "16p,16e6,16p,16c6,"
                       "16d6,16b5,16p,16p,"
                       "16c6,16p,16p,16g5,"
                       "16p,16p,16e5,16p,"
                       "16p,16a5,16p,16b5,"
                       "16p,16a#5,16a5,16p,"
                       "16g5.,16e6.,16g6.,"
                       "16a6,16p,16f6,16g6,"
                       "16p,16e6,16p,16c6,"
                       "16d6,16b5,16p,16p";

String dummy = "";
bool pitchesMode = true;

void setup(void) {
    pinMode(BUZZER, OUTPUT);         
    pinMode(LED_BUILTIN, OUTPUT);   //led indicator when singing a note
    digitalWrite(LED_BUILTIN, LOW);

    Serial.begin(57600);
    while (!Serial) {};
}

void loop() {
    if (pitchesMode) {
        int size = sizeof(pitches) / sizeof(int);
        for (int i = 0; i < size; i++) {
            int noteDuration = 1000 / tempo[i];
            tone(BUZZER, pitches[i], noteDuration);
            // To distinguish the notes, set a minimum time between them;
            // the note's duration + 30% seems to work well:
            myDelay(1.3*noteDuration);
        }
        myDelay(1000);
        pitchesMode = false;
    }

    if (!rtttl::isPlaying()) {
        rtttl::begin(BUZZER, mymario);
        digitalWrite(LED_BUILTIN, HIGH);
        while (rtttl::isPlaying()) { rtttl::play(); }
        rtttl::stop();
        myDelay(1000);
        digitalWrite(LED_BUILTIN, LOW);
        pitchesMode = true; 
    }
}

void myDelay(int dt) {
    unsigned long startTime = millis();
    while ((millis() - startTime) < dt) {}
}

A Tiny JukeBox

The following sketch incorporates several ringtones. However, since each ringtone is copied into
the Arduino’s tiny SRAM (which is only 2KBytes) when the program runs, only a few melodies can be
included. The user can choose which ringtone to play by opening the Serial Console.

Notice that the baud rate for the Serial Console was set at a very low value (only 1200 baud)! This
Jis so that a user could interact with this jukebox using any cell phone with an “On-the-Go” cable”
and an app that reads from the USB and sends its output to the phone’s own Serial Console.
(Just search for something like “serial usb” in your app store.) This low baud rate is necessary
because, even though the processors in cell phones are very fast, they are busy managing many tasks
at once, so they sometimes miss data sent over a USB->Serial connection.

/* A JukeBox continaing a number of tunes encoded in RTTTL format 
   All of the tunes in this sketch were downloaded from 
   http://arcadetones.emuunlim.com/nes/
   Limited RAM prevents using more than 3 or 4 tunes at once, so this
   jukebox is a pretty small box.  The project really needs the help
   of an add-on SD card.
*/

#include <NonBlockingRtttl.h>

#define SPEAKER 6

const int ntunes = 3;

String titles[ntunes] = {"Battle Of Olympus",
                   "Bomberman",
                   "Star Wars Imperial March"};

/* More Strings 
                   "Castlevania",
                   "Castlevania 2",
                   "Castlevania 3",
                   "Double Dragon",
                   "Dr. Mario",
                   "Duck Tales",
                   "Excite Bike",
                   "Faxanadu",
                   "Golgo 13",
                   "Gunsmoke",
                   "Gyromite",
                   "Ice Climber",
                   "Maniac Mansion",
                   "MegaMan",
                   "MegaMan 2",
                   "MegaMan 4",
                   "Metroid",
                   "Metal Gear",
                   "MegaMan 3",
                   "Ninja Gaiden",
                   "Nintendo Tetris",
                   "Snake Rattle & Roll",
                   "Super Dodge Ball",
                   "Super Mario Brothers",
                   "Super Mario Brothers 2",
                   "Super Mario Brothers 3",
                   "Wrecking Crew",
                   "Wizards &amp; Warriors",
                   "Wrestlemania",
                   "Legend Of Zelda",
                   "Zelda 2"};
*/

const char* tunes[ntunes] = 
{"batolymp:d=4,o=5,b=100:32a4,32b4,32c,32d,32e,32f,32f,32g,32a,32b,32c6,8e.6,16e6,16e6,16e6,16e.6,16d.6,16c.6,16d.6,8d#.6,16d#6,16d#6,16d#6,16d#.6,16g#.6,16d#.6,16g#.6,8g.6,16g6,16g6,16g6,16g.6,16f.6,16g.6,16f.6,4e.6,8b6", 
"bomberman:d=4,o=5,b=125:16b4,16b4,16b,16b4,16d,16p,16f#,16g#,16a,16p,16a,16p,8g#,8p,16a4,16a4,16a,16a4,16c#,16p,16e,16f#,16g,16p,16f#,16g,16a4,16g#4,8a4,16f#4,16f#4,16f#,16f#4,16e,16p,16d#,16e,16f#4,16p,16f#,16p,8f#.4,16p,16f#4,16f#4,16f#,16f#4,16e,16p,16d#,16e,16f#4,16p,16f#,16p,16f#4,16f#4,16g#4,16a#4",
"starwars:d=4,o=5,b=112:8d.,16p,8d.,16p,8d.,16p,8a#4,16p,16f,8d.,16p,8a#4,16p,16f,d.,8p,8a.,16p,8a.,16p,8a.,16p,8a#,16p,16f,8c#.,16p,8a#4,16p,16f,d.,8p,8d.6,16p,8d,16p,16d,8d6,8p,8c#6,16p,16c6,16b,16a#,8b,8p,16d#,16p,8g#,8p,8g,16p,16f#,16f,16e,8f,8p,16a#4,16p,8c#,8p,8a#4,16p,16c#,8f.,16p,8d,16p,16f,a.,8p,8d.6,16p,8d,16p,16d,8d6,8p,8c#6,16p,16c6,16b,16a#,8b,8p,16d#,16p,8g#,8p,8g,16p,16f#,16f,16e,8f,8p,16a#4,16p,8c#"
};

/* More tunes 
"cv_vk:d=4,o=5,b=100:8d5,32P,8a5,32P,32g#5,32P,32a5,32P,32g#5,32P,8f5,8P,32f6,32P,32d6,8P,32f5,32P,32d5,32P,32f6,8P,32f6,8P,32f6,8P,8d5,32P,8a5,32P,32g#5,32P,32a5,32P,32g#5,32P,32f5,4P,32f6,32P,32d6,8P,32f5,32P,32d5,32P,32f7,32P,32c7,32P,32g#6,32P,32f6,32P,32f5,32P,32d6,32P,32f6,32P,32g#6,32P,8c#5,32P,8e5,32P,16a#5,32P,8a5,32P,8f5,32P,16d5,32P,8c#5,32P,8e5,32P,16a#5,32P,16a5,32P,32d5,4P,8c#5,32P,8e5,32P,16a#5,32P,8a5,32P,8f5,32P,16d5,32P,8e5,32P,8g5,32P,16a#5,32P,8a5,32P,8b5,32P,16c#6,32P,32d6,32P,32d6,32P,32d5,32P,4d5",
"btears:d=4,o=5,b=125:16a#,16f,16f6,16f,16d#6,16f,16c#6,16f,16c6,16f,16c#6,16f,16c6,16f,16a#,16f,16c6,16f,16c#6,16f,16d#6,16f,16c#6,16f,16c6,16f,16g#,16f,16c6,16f,16a#,16f,16a#,16f,16f6,16f,16d#6,16f,16c#6,16f,16c6,16f,16c#6,16f,16c6,16f,16a#,16f,16c6,16f,16c#6,16f,16d#6,16f,16c#6,16f,16c6,16f,16g#,16f,16c6,16f,16a#,16f,8d#,16g#,2f,8d#,8c#,8d#.,8g#.,f.,8d#,8c#,8d#,16g#,2f,8d#,8f,8f#.,g#.,8f.,f#",
"cv3s1:d=4,o=5,b=112:32f#6,32p,32f6,32p,32d#6,32p,a#,16p,32a#,32p,32d#6,32p,32f6,32p,32f#6,32p,32f6,32p,32d#6,32p,32a#6,32p,8a6,32p,8f6,32p,16a6,32p,8g#6,32p,8e6,32p,16g#6,32p,32f#6,32p,32f6,32p,32d#6,32p,a#,16p,32a#,32p,32d#6,32p,32f6,32p,32f#6,32p,32f6,32p,32d#6,32p,32a#6,32p,8a6,32p,8f6,32p,16d#7,32p,32d7,32p,32d#7,32p,32f7,32p,a#6",
"nesddragon:d=4,o=5,b=63:16c6,16b,32p,16c6,16b,32p,16c6,32p,16c6,16b,8d6,32p,8d6,32p,16c6,16b,32p,16c6,16b,32p,16c6,32p,16c6,16b,8d6,32p,8e6,32p,16c6,16b,32p,16c6,16b,32p,16c6,32p,16c6,16b,8d6,32p,8d6,32p,16c6,16b,32p,16c6,16b,32p,16d6,32p,16e6",
"dmario:d=4,o=5,b=140:8c6,8p,8c6,8p,8c6,2p,8c#.6,16p,8c#6,8d#6,8p,8d#6,8p,8c6,8p,8c#6,8p,8c6,2p,8c#.6,16p,8c#6,8d#6,8p,8d#6,8p,8c6,8p,8c6,8p,8g#,2p,8c#.6,16p,8c#6,8d#6,8p,8d#6,8p,8c6,8p,8c#6,8p,8c6,2p,8d#6,8c#6,8p,8c6,8p,8a#",
"ducktales:d=4,o=5,b=112:8e6,8e6,16p,16g6,8b6,g#6,p,8e6,8d6,8c6,8d6,8e6,8d6,8c6,8d6,8e6,8e6,16p,16g6,8b6,g#6,p,8e6,8d6,8c6,8d6,8e6,8d6,8c6,8g6,8e6,8e6",
"exbike:d=4,o=5,b=112:8e,8g,8c6,16e,8g,4c.6,16p,8g#,8b,8e6,16g#,8b,4e.6,16p,16d.6,32c6,16d.6,32c6,16a.,32g,8f,16f.,32e,16d#.,8d.,16c,8e,8f,8f#,16g,4c6",
"faxanadu:d=4,o=5,b=70:16a4,16p,16a,16a4,16d,16e,16p,16e4,16g4,16a4,16a,16a4,16d,16e,16p,16e4,16a4,16p,16a,16a4,16d,16e,16e4,16g4,16a4,16b4,16c,16d,16e,16b4,16e4,16a4,16p,16a,16e,16e4,16e,16p,16e4,16a4,16g,16a,16e,16p,16c,16a4,16e4,16g4,16g,16d,16d4,16d,16p,16d4,16g4,16f,16g,16d",
"golgo13:d=4,o=5,b=125:8g#4,16b4,8c#,8g#,2g,16p,g#.4,b.4,d#,a#.4,f#.4,d#4,g#.4,b.4,8d#,8g#4,8f#,1e,16f#,32e,32d#,2c#,8f#4,8a#4,d#.,2b4,8p,16b.4,16b,16g#.4,16g#.,16e4,16e.,16b.4,16b,16g#.4,16g#.,16e4,16e.,16d#.4,16d#,16g.4,16g.,16a#4,16a#.,16c#.,16c#6,16b.4,16b.,16a#4,16a#.,g#.4,b.4,d#,a#.4,f#.4,d#4,g#.4,b.4,8d#,8g#4,8f#,1e,16f#,32e,32d#,2c#,8f#4,8a#4,d#.,2b4,8p,16b.4,16b,16g#.4,16g#.,16e4,16e.,16b.4,16b,16g#.4,16g#.,16e4,16e.,16d#.4,16d#,16g.4,16g.,16a#4,16a#.,16c#.,16c#6,16b.4,16b.,16a#4,16a#.,g#4",
"gunsmoke:d=4,o=5,b=63:16b,e6,32p,16e6,16f#6,16g6,16f#.6,32d6,b,32p,16b,e6,32p,16e6,16f#6,16g6,16f#.6,32d6,b6,32p,16g6,c7,32p,16c7,16d7,16e7,16d.7,32b6,8g.6,32p,16g6,16b6,16d7,16c.7,32a6,16f6,16c7,32b6,32g6,16e6,32a6,32f#6,16d6,e.6",
"gyromite:d=4,o=5,b=140:16g,16c6,16g,16c6,16g,16c6,16g,16c6,16a,16c6,16a,16c6,16a,16c6,16a,16c6,16a,16f,16a,16f,16c6,16a,16c6,16a,16e6,16b,16e6,16b,16d6,16a,16d6,16a,16g,16c6,16g,16c6,16g,16c6,16g,16c6,16a,16c6,16a,16c6,16a,16c6,16a,16c6,16a,16f,16a,16f,16c6,16a,16c6,16a,16e6,16b,16e6,16b,16d6,16a,16d6,16a",
"iceclimr:d=4,o=5,b=63:16g4,32e4,32f4,32p,32g4,16p,32c,32b4,32c,32b4,32c,16g4,32e4,32f4,32p,32g4,16p,32c,32b4,32c,32b4,32c,16a4,32f4,32g4,32p,32a4,16p,32d,32c#,32d,32c#,32d,16a4,32f4,32g4,32p,32a4,16p,32d,32c#,32d,32c#,32d,16b4,32g4,32a4,32p,32b4,16p,32f,32e,32f,32e,32f,16b4,32g4,32a4,32p,32b4,16p,32f,32e,32f,32e,32f,32e,32c,32g4,32e,32c,32g4,32f,32d,32a4,32f,32d,32a4,32f#,32d,32a4,32f#,32d,32a4",
"maniac:d=4,o=5,b=112:8f#4,16f#4,16f#4,8f#,16f#4,8f#4,16f#,16f#4,16f#4,8f#,8f#4,8g#4,16g#4,16g#4,8g#,16g#4,8g#4,16g#,16g#4,16g#4,8g#,8g#4,8a4,16a4,16a4,8a,16a4,8a4,16a,16a4,16a4,8a,8a4,8b4,16b4,16b4,8b,16b4,8b4,16b,8b4,16f#,16b4,8a4,8f#4,16f#4,16f#4,8c#,16f#4,8f#4,16c#,16d#4,16d#4,8e4,8f4,8f#4,16f#4,16f#4,8c#,16f#4,8f#4,16c#,16d#4,16d#4,8e4,8f4,8f#4,16f#4,16f#4,8c#,16f#4,8f#4,16c#,16d#4,16d#4,8e4,8f4,8f#4",
"chosen:d=4,o=5,b=160:16g,16p,16g,8f,2g,8p,16a,16p,16a,8g,2a,16a#,16a#,16p,16a#,p,16c#6,16c#6,16p,16c#6,p,16d6,16p,8c6,16a#,16c6,16c#6,32d6,32d#6,32d6,32d#6,32d6,32d#6,32d6,32d#6,16d6",
"megaman2:d=4,o=5,b=125:a#,c#6,g#6,8f6,f#6,8a#,8d#6,8f#6,8f6,8d#6,8c#6,d#6,8c6,8a#,8g#,8f#,8f#,8f,8f#,1c#6,d#.6,b.,f#,f.6,c#.6,g#,f.6,d.6,a#,f#.6,f.6,d#6,1f#6",
"mm4stage:d=4,o=5,b=140:f.,16a#,16c#6,f.6,16d#6,16c#6,d#.6,16c#6,16c6,16c#6,16c6,a#,16a#,16c6,c#.6,16c6,16c#6,c6,g#,1f,f.,16a#,16c#6,f.6,16d#6,16c#6,d#.6,16c#6,16c6,16c#6,16c6,a#,16a#,16c6,c#.6,16c6,16c#6,c6,g#,1a#",
"kraidbase:d=4,o=5,b=160:e,32p,8b4,32p,a,32p,8c,32p,f#,32p,8d,32p,b4,32p,8a#4,32p,e,32p,8b4,32p,a,32p,8c,32p,f#,32p,8d,32p,b4,32p,8a#4,32p,e,32p,8b4,32p,a,32p,8c,32p,f#,32p,8d,32p,b4,32p,8a#4,32p,e,32p,8b4,32p,a,32p,8c,32p,f#,32p,8d,32p,b4,32p,8a#4,32p",
"mgear:d=4,o=5,b=125:8d,16p,16d,2g4,8d,16p,16e,8c#,16p,16c#,a4,8e.,16p,8d,16p,16c#,c,d#4,8d#,16p,16d#,c,1a#4,8g4,16p,16g4,d.,16d,16a#.4,32p,32a#4,32p,32a#4,32p,16c#.,32p,32c#,32p,16c#,a4,e,16d.,32p,32c#,32p,32c#,32p,c,8d#4,32p,32g#4,16p,16d#,32f,32p,32f,32p,32f,32p,16c,16p,16g#.4,1d",
"mm3_magnet:d=4,o=5,b=125:8c#6,8p,8c#6,8p,8e6,8p,8e6,8f#6,8p,16b,16a#,16b,16c#6,8p,16b,16a#,16b,16c#6,p,8p,16b,16a#,16b,16c#6,8p,16b,16a#,16b,16c#6,p,8a#.,8b.,2c#6,8f#6,8a6,8f#6,16e6,16f#6,16p,16f#6,2f#6,a#,8f.6,2f#6,16a#,16d6,16p,16c#6,16p,16c#6,16b,16p,16b,2b,g#6,8f#.6,2f6,16b,16a#,16p,16a#,16p,16a#,16b,16p,2c#6,16p,d.6,16d6,16e6,8f#.6,8e.6,8d6,8c#.6,8a#.,2f#",
"gaiden:d=4,o=5,b=125:32f#,32p,32f#,16p,32a,32p,32b,32p,32a,32p,32b,16p,32f#,16p,32a,32p,32b,8p,32f#,16p,32f#,16p,32b,32p,32c#6,32p,32b,32p,32c#6,16p,32f#,16p,32f#,32p,32a#,8p,32a#,16p,32g,16p,32c#6,32p,32a#,32p,32g,32p,32c#6,16p,32g,16p,32g,32p,32c#6,8p,32c#6,16p,32a,16p,32d6,32p,32a,32p,32f#,32p,32a,16p,32a,16p,32a,32p,32d6,32p,32c#6,32p,32b,32p,32a,32p,32f#",
"ntetris:d=4,o=5,b=112:8g7,8e7,g7,f#7,d#7,e7,8d7,8d7,d7,8c#7,8c#7,c#7,8c7,8c7,c7,8b6,8e7,8c7,8e7,b.6,p,8g7,8e7,g7,f#7,d#7,e7,8g7,8g7,g7,8f#7,8f#7,f#7,8e7,8e7,e7,8d#7,8f#7,8e7,8f#7,d#7",
"rattle:d=4,o=5,b=80:8f,8d,16f,16d,16g,8f,8c,p,8b,8g,16b,16g,16d6,8b,8a,p,8f,8d,16f,16d,16g,8f,8c,8p,16g,16g,16a,8b,16c6,16b,16a,8b,8b,16d6,8c6",
"sdodge:d=4,o=5,b=140:8d#.6,8c#.6,d#6,8a#6,8g#6,8f#6,8f6,16f#6,16f6,16c#6,16p,d#6,8d#6,8f6,8f#6,16d#6,8f#6,16d#6,8f#6,8g#6,16f6,8g#6,16f6,16g#6,16b6,16a#6,p,16b6,16b6,16a#6",
"smb:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6",
"smario2:d=4,o=5,b=125:8g,16c,8e,8g.,16c,8e,16g,16c,16e,16g,8b,a,8p,16c,8g,16c,8e,8g.,16c,8e,16g,16c#,16e,16g,8b,a,8p,16b,8c6,16b,8c6,8a.,16c6,8b,16a,8g,16f#,8g,8e.,16c,8d,16e,8f,16e,8f,8b.4,16e,8d.,c",
"smb3lvl1:d=4,o=5,b=80:16g,32c,16g.,16a,32c,16a.,16b,32c,16b,16a.,32g#,16a.,16g,32c,16g.,16a,32c,16a,4b.,32p,16c6,32f,16c.6,16d6,32f,16d.6,16e6,32f,16e6,16d.6,32c#6,16d.6,16c6,32f,16c.6,16d6,32f,16d6,4e.6,32p,16g,32c,16g.,16a,32c,16a.,16b,32c,16b,16a.,32g#,16a.,16c6,8c.6,32p,16c6,4c.6",
"wrecking:d=4,o=5,b=125:16e,16p,16g,8p,16c,8p,16c,8p,16b4,16a4,p,16a4,16a,16p,16g,8p,16d,16e,16p,16g,p,16c,p,16a4,p,16g4,16g#4,16a4,16c,16d,16p,16c,16e,16p,16g,8p,16c,8p,16c,8p,16b4,16a4,p,16a4,16a,16p,16g,8p,16d,16e,16p,16g,p,16c,p,16a4",
"ww_title:d=4,o=5,b=112:16d6,16a,16e6,16a,16f6,16a,16g6,16a,16a#,16g,16d6,16g,16g6,16g,16f6,16g,16e6,16c6,16f6,16c6,16g6,16g,16c6,16g,16a#,16f,16a,16f,16f6,16f,16e6,16f,16d6,16a#,16e6,16a#,16f6,16a#,16d6,16a#,16d6,16g,16e6,16g,16f6,16g,16d6,16g,16e6,16a,16c#6,16a,16e6,16a,16c#6,16a,16a6,16a,16c#6,16a,16a6,16a,16a6,16a",
"nes_hogan:d=4,o=6,b=140:8a5,8b5,c,c,b5,b5,8a5,a5,p,8a5,8b5,c,c,b5,b5,8c,c,p,8a5,8b5,c,c,b5,b5,8a5,a5,8p,8a5,8b5,c,c,8p,8e.,d,8c,e",
"Zelda1:d=4,o=5,b=125:a#,f.,8a#,16a#,16c6,16d6,16d#6,2f6,8p,8f6,16f.6,16f#6,16g#.6,2a#.6,16a#.6,16g#6,16f#.6,8g#.6,16f#.6,2f6,f6,8d#6,16d#6,16f6,2f#6,8f6,8d#6,8c#6,16c#6,16d#6,2f6,8d#6,8c#6,8c6,16c6,16d6,2e6,g6,8f6,16f,16f,8f,16f,16f,8f,16f,16f,8f,8f,a#,f.,8a#,16a#,16c6,16d6,16d#6,2f6,8p,8f6,16f.6,16f#6,16g#.6,2a#.6,c#7,c7,2a6,f6,2f#.6,a#6,a6,2f6,f6,2f#.6,a#6,a6,2f6,d6,2d#.6,f#6,f6,2c#6,a#,c6,16d6,2e6,g6,8f6,16f,16f,8f,16f,16f,8f,16f,16f,8f,8f",
"Zelda2Intr:d=4,o=5,b=112:8c7,8b6,8g6,8d6,8c7,8b6,8g6,8d6,8c7,8b6,8g6,8d6,8c7,8b6,8g6,8d6,8c7,8b6,8g6,8d6,8c7,8b6,8g6,8d6,8c7,8b6,8g6,8d6,8c7,8b6,8g6,8d6,2d6,8p,b,8d6,2c.6,p,2a#,8p,g,8a#,2a.,p,2d6,8p,g6,8f6,2f.6,p,2d#6,8p,a#,8d#6,d.6,a.,f#,g.7,g7,g7,8g7,g#.7,g#7,g#7,8g#7,a.7,a7,a7,8a7,a#.7"};
*/

String dummy = "";

void setup() {
  Serial.begin(1200, SERIAL_8E1);
  while (!Serial) {}
  pinMode(LED_BUILTIN, OUTPUT);   //led indicator when singing a note
  pinMode(SPEAKER, OUTPUT);        //buzzer pin
}

void loop() {
    int selection = -1;

    while ((selection < 0) || (selection > ntunes)) {
        for (int i=0; i<ntunes; i++) {
            Serial.print(i+1);
            Serial.println(titles[i]);
            Serial.flush();
        }
        Serial.println("Which tune?");
        Serial.flush();
        while (!Serial.available()) {}
        selection = Serial.parseInt();
        Serial.print("You entered ");
        Serial.println(selection);
        Serial.flush();
    }

    if (!rtttl::isPlaying()) {
      Serial.print("Now playing ");
      Serial.println(titles[selection-1]);
      Serial.flush();
      rtttl::begin(SPEAKER, tunes[selection-1]);
      unsigned long start = millis();
      while (millis() - start < 5000) rtttl::play();
      rtttl::stop();
    }
    rtttl::play();

}