Home › Forums › Sparkfun MP3 Shield Library Support Forum › Question about Sparkfun's Liliypad mp3 › Reply To: Question about Sparkfun's Liliypad mp3
First post the web site did not post everything I typed hopefully this time it does.
I’m using a Sparkfun Lilypad mp3, the issue I’m running into right now is that the code should be set to randomly play 1 of 12 mp3 files when trigger 1 is pulled low. Right now it is randomly playing the files when ever it wants regardless of what is going on with trigger 1
Code:
// “Trigger” example sketch for Lilypad MP3 Player Modified
// 1.0 initial release MDG 2012/11/01
// We’ll need a few libraries to access all this hardware!
#include <SPI.h> // To talk to the SD card and MP3 chip
#include <SdFat.h> // SD card file system
#include <SFEMP3Shield.h> // MP3 decoder chip
// And a few outputs we’ll be using:
const int ROT_LEDR = 10; // Red LED in rotary encoder (optional)
const int EN_GPIO1 = A2; // Amp enable + MIDI/MP3 mode select
const int SD_CS = 9; // Chip Select for SD card
// Create library objects:
SFEMP3Shield MP3player;
SdFat sd;
// We’ll store the five filenames as arrays of characters.
// “Short” (8.3) filenames are used, followed by a null character.
char filename[12][13]; //This is for 12 sound files
int pirPin = A0; //the pin where we read the val of the motsen
int pirVal; //stores the value (hi or lo) for the motion sensor
unsigned long time1;
unsigned long time2;
void setup()
{
time1 = millis();
pinMode(pirPin, INPUT);
int x, index;
SdFile file;
byte result;
char tempfilename[13];
// The board uses a single I/O pin to select the
// mode the MP3 chip will start up in (MP3 or MIDI),
// and to enable/disable the amplifier chip:
pinMode(EN_GPIO1,OUTPUT);
digitalWrite(EN_GPIO1,LOW); // MP3 mode / amp off
// Initialize the SD card; SS = pin 9, half speed at first
sd.begin(SD_CS, SPI_HALF_SPEED); // 1 for success
// Start up the MP3 library
MP3player.begin(); // 0 or 6 for success
// Now we’ll access the SD card to look for any (audio) files
// starting with the characters ‘1’ to ‘5’:
// if (debugging) Serial.println(F(“reading root directory”));
// Start at the first file in root and step through all of them:
sd.chdir(“/”,true);
while (file.openNext(sd.vwd(),O_READ))
{
// get filename
file.getFilename(tempfilename);
// Does the filename start with char ‘1’ through ‘5’?
index = tempfilename[0] – ‘1’;
// Copy the data to our filename array.
strcpy(filename[index],tempfilename);
file.close();
}
// Set the VS1053 volume. 0 is loudest, 255 is lowest (off):
MP3player.setVolume(0,0);
// Turn on the amplifier chip:
digitalWrite(EN_GPIO1,HIGH);
delay(2);
}
void loop()
{
int t; // current trigger
static int last_t; // previous (playing) trigger
int x;
byte result;
time2 = millis();
if((time2 – time1)>=10000){
pirVal = digitalRead(pirPin);
time1 = time2;
}
t = 3;
if(pirVal == HIGH){
MP3player.playMP3(filename[random(0,11)]);
delay(5000);
}
}