Go Down

Topic: Created a library for the MDFLY MP3 module (TDB380) (Read 8623 times) previous topic - next topic

Retroplayer

BTW, this is the only thing that checkBsy is doing. No reason you couldn't do the same thing in your code without calling the function. The only real reason I have it in there was for my library to use it. Not really any advantage to using it in your code versus just using digitalRead(bsyPin);

But anyway, that shouldn't have anything to do with why your MP3 is stopping short. The only reasons I can think of for that to happen is if the file is corrupt and the module crashes out of playback or it received (or thought it did) an overriding command. A GND issue would make things really unstable and cause the module to hiccup. I had all the issues you have described happen to me whenever I forgot to connect the grounds together.

DavidFMarks

Further checks reveal a software problem (surprise surprise). The sketch works OK when I have a pushbutton on pin 2 . The problem has occurred when I have used my newly acquired PIR detector and connected its signal pin in place of the pushbutton. I assumed it would behave exactly the the same. Obviously not .. I need to write some sketches for the PIR to see how it works !

robtillaart

Rob Tillaart

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

DavidFMarks

DID WONDER ABOUT THAT BUT THE SKETCH HAS AN ANTIBOUNCE BIT IN IT.

DavidFMarks

I have now got my mp3 player working nicely .. thanks for the help. I have now set up a servo operated jaw movement which I want to run concurrently with the playing of the mp3 file, but don't seem to be able to do it. I thought that if I called play and then a few  servo movements that the file would start to play and continue whilst the servo operates but it doesn't. The servo starts to move then the file starts to play and is almost immediately interrupted by the next servo movements. Any suggestions would be appreciated.

Retroplayer

The file stops playing when the servo moves? May be a timer conflict. Can I see your code?

DavidFMarks

Hi again, Here is the code (I am enclosing it in square brackets which I hope is correct (the help file is down at the moment)
[
#include <serMP3.h>
#include <Servo.h>
Servo VerticalEyes;        // Define our Servo
Servo RightEyeHorizontal;  // Define our Servo
Servo LeftEyeHorizontal;   // Define our Servo
Servo Mouth;               // Define our Servo

const int buttonPin = 2;   // the number of the pushbutton pin
int buttonState;           // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin

serMP3 MP3(11,10);         //instance of mp3 library   


unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

int n=1;                             // n defines the file number to be played

void setup()
{
  VerticalEyes.attach(10);      // servo connected to digital pin 10
  RightEyeHorizontal.attach(9); // servo connected to digital pin 9
  LeftEyeHorizontal.attach(8);  // servo connected to digital pin 8
  Mouth.attach(7);
  pinMode(buttonPin, INPUT);    //set pin 2 to input
  MP3.begin(31);
  Serial.begin(9600);
  Serial.write(0xEF);
  Serial.write(0xE0);
   Mouth.write(100);
}

void loop()
{
int reading = digitalRead(buttonPin);
  if (reading != lastButtonState)
{
    lastDebounceTime = millis();
}
  if ((millis() - lastDebounceTime) > debounceDelay)
{
        buttonState = reading;
}
  if (buttonState == HIGH)    // if button press is genuine
{
    n=14;                     // set number of file to be called
   
    Playfile(n);            //  then call it
   
     int hpos=90;          //  define position of horizontal eye servos
    REye(hpos);             //  call Reye function and move servo to defined position
    LEye(hpos);             //  call Leye function and move servo to defined position
    int vpos=135;            //  define position of vertical eye servo
    VertEyes(vpos);         //  call VertEyes function and move servo to defined position   
}
lastButtonState = reading;
delay(150);
}
void Playfile(byte n)         // define playfile functio
{
 
 
  MP3.play(n);
  delay(150);
   Mouth.write(100);
   delay(300);
   Mouth.write (70);
   delay(200);
   Mouth.write(100);
   Mouth.write(100);
   delay(300);
   Mouth.write (70);
   delay(200);
   Mouth.write(100);
   Mouth.write (70);
   delay(200);
   Mouth.write(100);
    Mouth.write(100);
   delay(300);
    Mouth.write (70);
   delay(200);
   Mouth.write(100); 
}
int VertEyes(int vpos)        // Define VertEyes function
{
VerticalEyes.write(vpos);
}
int REye(int hpos)            //Define REye function
{
RightEyeHorizontal.write(hpos);
}

int LEye(int hpos)             //Define LEye function
{
LeftEyeHorizontal.write(hpos);
}

]

Retroplayer

It should be

[ code ]
your code goes here
[ /code ]

Without the spaces of course. Or just click on the # symbol at the start of your code and then again at the end.

DavidFMarks

Aha ! Thanks, sorry to clutter the place up !

Retroplayer

I can't see anything in your code that would cause that behavior. I just checked my library and I am not using a timer (I coulldn't remember), so it's not that either. My guess would be that somehow it is reading your switch as changing states somehow. How are you pulling it up? Noise from the servos might be triggering it perhaps.

I do see a few pieces in your code you should look at. Nothing that would cause your issue, but thought I would mention them:

Code: [Select]

void setup()
{

 Serial.write(0xEF); // ********I assume you meant to send these to the MP3 module
 Serial.write(0xE0); //*********if so, they should be mp3.send(0xE0) (or MP3.stop())
}


Code: [Select]

void Playfile(byte n)
{
 
 
 MP3.play(n);
 delay(150);
  Mouth.write(100);
  delay(300);
  Mouth.write (70);
  delay(200);
  Mouth.write(100);
  Mouth.write(100); //Did you really mean to call that twice?
  delay(300);
  Mouth.write (70);
  delay(200);
  Mouth.write(100); //I may be wrong, but I doubt you will see it do anything but move to 70
  Mouth.write (70);
  delay(200);
  Mouth.write(100);
   Mouth.write(100); //twice again
  delay(300);
   Mouth.write (70);
  delay(200);
  Mouth.write(100);  
}


In the instances were you call mouth.write(100) twice right away, I don't really understand why. Also, when you call 100 and 70 back to back, it probably is only going to move to 70 (unless I don't understand something about the servo library.)

Retroplayer

BTW, sending E0 would set the volume at 24, which you just set at 31 with the MP3.begin(31) command. 0xEF is just stop. You could do the same thing with MP3.stop(); or really it isn't needed since I do it for you in the begin method.

So you could just remove those two lines altogether.

You did make me open my code again and I see some areas that I can improve on it now that I am a bit more experienced. Nothing functional will change, but I will post the updated library later (maybe tomorrow even.)

DavidFMarks

Hi, Thanks. the errors you point out are just a few silly cut and paste errors that I hadn't spotted. I just bunged in a few movements to try it out. I think you may have hit the spot when you mention the pushbutton. I had trouble with that before and put in the debounce routine suggested although i have to confess I couldn't fully understand it. For that reason I decided to take the cowardly way out! I have been on ebay and ordered a few inverting Schmitt triggers to do the job then I can declutter the code. I was trying to control it from a PIR detector and having trouble so I reckon it's something to do with the switch.

Retroplayer

Are you pulling the switch up at all? It doesn't look like you are in code. Do you have an external resistor?

The schmidt trigger isn't the cowardly way, it is the reliable way. Most shy away from it only because it adds components.

DavidFMarks

Pin 2 is pulled low with 10k to ground. Pushbutton takes it up to 5v. I take your point about servo noise although I have got the servo powered from a separate 6v battery pack

Retroplayer

Try pulling pin 2 up with the a 1K instead. Obviously you will have to watch for it to go low instead.

Go Up