ArduinoGeneral

DFPlayer mini MP3 Player - problem with specify File to Play.

userHead Account cancelled 2017-11-20 21:42:48 2948 Views0 Replies
Hello, I have got a problem during my project.
I am working with Arduino Uno.
In my project I am using 2 ultrasound sensors , DFPlayer mini MP3 Player and a speaker.
The idea is to play specified file, when the sensor can sense a length from the object in range (10-30 cm).

I think that I understood badly the Manual with the part explaining how to specify the file.
I have saved .mp3 files on microSD in folder "01" and they are named "001.mp3" and "002.mp3"

Also, I have a question is there any method to block the switching between .mp3 files by IO_1 and IO_2?
I want to use buttons only to regulate the volume of sound, but I do not want to switch between files from microSD.

I am putting the code below. I hope that somebody will help me.
Code: Select all
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

# define ACTIVATED LOW

boolean isPlaying = false;

int Trig1 = 3;  //pin 2 Arduino połączony z pinem Trigger czujnika
int Echo1 = 2;  //pin 3 Arduino połączony z pinem Echo czujnika
int Trig2 = 5;
int Echo2 = 4;

int CM1;        //odległość w cm
long CZAS1;     //długość powrotnego impulsu w uS
int CM2;
long CZAS2;
  
void setup () 
{
  Serial.begin (9600);
  pinMode(Trig1, OUTPUT);                     //ustawienie pinu 2 w Arduino jako wyjście
  pinMode(Echo1, INPUT);                      //ustawienie pinu 3 w Arduino jako wejście
  pinMode(Trig2, OUTPUT);                     //ustawienie pinu 4 w Arduino jako wyjście
  pinMode(Echo2, INPUT);                      //ustawienie pinu 5 w Arduino jako wejście
  Serial.println("Test czujnika odleglosci");
}

void loop ()
{
  pomiar_odleglosci1();              //pomiar odległości
  Serial.print("Odleglosc1: ");      //wyświetlanie wyników na ekranie w pętli dfrobot 200 ms
  Serial.print(CM1);
  Serial.println(" cm");
  delay(100);

  pomiar_odleglosci2();
  Serial.print("Odleglosc2: ");      //wyświetlanie wyników na ekranie w pętli dfrobot 200 ms
  Serial.print(CM2);
  Serial.println(" cm");
  delay(100);
}

void pomiar_odleglosci1 ()
{
  digitalWrite(Trig1, LOW);        //ustawienie stanu wysokiego na 2 uS - impuls inicjalizujacy - patrz dokumentacja
  delayMicroseconds(2);
  digitalWrite(Trig1, HIGH);       //ustawienie stanu wysokiego na 10 uS - impuls inicjalizujacy - patrz dokumentacja
  delayMicroseconds(10);
  digitalWrite(Trig1, LOW);
  digitalWrite(Echo1, HIGH); 
  CZAS1 = pulseIn(Echo1, HIGH);
  CM1 = CZAS1 / 58;                //szerokość odbitego impulsu w uS podzielone przez 58 to odleglosc w cm - patrz dokumentacja
}

void pomiar_odleglosci2 ()
{
  digitalWrite(Trig2, LOW);        //ustawienie stanu wysokiego na 2 uS - impuls inicjalizujacy - patrz dokumentacja
  delayMicroseconds(2);
  digitalWrite(Trig2, HIGH);       //ustawienie stanu wysokiego na 10 uS - impuls inicjalizujacy - patrz dokumentacja
  delayMicroseconds(10);
  digitalWrite(Trig2, LOW);
  digitalWrite(Echo2, HIGH); 
  CZAS2 = pulseIn(Echo2, HIGH);
  CM2 = CZAS2 / 58;                //szerokość odbitego impulsu w uS podzielone przez 58 to odleglosc w cm - patrz dokumentacja
}
 
void playFront()
{
  execute_CMD(0x3F,0,0);
  delay(500);
  setVolume(20);
  delay(500);
  isPlaying = true;
}

void pause()
{
  execute_CMD(0x0E,0,0);
  delay(500);
}

void playBack()
{
  execute_CMD(0x3F, 0, 1);
  delay(500);
  setVolume(20);
  delay(500);
  isPlaying = true;
}

void setVolume(int volume)
{
  execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
  delay(500);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2// Build the command line
, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}

if(CM1<30&&CM1>10)
{
 playBack();
 delay(100);
}
 else
{
 pause();
 delay(100);
}
if(CM2<30&&CM2>10)
{
 playFront();
 delay(100);
}
 else
{
 pause();
 delay(100);
}
}