LISTING 1
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
// SPI library (comes with Arduino)
// code.google.com/p/sdfatlib
// Part of SdFat
// See text
SFEMP3Shield MP3player;
// MP3 player object
char trackName[] = "track000.mp3";
// Char array for filename
void setup() {
Serial.begin(9600);
// Serial Monitor for debugging
MP3player.begin();
MP3player.SetVolume(10, 10);
// Start shield
// Set volume to about 75%
for(int i = 0; i<10; i++) {
sprintf(trackName, "track%03d.mp3", i);
MP3player.playMP3(trackName);
Serial.println(trackName);
while(MP3player.isPlaying());
}
}
// Iterate files 000 to 009
// Format: track###.mp3
// Play track
// Show track name in Serial Monitor
// Wait until track is done
void loop() {
// Empty loop
}
standpoint constant
bit rates are easier
to work with. Use a
constant bit rate
whenever you can.
The audio clips for
this project — see
Sources for where
to find samples you
can use — were
encoded at 160
kbps constant bit
rate. Feel free to
experiment with
other variations.
Table 1
compares several
commonly used
MP3 bit rates.
Compression ratio,
file sizes, and
quality comparisons
are approximate.
Making MP3
Audio Clips
You can make your own MP3
clips for your robot projects, either
by recording them using a
microphone or musical instrument,
or by converting an already existing
audio file. There are numerous free
and paid software for creating MP3
files. Two favorites are Audacity and
FIGURE 3. Audacity is a popular open
source software for recording,
editing, and exporting sound files,
including MP3 (requires a separate
download of the LAME MP3 plug-in).
Using Arduino Libraries
The sketches in this article require the use of several
third-party libraries that are not included with the Arduino
IDE software. The libraries are available for download from the
locations listed below, but for your convenience they are also
provided as part of the downloads available on the SERVO
website; refer to the link at the start of this article.
The libraries must be moved to your Arduino sketchbook
libraries folder. On Windows, this is found at
Documents\Arduino\libraries (or My
Documents\Arduino\libraries when using Windows XP). If
your sketchbook doesn't already have a libraries folder, you'll
need to create one first.
The extra libraries are:
SdFat -- code.google.com/p/sdfatlib. Reads the MP3
files on the micro-SD card attached to the SparkFun MP3
player shield.
40 SERVO 05.2012
SFEMP3Shield -- github.com/madsci1016/Sparkfun-MP3-
Player-Shield-Arduino-Library. Plays and manages MP3 clips.
This library, by electrical engineer Bill Porter, makes using MP3
on the Arduino much easier.
IRremote -- arcfn.com/2009/08/multi-protocol-infrared-
remote-library.html. Handy library written by Ken Shirriff
decodes infrared signals from an ordinary universal remote
control. Note: As of this writing, the version of the IRremote
library on Ken's site is not Arduino 1.0-ready. Use the version
included with the sketches for this article.
Important! The Getting Started guide provided by
SparkFun for the MP3 player shield talks about modifying the
SdFat library to alter the Slave Select (SS) pin used by the
Arduino to activate the micro-SD card. This step is not required
as long as you use a current version of the SdFat library and
the sketches provided with this article. The Slave Select pin is
now an optional parameter when setting up the SdFat object.