Bonjour
Voilà, je t'ai fait un petit exemple avec 3 répertoires avec chacuns 4 (repNombreMp3) MP3.
01/001_xxx.MP3, 002_xxx.MP3, 003_xxx.MP3, 004_xxx.MP3
02/001_xxx.MP3, 002_xxx.MP3, 003_xxx.MP3, 004_xxx.MP3
03/001_xxx.MP3, 002_xxx.MP3, 003_xxx.MP3, 004_xxx.MP3
Le câblage de la carte MP3
Rx sur pin 2 Arduino
Tx sur pin 3 Arduino
Il y a 3 boutons pour la démo
btnYESpin pin 8
btnNOpin pin 9
btnWIN pin 10
Ces boutons sont actifs à 0 (INPUT_PULLUP, btnEtatPresse 0 donc câblés au GND)
Dans ton programme pour;
démarrer la lecture d'un fichier MP3, il suffit de faire:
mp3PlayFolderFile(1, 3);
pour jouer le MP3 003_xxx.MP3 dans le répertoire 01
jouer un MP3 au hasard, en fonction des états YES, NO ou WIN:
mp3PlayFolderRndFile(folderXXX); (selon enum foldersIndex)
Pour le générateur pseudo-aléatoire il y a:
randomSeed(analogRead(0)); // Pour l'initialisation
et
random(1, repNombreMp3 +1); // Pour choisir le fichier MP3
/*
Name: ARDFR_Eppler314_MP3serial.ino
Created: 05.05.2021
Author: jpbbricole
*/
//------------------------------------- Port série
#include <SoftwareSerial.h>
#define mp3SerialRxPin 3 //should connect to TX of the Serial MP3 Player module
#define mp3SerialTxPin 2 //connect to RX of the module
SoftwareSerial mp3Serial(mp3SerialRxPin, mp3SerialTxPin);
//------------------------------------- Carte MP3 série
static int8_t mp3SendBuffer[8] = {0} ;
#define mp3CmdSetVolume 0X06
#define mp3CmdSelDevice 0X09
#define mp3CmdSelSdCard 0X02
#define mp3CmdPlayFoldFile 0X0F
//------------------------------------- Boutons
#define btnYESpin 8
#define btnNOpin 9
#define btnWINpin 10
#define btnEtatPresse 0 // Etat lu d'un bouton pressé
enum foldersIndex {folder0, folderYES, folderNO, folderWIN};
#define repNombreMp3 4 // Nombre de fichiers MP3 par repertoire
void setup()
{
Serial.begin(115200);
mp3Serial.begin(9600);
delay(500); //Wait chip initialization is complete
pinMode(btnYESpin, INPUT_PULLUP);
pinMode(btnNOpin, INPUT_PULLUP);
pinMode(btnWINpin, INPUT_PULLUP);
randomSeed(analogRead(0));
mp3IfaceSendCommand(mp3CmdSelDevice, mp3CmdSelSdCard); //selectionner la carte SD
delay(200);
mp3IfaceSendCommand(mp3CmdSetVolume, 0X0012); // Régler le volume
//mp3PlayFolderFile(1, 1);
//delay(4000);
}
void loop()
{
int btnPin = 0;
if (digitalRead(btnYESpin) == btnEtatPresse)
{
mp3PlayFolderRndFile(folderYES);
btnPin = btnYESpin;
}
else if (digitalRead(btnNOpin) == btnEtatPresse)
{
mp3PlayFolderRndFile(folderNO);
btnPin = btnNOpin;
}
else if (digitalRead(btnWINpin) == btnEtatPresse)
{
mp3PlayFolderRndFile(folderWIN);
btnPin = btnWINpin;
}
if (btnYESpin != 0) // Si un bouton a été pressé
{
while(digitalRead(btnPin) == btnEtatPresse){} // Attendre le relachement du bouton
btnPin = 0;
}
}
void mp3PlayFolderRndFile(int folderIndex)
{
int mp3Index = random(1, repNombreMp3 +1);
Serial.println("Folder " + String(folderIndex) + "\tMP3 " + String(mp3Index));
mp3PlayFolderFile(folderIndex, mp3Index);
delay(500);
}
void mp3PlayFolderFile(int numFolder, int numFile)//, int16_t sndLevel)
{
int16_t cmdParam = numFile + ( numFolder <<8);
mp3IfaceSendCommand(mp3CmdPlayFoldFile, cmdParam);
if (numFolder == 0 && numFile == 0)
{
Serial.println("Play sound ABORT");
}
else
{
Serial.println("Play sound " + String(numFolder) + "-" + String(numFile));
}
}
void mp3IfaceSendCommand(int8_t command, int16_t dat)
{
delay(20);
mp3SendBuffer[0] = 0x7e; //starting byte
mp3SendBuffer[1] = 0xff; //version
mp3SendBuffer[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
mp3SendBuffer[3] = command;
mp3SendBuffer[4] = 0x00; //0x00 = no feedback, 0x01 = feedback
mp3SendBuffer[5] = (int8_t)(dat >> 8); //datah
mp3SendBuffer[6] = (int8_t)(dat); //datal
mp3SendBuffer[7] = 0xef; //ending byte
for(uint8_t i=0; i<8; i++)
{
mp3Serial.write(mp3SendBuffer[i]) ;
}
}
A+
Cordialement
jpbbricole