Go Down

Topic: Serial MP3 Player Implementation (Read 105 times) previous topic - next topic

Quantum357

I've recently got a Catalex Serial MP3 Player and I have not had any success implementing it on my own.  I'm following the BasicCommands example, and although my code seems to match the example, it doesn't want to play, and it doesn't show any errors.  Here's my code:

Code: [Select]
#include "SerialMP3Player.h"
#include <SPI.h>
#include <RF24.h>


#define TX 11
#define RX 10
SerialMP3Player mp3(RX,TX);

RF24 radio(9, 10);
const uint64_t pipe = 0xE6E6E6E6E6E6; // Needs to be the same for communicating between 2 NRF24L01
float data[10];

int red = 5;
int blue = 4;
int white = 3;

long audioDelayPeriod = 10000;
long audioDelay = 0;
long whiteDelay = 0;

boolean playingSFX = false;

void setup() {
  digitalWrite(red, HIGH);
  radio.begin(); // Start the NRF24L01

  mp3.showDebug(1);
  Serial.begin(9600);     // start serial interface
  mp3.begin(9600);        // start mp3-communication
  delay(500);             // wait for init

  mp3.sendCommand(CMD_SEL_DEV, 0, 2);   //select sd-card
  delay(500); 

  radio.openReadingPipe(2,pipe);
  radio.startListening(); // Listen to see if information received
 
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(white, OUTPUT);
  mp3.playSL(2,1);
}

void loop() {
  if (false) {
    playingSFX = true;
  } else {
    playingSFX = false;
  }
 
  if (radio.available()) {
    radio.read(&data, radio.getDynamicPayloadSize());

    if (data[6] == 1) {
      mp3.playSL(2,8);
    }   
  }
 
  if (!playingSFX){
    if (audioDelay > audioDelayPeriod){
      audioDelayPeriod = random(5000, 10000); //set delay period
      mp3.playSL(1,random(1, 32));
    } else {
      audioDelay ++;
    }
  }
}


Example:
Code: [Select]

#include "SerialMP3Player.h"

#define TX 11
#define RX 10

SerialMP3Player mp3(RX,TX);


void setup() {
  mp3.showDebug(1);       // print what we are sending to the mp3 board.

  Serial.begin(9600);     // start serial interface
  mp3.begin(9600);        // start mp3-communication
  delay(500);             // wait for init

  mp3.sendCommand(CMD_SEL_DEV, 0, 2);   //select sd-card
  delay(500);             // wait for init

  menu('?',0); // print the menu options.
}

 char c;  // char from Serial
 char cmd=' ';
 char cmd1=' ';


// the loop function runs over and over again forever
void loop() {

  if (Serial.available()){
    c = Serial.read();
    decode_c(); // Decode c.
  }
  // Check for the answer.
  if (mp3.available()){
    Serial.println(mp3.decodeMP3Answer()); // print decoded answers from mp3
  }
}

void menu(char op, int nval){
  // Menu
  switch (op){
    case '?':
    case 'h':
        Serial.println("SerialMP3Player Basic Commands:");
        Serial.println(" ? - Display Menu options. ");
        Serial.println(" P01 - Play 01 file");
        Serial.println(" F01 - Play 01 folder");
        Serial.println(" S01 - Play 01 file in loop");
        Serial.println(" V01 - Play 01 file, volume 30");
        Serial.println(" p - Play");
        Serial.println(" a - pause");
        Serial.println(" s - stop ");
        Serial.println(" > - Next");
        Serial.println(" < - Previous");
        Serial.println(" + - Volume UP");
        Serial.println(" - - Volume DOWN");
        Serial.println(" v15 - Set Volume to 15");
        Serial.println(" c - Query current file");
        Serial.println(" q - Query status");
        Serial.println(" x - Query folder count");
        Serial.println(" t - Query total file count");
        Serial.println(" r - Reset");
        Serial.println(" e - Sleep");
        Serial.println(" w - Wake up");
        break;

    case 'P':
        Serial.println("Play");
        mp3.play(nval);
        break;

    case 'F':
        Serial.println("Play Folder");
        mp3.playF(nval);
        break;

    case 'S':
        Serial.println("Play loop");
        mp3.playSL(nval);
        break;

    case 'V':
        Serial.println("Play file at 30 volume");
        mp3.play(nval,30);
        break;


    case 'p':
        Serial.println("Play");
        mp3.play();
        break;

    case 'a':
        Serial.println("Pause");
        mp3.pause();
        break;

    case 's':
        Serial.println("Stop");
        mp3.stop();
        break;

    case '>':
        Serial.println("Next");
        mp3.playNext();
        break;

    case '<':
        Serial.println("Previous");
        mp3.playPrevious();
        break;

    case '+':
        Serial.println("Volume UP");
        mp3.volUp();
        break;

    case '-':
        Serial.println("Volume Down");
        mp3.volDown();
        break;

    case 'v':
        Serial.println("Set to Volume");
          mp3.setVol(nval);
          mp3.qVol();
        break;

    case 'c':
        Serial.println("Query current file");
        mp3.qPlaying();
        break;

    case 'q':
        Serial.println("Query status");
        mp3.qStatus();
        break;

    case 'x':
        Serial.println("Query folder count");
        mp3.qTFolders();
        break;

    case 't':
        Serial.println("Query total file count");
        mp3.qTTracks();
        break;

    case 'r':
        Serial.println("Reset");
        mp3.reset();
        break;

    case 'e':
        Serial.println("Sleep");
        mp3.sleep();
        break;

    case 'w':
        Serial.println("Wake up");
        mp3.wakeup();
        break;
  }
}

void decode_c(){
  // Decode c looking for a specific command or a digit

  // if c is a 'v', 'P', 'F', 'S' or 'V' wait for the number XX
  if (c=='v' || c=='P' || c=='F' || c=='S' || c=='V'){
    cmd=c;
  }else{
    // maybe c is part of XX number
    if(c>='0' && c<='9'){
      // if c is a digit
      if(cmd1==' '){
        // if cmd1 is empty then c is the first digit
        cmd1 = c;
      }else{
        // if cmd1 is not empty c is the second digit
        menu(cmd, ((cmd1-'0')*10)+(c-'0'));
        cmd = ' ';
        cmd1 = ' ';
      }
    }else{
      // c is not a digit nor 'v', 'P', 'F' or 'S' so just call menu(c, nval);
      menu(c, 0);
    }
  }
}


When I run the example it works exactly as planned, but when I run mine, it is silent.  Please help!

hzrnbgy

Code: [Select]

  if (false) {
    playingSFX = true;
  } else {
    playingSFX = false;
  }


This part will always evaluate to playingSFX = false. I assume you did not intend the if(false) in this

Power_Broker

You might also find it slightly easier/faster to use DFPlayerMini_Fast.h as your MP3 driver library
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Quantum357

Code: [Select]

  if (false) {
    playingSFX = true;
  } else {
    playingSFX = false;
  }


This part will always evaluate to playingSFX = false. I assume you did not intend the if(false) in this
Actually that is intended, but only as temporary until I can figure how to get the status of the mp3 module (e.g. playing, stopped, etc.)


You might also find it slightly easier/faster to use DFPlayerMini_Fast.h as your MP3 driver library
I'll try that, thanks.

Quantum357

You might also find it slightly easier/faster to use DFPlayerMini_Fast.h as your MP3 driver library
Does it matter that I'm not using the DFPlayer Module?  I'm using the Catalex Serial MP3 Player Module.

Go Up