Páginas

Wednesday, June 15, 2016

Music for All - Playing Melodies in Arduino

Some time ago Brett Hangman did a sample sketch to play melodies using de Arduino Tone Library.

The sketch plays RTTTL songs, this is convenient for micro processors, its simple string with all the information about the melody, tempo, duration and notes. So you don't have a mess with arrays and pointers and also there is thousands of RTTTL songs already made you can download for free, from tv show to popular pop songs.

To make this better we are going to use TimerFreeTone Library from Tim Eckel. This remove the need to use timer1 for tones. That way we can use servos and PWM control for Leds.



The original sketch has been converted to a Library and has been heavily rewritten to allow asynchronous play (asynchronous from led blinking and servo moving, etc..) so you could do more things while music plays.

The original sketch also was having memory size problems, long songs could crash by using all available RAM, now a Flash library by Mikal Hart is used to store all songs to PROGMEM, reducing the RAM used.

You can get the new RTTTL Library from here. The Library includes a demo to help you start, the connections are pretty straight forward, just connect a speaker or buzzer to pin 13 using a resistor in series (100-200omh or anything similar) and the other side to GND.




Here is de demo code:



No comments:

Post a Comment