#7679
There is already many useful commands in the library on its wiki, it's a powerful mini MP3 module. But actually, recently I found something new from the Chinese version datasheetof the module——play an Ad file while playing background music, after finishing the Ad, it will go back to play the background music file. Pretty cool but there is supprisingly no discription on the manual book :? I guess it's because this function is not stable, after I test. See in the end.

Anyway, this post will show you how to make it! Include:
*The revised library
*Sample sketch
*Connection
*etc more details


Requirments:

*Arduino controller( Here I use Uno, others are ok, but you have to check its interrupt pin )
*Mini MP3 Player module
*SD/TF card
*A button( as an external interrupt ) (suggested one)
*A speaker
*Some Wires
*A USB cable for arduino


STEP
STEP 1 Download and Install the library first!
(4.58 KiB) Downloaded 1177 times


STEP 2 Place two folders and two music files( MP3/ WAV/ WMA) into seperate folders like this: The name of the folders and music files must be like with the picture shows.

(38.61 KiB) Downloaded 8009 times


STEP 3
Connection

(101.88 KiB) Downloaded 8009 times


STEP 4 Upload the sketch below, it's also in the attachment.

Code: Select all
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3ad.h>

boolean play_state = HIGH;  // module play state HIGH:NOT playing; LOW:playing
boolean adState = HIGH;     // is the AD music playing? HIGH:NOT playing; LOW:playing
int playStatePin = 5;       //digital pin5: read the module play state

SoftwareSerial mySerial(10, 11); // RX, TX (Serial wiring; RX-TX, TX-RX)

void setup () {
  Serial.begin (9600);
  mySerial.begin (9600);
  mp3_set_serial (mySerial);  //set softwareSerial for DFPlayer-mini mp3 module
  mp3_set_volume (14);        //value 0~30
  delay(1000);                //wait for the module setting finished
  attachInterrupt(0, playAd, RISING );     //connect a button on Arduino interrupt 0
          //check interrupt map https://www.arduino.cc/en/Reference/AttachInterrupt
}

// The delays are indispensible!!!
void loop () {
  delay(1000); 
  play_state = digitalRead(playStatePin);
  if (play_state == HIGH ) {
    mp3_reset ();
    delay(1000);
    mp3_next ();
    delay(100);
  }

  if (adState == LOW) {
    delay(100);
    play_state = digitalRead(playStatePin);
    while (play_state == LOW)
      play_state = digitalRead(playStatePin);
    adState = HIGH;
    ad_stop();
    delay(100);
  }
}

void playAd() {
  ad_play (2);
  adState = LOW;
}


STEP 5 After all the steps above, if your SD card is no problem and the file name is correct (which start with number e.g. 0001Amazing Grace.mp3) the mp3 player should start to play the music, and if you press the button it will start to play the AD file in the ADVERT folder, after finish......That it! But sometimes, it will need to press the button several times to call the ad_play() function, I guess if use the millis() to replace delay() will help. More info seen in the arduino IDE sample sketch "blink without delay".

Thanks for your reading! :)
#7900
i don't know why about this problem... can you help me :([

#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3ad.h>

boolean play_state = HIGH; // module play state HIGH:NOT playing; LOW:playing
boolean adState = HIGH; // is the AD music playing? HIGH:NOT playing; LOW:playing
int playStatePin = 5; //digital pin5: read the module play state

SoftwareSerial mySerial(10, 11); // RX, TX (Serial wiring; RX-TX, TX-RX)

void setup () {
Serial.begin (9600);
mySerial.begin (9600);
mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
mp3_set_volume (14); //value 0~30
delay(1000); //wait for the module setting finished
attachInterrupt(0, playAd, RISING ); //connect a button on Arduino interrupt 0
//check interrupt map https://www.arduino.cc/en/Reference/AttachInterrupt
}

// The delays are indispensible!!!
void loop () {
delay(1000);
play_state = digitalRead(playStatePin);
if (play_state == HIGH ) {
mp3_reset ();
delay(1000);
mp3_next ();
delay(100);
}

if (adState == LOW) {
delay(100);
play_state = digitalRead(playStatePin);
while (play_state == LOW)
play_state = digitalRead(playStatePin);
adState = HIGH;
ad_stop();
delay(100);
}
}

void playAd() {
ad_play (2);
adState = LOW;
}

sketch_nov26a.ino: In function 'void setup()':
sketch_nov26a:13: error: 'mp3_set_serial' was not declared in this scope
sketch_nov26a:14: error: 'mp3_set_volume' was not declared in this scope
sketch_nov26a.ino: In function 'void loop()':
sketch_nov26a:25: error: 'mp3_reset' was not declared in this scope
sketch_nov26a:27: error: 'mp3_next' was not declared in this scope
sketch_nov26a:37: error: 'ad_stop' was not declared in this scope
sketch_nov26a.ino: In function 'void playAd()':
sketch_nov26a:43: error: 'ad_play' was not declared in this scope
#8433
budiwahyunugroho92 wrote:I do not know what happened with my project .. I've been followed the tutorial ... but the result is like this.

Hi,

Sorry for my belated reply!

The display error are ofen caused by the baud rate setting, please make it as corresponding as the setting in your code.
Attachments
(5.19 KiB) Downloaded 6120 times