/arduino_rtttl_parser

A parser for RTTTL (Nokia) melodies for Arduino (uses protothreading)

Primary LanguageC++GNU General Public License v3.0GPL-3.0

Arduino RTTTL parser

A parser for RTTTL (Nokia) melodies for Arduino (uses protothreading). Useful for game making, robotics with melody background ...

Installation

Install this library to your sketchbook/libraries directory. First make sure that you have installed LedControl library before installing this one (it is needed for some examples).

Linux :

sudo apt-get install arduino
sudo apt-get install python-pip
sudo pip install confduino
sudo python -m confduino.libinstall https://github.com/netmonster/arduino_rtttl_parser/archive/master.zip

Windows

Download this repository as a zip file
Open arduino application.
Go to menue Sketch > Include library > Add Zip Library
choose the zip file, and that's it !!
Restart the arduino application

Usage

The use if straight forward. First include the RTTTL.h header file.

#include <RTTTL.h>

Then specify the buzzer pin id and instantiate the RTTTL object :

// Buzzer output pin
const int pinBuzzer = 8;
// Instantiate the RTTTL object
RTTTL rtttl = RTTTL(pinBuzzer);

Create a null terminated string containing the RTTTL code to be parsed. For example :

// Mario melody in RTTTL format
char mario[]="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";

Now you have to tell the RTTTL object that you want to play the mario melody. This can be done at startup in setup function or at any time. This function does not play the song, it only parses the song header and prepares for playing the song. It is non blocking:

// Start playing Mario melody
rtttl.playMelody(mario);

In your loop function, call the tick() method to play the song. This function is non blocking, it only needs to be called regularly (that's why it is put in the loop function). Most of the time, this function returns without doing any thing. It actually check if it is time to parse the next note. If it is the case, it parses the note and starts playing.

New starting from V 1.1.0 : Starting from V 1.1.0, you can call a blocking version of playMelody as follow :

// Start playing Mario melody
// mario : melody string (char*)
// false : No loop (if you set it to true, this will loop forever)
// true : is_blocking, tells wather you want the playing to start immediately and block the execution
rtttl.playMelody(mario, false, true);

Information about the currently played note can be found in the member variable m_currentNote of the rtttl object.

  // Call the rtttl parser to update playing status
  rtttl.tick();
  // Recover last parsing note information
  RTTTL_NoteInfos* note = &rtttl.m_currentNote;

The RTTTL_NoteInfos object if a structure that gives information about the current note and the parsing status :

  int duration; // The duration of the note
  int note;       // The note (0 to 11 for regular notes C C# D D# E F F# G G# A A# B) and 12 for P (silence)
  int octave;    // The note octave 4 to 7
  int is_dotted;// Tells weather a half time should be added to the duration
  int info;         // Information about the current status of the parsing

The info field gives information about the status of the note parsingf :

#define RTTTL_INFO_NONE            (0)	// No specific information
#define RTTTL_INFO_PARSING_ERR     (1) // Parsing error occured
#define RTTTL_INFO_PAUSED          (2)     // The melody is paused by user
#define RTTTL_INFO_EOM             (3)	// End Of Melody (The melody is ended)
#define RTTTL_INFO_INCOMPLETE_SEQ  (4) // An incomplete sequence was detected

It is possible to pause the execution of the song at anytime by calling the pauseMelody method of the RTTTL object :

rtttl.pauseMelody();

Call this function again to resume playing.

You can also change the melody or rewind the song by calling the method playMelody();

Examples

Melody player : Illustrates how the Library can be used in a protothreading context. This plays several mélodies that can be selected using remote control via infrared communication, while playing leds to show the note.

Melody playerV2 : Illustrates how the Library can be used in a protothreading context. This plays several mélodies that can be selected using remote control via infrared communication, while showing line bars on the led matrix.

Warning !
Since the Library uses the Tone function, the Timer 2 use may be conflicted between the IRemote Library and the Tone Library. So, you can for example switch to using the Timer 1 instead by modifying the IRemote Library file boarddefs.h

![BreakOut game using arduino nano and RTTTL arduino library]

![Using RTTTL library for BreakOut game developpement]

Status

Beta version is ready.