Arduino library to play RTTTL melodies / ringtones non blocking from FLASH or RAM.
GPL-3.0 License
Improved Arduino library version of the RTTTL.pde example code written by Brett Hagman http://www.roguerobotics.com/ [email protected]
Available as Arduino library "PlayRtttl".
Available as Arduino library "PlayRtttl"
Arduino tone()
function and thus Timer 2 on Arduino Uno, Nano etc.
updatePlayRtttl()
must be repeatedly called to proceed in melody.
YouTube video of the RandomMelody example in action.
WOKWI online simulation of the RandomMelody example. .
#include <PlayRtttl.h>
const int TONE_PIN = 11;
...
playRtttlBlockingPGM(TONE_PIN, Bond);
...
...
startPlayRtttlPGM(TONE_PIN, TakeOnMe);
while (updatePlayRtttl()) {
// your own code here...
delay(1);
}
...
<NameString>:<Option>:(<Option>:)<Note>,<Note>...
Option:
Note:
Example: "Short:d=4,o=3,b=240,s=4:c4,8g,8g,a,g.,b,c4"
To customize the library to different requirements, there are some compile options / macros available.
These macros must be defined in your program before the line #include <PlayRtttl.hpp>
to take effect.
Modify them by enabling / disabling them, or change the values if applicable.
Name | Default value | Description |
---|---|---|
USE_NO_RTX_EXTENSIONS |
disabled | Disables interpretation of RTX format definitions 's' (style) and 'l' (loop).Even with USE_NO_RTX_EXTENSIONS activated, the default style is natural (Tone length = note length - 1/16).Saves up to 332 bytes program memory. |
RTX_STYLE_DEFAULT |
'N' | (Natural) Tone length = note length - 1/16. |
First, use Sketch > Show Sketch Folder (Ctrl+K).
If you have not yet saved the example as your own sketch, then you are instantly in the right library folder.
Otherwise you have to navigate to the parallel libraries
folder and select the library you want to access.
In both cases the library source and include files are located in the libraries src
directory.
The modification must be renewed for each new library version!
If you are using PlatformIO, you can define the macros in the platformio.ini file with build_flags = -D MACRO_NAME
or build_flags = -D MACRO_NAME=macroValue
.
If you are using Sloeber as your IDE, you can easily define global symbols with Properties > Arduino > CompileOptions.
If running with 1 MHz, e.g on an ATtiny, the millis() interrupt needs so much time, that it disturbes the tone() generation by interrupt. You can avoid this by using a tone pin, which is directly supported by hardware. Look at the appropriate pins_arduino.h, find digital_pin_to_timer_PGM[]
and choose pins with TIMER1x entries.
More RTTTL songs can be found under http://www.picaxe.com/RTTTL-Ringtones-for-Tune-Command/ or ask Google. C array of songs on GitHub
In order to fit the examples to the 8K flash of ATtiny85 and ATtiny88, the Arduino library ATtinySerialOut is required for this CPU's.
RTTTLMelodiesSmall
sample array with less entries.OneMelody
example.playRandomRtttlBlocking()
+ startPlayRandomRtttlFromArrayPGM()
do not print name now. If needed, use new functions playRandomRtttlSampleBlockingAndPrintName()
+ startPlayRandomRtttlFromArrayPGMAndPrintName()
.playRandomRtttlBlocking()
renamed to playRandomRtttlSampleBlocking()
and bug fixing.setNumberOfLoops()
and setDefaultStyle()
functions.Initial Arduino library version
The library examples are tested with GitHub Actions for the following boards: