Pages: [1] 2 3 4   Go Down
Author Topic: Created a library for the MDFLY MP3 module (TDB380)  (Read 6924 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I created a library for use with the MDFLY MP3 module. It is apparently the same as a Tenga TDB380 module, but there are some differences between the commands. My library works on any pin, so it doesn't take up the serial port.

On my MDFLY module, retrieving the number of files in a folder works, but in the Tenga datasheet it says these commands are no longer valid. My library has no receive method, so either way you can't get the result. I did it that way because it was much simpler and smaller code and since those types of commands are supposed to be invalid anyway...

Constructor:

serMP3 mp3(10,11) //TX pin, Busy Pin



Methods are:
    begin(00-31) //intializes the module and sets the initial volume
    play(001 -199)
    pause()
    stop()
    playRand()
    volUP()
    volDN()
    setVol(00-31)
    cd(01-15) //Change directory
    send(byte) //I made this function public so that you can send other commands if they happen to be supported by your module)
    checkBsy() //gets the status of the Busy pin

Here is my example sketch:
Code:
#include <serMP3.h>

//union serial_data {
//   unsigned long number;
//   byte read_byte[3];
//} data;



serMP3 MP3(11,10);


void setup(){
  
  MP3.begin(31);
  Serial.begin(9600);
  
}

void loop(){
  byte sc = 0;
  unsigned long number = 0;
  byte n =0;
  
  if(Serial.available()){
    sc = Serial.read();  
  }
  
  if(sc == 'p' || sc == 'P'){ //Play (001-199)
    number=Serial.parseInt();
    n = byte(number);
    MP3.play(n);
  }
  if(sc == 's' || sc == 'S'){ //Stop Playing
    MP3.stop();
  }
  if(sc == '+'){ //Voume Up
    MP3.volUP();
  }
  if(sc == '-'){ //Volume Down
    MP3.volDN();
  }
  if(sc == 'v' || sc == 'V'){ //Set Volume (01-31)
    number=Serial.parseInt();
    n = byte(number);
    MP3.setVol(n);
  }
  if(sc == 'f' || sc == 'F'){ //Change Directory (01-15) 01 is root
    number=Serial.parseInt();
    n = byte(number);
    MP3.cd(n);
  }
  if(sc == '*'){ //Play a random file (will switch back to the root folder! Only plays from root folder)
   MP3.playRand();
  }
  if(sc == '/'){ //Pause/Resume
   MP3.pause();
  }
  
  
  
}

Hopefully someone finds it useful. Parts of this were taken from the softwareSerial library, but heavily modified and will not interfere with it.

* serMP3.cpp (2.46 KB - downloaded 61 times.)
* serMP3.h (1.23 KB - downloaded 62 times.)
« Last Edit: February 14, 2013, 02:37:59 pm by Retroplayer » Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1328
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 Thanks for sharing your library! Is this the module you are talking about? http://www.mdfly.com/index.php?main_page=product_info&products_id=284

 I looks good. What hardware are you using with it?
Logged


Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 Thanks for sharing your library! Is this the module you are talking about? http://www.mdfly.com/index.php?main_page=product_info&products_id=284

 I looks good. What hardware are you using with it?

That is the correct one. I am using it with the Uno Rev. 3.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Firstly,many thanks for taking the trouble to post your library. It appears to be exactly what I need.
I am currently building a talking scarecrow and have purchased the tdb380.
Unfortunately my understanding of the arduino is still very rudimentary.
I have wired the thing up using pins 10 and 11 and have created a directory in the library called serMP3 and copied your two files into it.
I have called up the library and pasted up your example program. It compiles and uploads
 OK and there is a brief snatch of sound from one of the several files I have added to the sd card.
So, hopefully quite a lot is OK ! What I can't figure out although I'm sure that its embarrassingly
obvious is how to call up a specific track. Do I insert a command int the sketch loop ?
Logged

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you mean from my example program, you would enter P001 to play MP3 number one, P002 to play MP3 number 2 and so on. This is using the serial terminal.

The TDB380 is 'stupid'. It doesn't matter what you name the MP3s. It calls the track by what order it is in the FAT table. So, you have to be careful how you copy them over and make sure they are copied in the order you want to address them. There is a program that can re-arrange stuff called DriveSort (http://www.anerty.net/software/file/DriveSort/)

If you just mean from your own code, you first have to create an instance (from my example):
Code:
serMP3 MP3(11,10);


The new instace is named MP3, but you can name that anything. 11 and 10 are the pin numbers. 11 is Tx and 10 is the busy signal.

Then to play something you call it like this:
Code:
MP3.play(n);

Where n is the number of the track you want to play.There are several other functions controlling all aspects of the module. See my original post to see how to use them. But it will always be called like MP3.stop() for example, to stop playback.

Send pause to pause the playback, and send pause again to resume.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks ! I think I understand. will go away and do some fiddling !
Logged

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just let me know if you have any questions or find any errors. Also appreciate any suggestions once you get things up and running.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry about this. I am now getting no sound at all. Pins 11 and 11 are connected to 15 and 13 on the module, the  amplifier is OK and connected to pin18. When I select the serial monitor and enter p001 then send... Nothing happens. I have a strong feeling that I am doing something daft. I haven't tried writing my own sketch yet as I don't really understand what you mean by creating my own instance, but I'm busy consulting books to try and find out !
 Cheers
Logged

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you happen to have other files on the SD card (non-Mp3s, etc...)?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No.. Just the system volume info (0 bytes) and 6 short mp3 files. I did get them going after a fashion using the sketch in instructables using pins 1 and 2 . It worked but I never quite got the file order sorted although I did use the program you mentioned to sort them.
Logged

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So it worked using the real serial port but you couldn't get it to work using pin 11? That was the only difference? Are you connecting GND on the module to the Arduino ground?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for sharing,

Some minor remarks on the lib
Code:
void serMP3::begin(uint8_t vol)
{
delay(3);  // is this really needed??????
setVol(vol);
delay(5);
stop();
}

Code:
bool serMP3::busy()    // just busy 'reads/sounds' better in an if or while statement if (!busy()) or while(busy())
{
return (digitalRead(_bsyPin) == HIGH);
}

further you could consider making small functions inline (directly in the .h file) to reduce the footprint of sketches.
(send must be public ?)
Code:
inline void stop() { send(239) };
inline void pause() { send(234) };
inline void volUP() { send(232) };
inline void volDN() { send(233) };
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great suggestions. The delay was needed at least on mine. It takes a moment for the module to respond. Before I added it, the volume was not getting changed. Someone may experiment with removing it and see if works for them.

I will make the other changes that you suggested. Especially the inline functions. I hadn't known how to do that before. Very useful.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, Gnd was connected to module and arduino. Think its time to go back to the real serial and see if it still works with that or if I've done something really daft !
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tried using pin 1 to tx and 2 as busy with the following sketch to test the module
[const int busyPin = 2; // the number of the Busy pin



void setup() {
// initialize the serial communication:
Serial.begin(4800); //Set to 4800 bps
Serial.write(0xEF); // Reset board
delay(2000);
pinMode(busyPin, INPUT);
}

void loop(){
if (digitalRead(busyPin) ==LOW) {
//device busy
}
else {
Serial.write(0); // Play Random file
delay(5000);
}
}]
The result was to play one of the files at full volume. when I recompile and run it plays the same file at greatly reduced volume and if I change the file number it plays the same file again at reduced volume.  Whilst I don't expect people to spend too much time on my problem If there is an obvious error here I would like to know what it is !
Logged

Pages: [1] 2 3 4   Go Up
Jump to: