SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By mauimaus
#197889
Hello (i am an absolute beginner)

I am trying to start up my Lilypad MP3 using 'Trigger'.
It didn't work (could hear the files) so I uploaded it again on the board.Upload is fine but I still can't hear the files (T1, T2)
I went through the whole read-me list: SD card (I reformatted in exFat), proper file names, battery, on switch, tried 2 different ear phones well-plugged in, circuite, etc.

I can't figure it out.

Thanks in advance for your help!

Code: Select all
// "Trigger" example sketch for Lilypad MP3 Player
// Mike Grusin, SparkFun Electronics
// http://www.sparkfun.com


#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



const int TRIG1 = A0;
const int TRIG2 = A4;
const int TRIG3 = A5;
const int TRIG4 = 1;
const int TRIG5 = 0;
int trigger[5] = {TRIG1,TRIG2,TRIG3,TRIG4,TRIG5};



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



SFEMP3Shield MP3player;
SdFat sd;

boolean debugging = false;


boolean interrupt = true;


boolean interruptself = false;



char filename[5][13];


void setup()
{
  int x, index;
  SdFile file;
  byte result;
  char tempfilename[13];


  for (x = 0; x <= 4; x++)
  {
    pinMode(trigger[x],INPUT);
    digitalWrite(trigger[x],HIGH);
  }

  
  pinMode(ROT_LEDR,OUTPUT);
  digitalWrite(ROT_LEDR,HIGH);  // HIGH = off

  pinMode(EN_GPIO1,OUTPUT);
  digitalWrite(EN_GPIO1,LOW);  // MP3 mode / amp off


  
  if (debugging)
  {
    Serial.begin(9600);
    Serial.println(F("Lilypad MP3 Player trigger sketch"));
  }
  

  if (debugging) Serial.print(F("initialize SD card... "));

  result = sd.begin(SD_CS, SPI_HALF_SPEED); // 1 for success
  
  if (result != 1) // Problem initializing the SD card
  {
    if (debugging) Serial.print(F("error, halting"));
    errorBlink(1); // Halt forever, blink LED if present.
  }
  else
    if (debugging) Serial.println(F("success!"));
  
  // Start up the MP3 library

  if (debugging) Serial.print(F("initialize MP3 chip... "));

  result = MP3player.begin(); // 0 or 6 for success

  // Check the result, see the library readme for error codes.

  if ((result != 0) && (result != 6)) // Problem starting up
  {
    if (debugging)
    {
      Serial.print(F("error code "));
      Serial.print(result);
      Serial.print(F(", halting."));
    }
    errorBlink(result); // Halt forever, blink red LED if present.
  }
  else
    if (debugging) Serial.println(F("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'?      

    if (tempfilename[0] >= '1' && tempfilename[0] <= '5')
    {
      // Yes! subtract char '1' to get an index of 0 through 4.

      index = tempfilename[0] - '1';
      
      // Copy the data to our filename array.

      strcpy(filename[index],tempfilename);
  
      if (debugging) // Print out file number and name
      {
        Serial.print(F("found a file with a leading "));
        Serial.print(index+1);
        Serial.print(F(": "));
        Serial.println(filename[index]);
      }
    }
    else
      if (debugging)
      {
        Serial.print(F("found a file w/o a leading number: "));
        Serial.println(tempfilename);
      }
      
    file.close();
  }

  if (debugging)
    Serial.println(F("done reading root directory"));
  
  if (debugging) // List all the files we saved:
  {
    for(x = 0; x <= 4; x++)
    {
      Serial.print(F("trigger "));
      Serial.print(x+1);
      Serial.print(F(": "));
      Serial.println(filename[x]);
    }
  }


  MP3player.setVolume(10,10);
  

  digitalWrite(EN_GPIO1,HIGH);
  delay(2);
}


void loop()
{
  int t;              // current trigger
  static int last_t;  // previous (playing) trigger
  int x;
  byte result;
  


  for(t = 1; t <= (debugging ? 3 : 5); t++)
  {

    if (digitalRead(trigger[t-1]) == LOW)
    {

      
      x = 0;
      while(x < 50)
      {
        if (digitalRead(trigger[t-1]) == HIGH)
          x++;
        else
          x = 0;
        delay(1);
      }
        
      if (debugging)
      {
        Serial.print(F("got trigger "));
        Serial.println(t);
      }


      if (filename[t-1][0] == 0)
      {
        if (debugging)
          Serial.println(F("no file with that number"));
      }
      else // We do have a filename for this trigger!
      {

        if (interrupt && MP3player.isPlaying() && ((t != last_t) || interruptself))
        {
          if (debugging)
            Serial.println(F("stopping playback"));

          MP3player.stopTrack();
        }

 

        result = MP3player.playMP3(filename[t-1]);

        if (result == 0) last_t = t;  // Save playing trigger
  
        if(debugging)
        {
          if(result != 0)
          {
            Serial.print(F("error "));
            Serial.print(result);
            Serial.print(F(" when trying to play track "));
          }
          else
          {
            Serial.print(F("playing "));
          }
          Serial.println(filename[t-1]);
        }
      }
    }
  }
}


void errorBlink(int blinks)
{


  int x;

  while(true) // Loop forever
  {
    for (x=0; x < blinks; x++) // Blink the given number of times
    {
      digitalWrite(ROT_LEDR,LOW); // Turn LED ON
      delay(250);
      digitalWrite(ROT_LEDR,HIGH); // Turn LED OFF
      delay(250);
    }
    delay(1500); // Longer pause between blink-groups
  }
}
By paulvha
#197893
couple of things:

The debug code does not make it easier to understand what you want to do. Keep it to the minimum / remove if you look for help.

Why in setup() do you do
pinMode(trigger[x],INPUT);
digitalWrite(trigger[x],HIGH);

If you want a pullup for your switches then do pinMode(trigger[x]. INPUT_PULLUP );

There was another post recently ( https://forum.sparkfun.com/viewtopic.php?f=42&t=47734) . He found out that he only needed to play track 1 MP3player.playTrack(1);

Maybe there is more that you can use for your sketch.

regards,
Paul
By mauimaus
#197911
Hello Paul

Thanks for your reply.
I didn't write the code or made any decisions yet: I only uploaded the code + librairies provided with this Lilypad MP3 on the Sparkfun website. I followed the instructions to test the audio output with the headphones (which is supposed to be plug and play).
Therefore I didn't remove any code to create this post.

I'll check your suggestion and see if it works.
By mauimaus
#197913
Hello
I tried the pinMode(trigger[x]. INPUT_PULLUP );
it didn't change anything

I cleaned the code from the debugging. What I don't understand is that there is no analog output in the code for the earphones. Is that missing or it's automatic?
I tried with the speaker connectors as well (unplugging the earphones). Didn't work.
Is there an OUTPUT missing? I am beginning and very unsure of what i understand.

thanks again for your help!
Code: Select all
#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


//inputs
const int TRIG1 = A0;
const int TRIG2 = A4;
const int TRIG3 = A5;
const int TRIG4 = 1;
const int TRIG5 = 0;
int trigger[5] = {TRIG1,TRIG2,TRIG3,TRIG4,TRIG5};

//outputs
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

//library objects
SFEMP3Shield MP3player;
SdFat sd;

//file names in an array
char filename[5][13];



void setup() 
{
int x, index;
  SdFile file;
  byte result;
  char tempfilename[13];
  // Set the five trigger pins as inputs, and turn on the 
  // internal pullup resistors:
  
  for (x = 0; x <= 4; x++)
  {
    pinMode(trigger[x],INPUT_PULLUP);
    digitalWrite(trigger[x],HIGH);
  }

//debugging led for startup error code
  pinMode(ROT_LEDR,OUTPUT);
  digitalWrite(ROT_LEDR,HIGH);  // HIGH = off

  pinMode(EN_GPIO1,OUTPUT);
  digitalWrite(EN_GPIO1,LOW);  // MP3 mode / HIGH:amp off Low: amp on
  
  // Set the VS1053 volume. 0 is loudest, 255 is lowest (off):
  MP3player.setVolume(10,10);
  
  // 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;

  // The trigger pins are stored in the inputs[] array.
    // Read the pin and check if it is LOW (triggered).

    if (digitalRead(trigger[t-1]) == LOW)
    {
      // Wait for trigger to return high for a solid 50ms
      // (necessary to avoid switch bounce on T2 and T3
      // since we need those free for I2C control of the
      // amplifier)
      
      x = 0;
      while(x < 50)
      {
        if (digitalRead(trigger[t-1]) == HIGH)
          x++;
        else
          x = 0;
        delay(1);
      } 

       // Play the filename associated with the trigger number.
        // (If a file is already playing, this command will fail
        //  with error #2).

        result = MP3player.playMP3(filename[t-1]);

        if (result == 0) last_t = t;  // Save playing trigger

  }
}

        
        
void errorBlink(int blinks)
{
  // The following function will blink the red LED in the rotary
  // encoder (optional) a given number of times and repeat forever.
  // This is so you can see any startup error codes without having
  // to use the serial monitor window.

  int x;

  while(true) // Loop forever
  {
    for (x=0; x < blinks; x++) // Blink the given number of times
    {
      digitalWrite(ROT_LEDR,LOW); // Turn LED ON
      delay(250);
      digitalWrite(ROT_LEDR,HIGH); // Turn LED OFF
      delay(250);
    }
    delay(1500); // Longer pause between blink-groups
  }

}
By paulvha
#197920
So I took longer time to look at the code. You have stripped out too much.

I studied the original code from https://github.com/sparkfun/LilyPad_MP3 ... er_I2C.ino

Can you use that one again and change line 88:
boolean debugging = false;
to
boolean debugging = true;

What output message do you get?

The output is coming from a build-in amplifier to the headphone connection.
regards,
Paul
By mauimaus
#197928
Hello Paul Thanks again for your help

I tried the debug already but I couldn't see a line printed on the monitor.
Just fixed it by changing Serial.begin to 115200: works!

Error at the very start:

*Lilypad MP3 Player trigger sketch*
*initialize SD card... error, halting*

I reformated my SD card, re-checked my sound files, the inserted SD etc

One thing I find strange is in the libraries: "SPI" is displayed red, not black.

#include <SPI.h> // To talk to the SD card and MP3 chip

Could that be the problem?
If yes what does that mean?

In my library manager it says SPI.h is installed.

I am puzzled. (but making progress..)
By mauimaus
#197929
the problem lies at the very beginning, when i turn on the microcontroller
it doesn't initialize the sd card
i updated all librairies to their last versions
i really don't know what to do now...
thanks for your input!!
By mauimaus
#197931
Hi !!!
SUCCESSSSS!
I found the problem: I had to format my SD card in MS-DOS(fat) format!!!!
I had tried 2 different formats, but not this one.
Cheers :-)