Skip to content

Instantly share code, notes, and snippets.

@mpflaga
Last active August 23, 2016 21:25
Show Gist options
  • Save mpflaga/5800889 to your computer and use it in GitHub Desktop.
Save mpflaga/5800889 to your computer and use it in GitHub Desktop.
Simple example of playing random tracks.
// libraries
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
#define NUMBER_OF_TRACKS 5
// initialize MP3 card
SdFat sd;
SFEMP3Shield MP3player;
int lastTrack;
// setup
void setup() {
if(!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt();
if (!sd.chdir("/")) sd.errorHalt("sd.chdir");
MP3player.begin(); // start mp3 player shield
MP3player.setVolume(10, 10);
randomSeed (millis()); // set a more random seed for the random function
// Note the seed is after the MP3 init, and the SdCard is asynchronous
}
// loop
void loop() {
int randNumber;
randNumber = lastTrack; // just to get into the next loop
while(randNumber == lastTrack) // keep choosing if it was repeat
{
randNumber = random(NUMBER_OF_TRACKS);
}
MP3player.playTrack(randNumber); // range of tracks to randomly play.
lastTrack = randNumber;
while (MP3player.isPlaying());
MP3player.stopTrack();// stop track
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment