Faire jouer une musique en boucle dès la mise sous tentions de l’arduino.

Salut à tous ils amis,

Je suis un grand débutant en électronique et en programmation Arduino j’y connais presque rien.
Ça promet me direz-vous….

Voici mon projet:

Faire jouer une musique en boucle dès la mise sous tentions de l’arduino.

Dans ce bute, j’ai acheté une module Mp3 pour Arduino
PDF de la carte mp3

Je l’ai branché sur mon Arduino nano: Alim 5v / GND / XR sur D5 / TX sur D6
J'ai formaté la carte SD en FAT, mis mon mp3 noté 001.mp3 sur la racine principale, mais je ne sais pas quelle programmation mettre sur mon Arduino pour qu'il lise la carte SD en boucle dès l'allumage.

J'ai bien testé l'exemple que l'on peut télécharger sur le pdf, mais rien ne fonctionne et modifier l'exemple est beaucoup trop compliqué pour mes connaissance en c++

Est ce que quelqu'un pourrais m'aider stp merci d'avance.

L'exemple L1_Play_Music.ino semble faire ce que tu vaux:

#include <SoftwareSerial.h>
#include "RedMP3.h"

#define MP3_RX 7//RX of Serial MP3 module connect to D7 of Arduino
#define MP3_TX 8//TX to D8, note that D8 can not be used as RX on Mega2560, you should modify this if you do not use Arduino UNO
MP3 mp3(MP3_RX, MP3_TX);

int8_t index  = 0x01;//the first song in the TF card
int8_t volume = 0x1a;//0~0x1e (30 adjustable level)
void setup()
{
 delay(500);//Requires 500ms to wait for the MP3 module to initialize  
 mp3.playWithVolume(index,volume);
 delay(50);//you should wait for >=50ms between two commands
}

void loop()
{
 
}

Est-ce celui que tu as testé ?
En fait il ne joue pas en boucle, il joue uniquement le premier fichier à la mise sous tension.

Mais cette instruction devrait permettre de le faire :

mp3.singleCycle();//it can be set to cycle play the currently playing song

Peut-être la mettre dans le code dans le setup, qui deviendrait alors :

 delay(500);//Requires 500ms to wait for the MP3 module to initialize  
 mp3.playWithVolume(index,volume);
 delay(50);//you should wait for >=50ms between two commands
 mp3.singleCycle();//it can be set to cycle play the currently playing song
 delay(50);//you should wait for >=50ms between two commands

Bonjour

Je l'ai branché sur mon Arduino nano: Alim 5v / GND / XR sur D5 / TX sur D6

pour RX et TX il faut bien entendu s'assurer de la cohérence entre le câblage réalisé et le pins attribuées dans le code

Merci, je viens de tester et lors de la compilation il me met une erreur sur

sketch_apr11h:2:10: fatal error: RedMP3.h: No such file or directory
#include "RedMP3.h"

Tu as une idée ?

J'ai modifier aussi les attribution RX TX

#include <SoftwareSerial.h>
#include "RedMP3.h"

#define MP3_RX 5//RX of Serial MP3 module connect to D7 of Arduino
#define MP3_TX 6//TX to D8, note that D8 can not be used as RX on Mega2560, you should modify this if you do not use Arduino UNO
MP3 mp3(MP3_RX, MP3_TX);

int8_t index  = 0x01;//the first song in the TF card
int8_t volume = 0x1a;//0~0x1e (30 adjustable level)
void setup()
{
 delay(500);//Requires 500ms to wait for the MP3 module to initialize  
 mp3.playWithVolume(index,volume);
 delay(50);//you should wait for >=50ms between two commands
 mp3.singleCycle();//it can be set to cycle play the currently playing song
 delay(50);//you should wait for >=50ms between two commands

void loop()
{
 
}

L'exemple necessite l'installation d'une librairie : RedMP3

Désolé, mais je n'y comprend rien. Quelle librairie et je la met ou et comment ?

Elle est dans un fichier zip que tu peux télécharger, grâce à un lien qui se trouve dans le document pdf que j'ai indiqué plus haut. Le lien pointe vers un Google drive, avec un fichier zip dedans. Tu le télécharges sur ton disque et tu cherches l'archive arduino libraries.rar. Dedans se trouve un répertoire redmp3 que tu copies dans le répertoire libraries sous le répertoire arduino sur ton PC.

J'ai réussis, et j'en ai profiter pour lier deux programmations en une. :slight_smile:

La compilation fonctionne, mais j'ai quand même deux problèmes:

  • Ligne 116 même si je change la valeur du volume rien ne change en sortie, le son reste au maximum.
  • Qaund je retire le branchement USB et que je test l'arduino sur l'alimentation externe après 1 sec de musique j'ai un gros bourdonnement et plus de musique.

J'imagine que les deux problèmes sont liés.

Si quelqu'un peut m'aider la dessus, je planche mais je suis tres limité en programmation. :frowning:

ligne 116 ? de quel programme ?
dans les petits programmes (une vingtaine de lignes) des messages précédents le volume est réglé sur cette ligne :

int8_t volume = 0x1a;//0~0x1e (30 adjustable level)

donc presque à fond car 0x1a (ou 26) , étant presque égal au maximum 0x1e (ou 30)

Qaund je retire le branchement USB et que je test l'arduino sur l'alimentation externe après 1 sec de musique j'ai un gros bourdonnement et plus de musique.

et quelle est cette alimentation externe ?

/******DELOREAN SOUND******/

#include <SoftwareSerial.h>

#define ARDUINO_RX 2//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 3//connect to RX of the module
SoftwareSerial myMP3(ARDUINO_RX, ARDUINO_TX);

static int8_t Send_buf[6] = {0} ;
/************Command byte**************************/
/*basic commands*/
#define CMD_PLAY 0X01
#define CMD_PAUSE 0X02
#define CMD_NEXT_SONG 0X03
#define CMD_PREV_SONG 0X04
#define CMD_VOLUME_UP 0X05
#define CMD_VOLUME_DOWN 0X06
#define CMD_FORWARD 0X0A // >>
#define CMD_REWIND 0X0B // <<
#define CMD_STOP 0X0E
#define CMD_STOP_INJECT 0X0F//stop interruptting with a song, just stop the interlude

/*5 bytes commands*/
#define CMD_SEL_DEV 0X35
#define DEV_TF 0X01
#define CMD_IC_MODE 0X35
#define CMD_SLEEP 0X03
#define CMD_WAKE_UP 0X02
#define CMD_RESET 0X05

/*6 bytes commands*/ 
#define CMD_PLAY_W_INDEX 0X41
#define CMD_PLAY_FILE_NAME 0X42
#define CMD_INJECT_W_INDEX 0X43

/*Special commands*/
#define CMD_SET_VOLUME 0X31
#define CMD_PLAY_W_VOL 0X31

#define CMD_SET_PLAY_MODE 0X33
#define ALL_CYCLE 0X00
#define SINGLE_CYCLE 0X01

#define CMD_PLAY_COMBINE 0X45//can play combination up to 15 songs



/********DELOREAN LED********/

#define LED 3 // led3 en patte 6

float cli = 0;
float nivcli = 0;
int t = 0;
int a = 0;
float niv = 255;
int rnd = 0;
int start = 0;
float timeoff = 0;
int phare = 0;
int casecli = 0;
float nivphare = 0;
int nivpharetarget = 100;
float flx = 0.01;
float vtf =0.01;
float vtftarget =0.01 ;
float flxlumv = 20;
int flxlum = 0 ;
int chplu = 1;
int chss = 1;
int cht = 1;
int boom = 0;
int boomi = 0;



/******DELOREAN SOUND******/

void sendCommand(int8_t command, int16_t dat );


void setup() 
{
myMP3.begin(9600);
delay(500);//Wait chip initialization is complete
sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card 
delay(200);//wait for 200ms
setVolume(10); // Volume max 15
delay(200);
cyclePlay(1);



/********DELOREAN LED********/

pinMode(4, OUTPUT); // led est en sortie
pinMode(5, OUTPUT); // led est en sortie
pinMode(6, OUTPUT); // led est en sortie
pinMode(7, OUTPUT); // led est en sortie
pinMode(8, OUTPUT); // led est en sortie
pinMode(9, OUTPUT); // led est en sortie
pinMode(10, OUTPUT); // led est en sortie
pinMode(11, OUTPUT); // led est en sortie
pinMode(12, OUTPUT); // led est en sortie
pinMode(13, OUTPUT); // led est en sortie

}



/******DELOREAN SOUND******/

void loop() 
{

}

void setVolume(int8_t vol)
{
mp3_5bytes(CMD_SET_VOLUME, vol);
}
void playWithVolume(int16_t dat)
{
mp3_6bytes(CMD_PLAY_W_VOL, dat);
}

/*cycle play with an index*/
void cyclePlay(int16_t index)
{
mp3_6bytes(CMD_SET_PLAY_MODE,index);
}

void setCyleMode(int8_t AllSingle)
{
mp3_5bytes(CMD_SET_PLAY_MODE,AllSingle);
}


void playCombine(int8_t song[][2], int8_t number)
{
if(number > 15) return;//number of songs combined can not be more than 15
uint8_t nbytes;//the number of bytes of the command with starting byte and ending byte
nbytes = 2*number + 4;
int8_t Send_buf[nbytes];
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = nbytes - 2; //the number of bytes of the command without starting byte and ending byte
Send_buf[2] = CMD_PLAY_COMBINE; 
for(uint8_t i=0; i < number; i++)//
{
Send_buf[i*2+3] = song[i][0];
Send_buf[i*2+4] = song[i][1];
}
Send_buf[nbytes - 1] = 0xef;
sendBytes(nbytes);
}


void sendCommand(int8_t command, int16_t dat = 0)
{
delay(20);
if((command == CMD_PLAY_W_VOL)||(command == CMD_SET_PLAY_MODE)||(command == CMD_PLAY_COMBINE))
return;
else if(command < 0x10) 
{
mp3Basic(command);
}
else if(command < 0x40)
{ 
mp3_5bytes(command, dat);
}
else if(command < 0x50)
{ 
mp3_6bytes(command, dat);
}
else return;

}

void mp3Basic(int8_t command)
{
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0x02; //the number of bytes of the command without starting byte and ending byte
Send_buf[2] = command; 
Send_buf[3] = 0xef; //
sendBytes(4);
}
void mp3_5bytes(int8_t command, uint8_t dat)
{
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0x03; //the number of bytes of the command without starting byte and ending byte
Send_buf[2] = command; 
Send_buf[3] = dat; //
Send_buf[4] = 0xef; //
sendBytes(5);
}
void mp3_6bytes(int8_t command, int16_t dat)
{
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0x04; //the number of bytes of the command without starting byte and ending byte
Send_buf[2] = command; 
Send_buf[3] = (int8_t)(dat >> 8);//datah
Send_buf[4] = (int8_t)(dat); //datal
Send_buf[5] = 0xef; //
sendBytes(6);
}
void sendBytes(uint8_t nbytes)
{
for(uint8_t i=0; i < nbytes; i++)//
{
myMP3.write(Send_buf[i]) ;
}



/********DELOREAN LED********/

if (start == 0)
{
  // reset
  casecli = 0; cli=0;

  
  for (int res = 0; res<13; res++)
  { 
    analogWrite(res,0);
  }
  start = 1;
  delay(50);

// phare demarage

while (phare == 0)
{
  for (int ph = 0 ; ph<5; ph++)
  {
    // rien encore
    phare = 1;
  }
}
// phare demarre !!
analogWrite(5,100);
delay(2);
for (int phh=100 ; phh>0; phh--) 
  {
   analogWrite(5,phh); 
   delay(1); 
  }
delay(1);
for (int phh=100 ; phh>0; phh--) 
  {
   analogWrite(5,phh);  
     delay(1);
  }
  delay(2);
for (int phh=100 ; phh>=0; phh--) 
  {
   analogWrite(5,phh);  
     delay(1);
  }
delay(1000);
for (int phh=100 ; phh>0; phh--) 
  {
   analogWrite(5,phh); 
   delay(1); 
  }
delay(1);
for (int phh=100 ; phh>0; phh--) 
  {
   analogWrite(5,phh);  
     delay(1);
  }
  delay(2);
for (int phh=100 ; phh>0; phh--) 
  {
   analogWrite(5,phh);  
     delay(1);
  }
delay(10);

analogWrite(5,200);
delay(100);
// allumage code porte 

analogWrite(13,128);
delay(1000);
analogWrite(4,128);
delay(1000);
analogWrite(14,128);
delay(500);
analogWrite(10,90);// plutonium vue et tb
analogWrite(9,00);

analogWrite(11,0);
}

// DEBUT BOUCLE
// Clignotant

if (casecli==0)
  {
  nivcli = nivcli + 0.8; 
  analogWrite(6, nivcli);
  if (nivcli >250) { casecli = 1; }  
  }
if (casecli==1)
  {
  cli=cli+0.1;
  if (cli>70) { casecli = 2; cli=0; }  
  }
if (casecli==2)
  {
  nivcli = nivcli - 0.6; 
  analogWrite(6, nivcli);
  if (nivcli <1) { casecli = 3; }  
  }
if (casecli==3)
  {
  cli=cli+0.1;
  if (cli>70) { casecli = 0; cli=0; }  
  }  

// PHARE
if (nivphare<nivpharetarget) { nivphare=nivphare +0.3; }
if (nivphare>nivpharetarget) { nivphare=nivphare -0.1; }
if (random(200)==1) { nivpharetarget = random(20,200); }
analogWrite(5, nivphare);

// Flux Capacitor !!!

flxlumv = 17-vtf*120;

// ==== Allumage Bande Bleu ==== 
 if (vtf>0.11 && boom==0) {analogWrite(11,255); boom=1 ;} 
 if (vtf<0.07 && boom==1) {analogWrite(11,0); boom=0 ; }
 if (vtf>0.11 ) { niv=255 ;} 




if (flxlum < flxlumv) // reglage intensité  lumiere plus grand est le chiffre plus c'est sombre

{
  flxlum ++; analogWrite (8,127); analogWrite (7,127); analogWrite (12,127);
}
else
{
if (vtf<vtftarget) { vtf=vtf +0.00005; }
if (vtf>= vtftarget) { vtf=vtf -0.00005; }
if (random(250)==1) { vtftarget = random(1,30); vtftarget = vtftarget/120; }

flx = flx + vtf;
if (flx >0 && flx<1) { analogWrite (8,128); analogWrite (7,127); analogWrite (12,127);}
if (flx >1 && flx<2) { analogWrite (8,127); analogWrite (7,128); analogWrite (12,127);}
if (flx >2 && flx<3) { analogWrite (8,127); analogWrite (7,127); analogWrite (12,128);}
if (flx >3 && flx<4) { analogWrite (8,127); analogWrite (7,127); analogWrite (12,127);}
if (flx >4) {  flx=0;}
flxlum =0;
}

// chambre Plutonium
if (cht>50) { chplu = chplu + chss; cht =0; }
if (chplu>120) { chss = -1; }
if (chplu<10) { chss = 1; }
analogWrite (9,chplu);
cht++;



timeoff = timeoff +0.1 ;
if (timeoff>50000) 
{
  timeoff=0;
  start = 0;
}



}

Voilà, j'ai mis le code complet.

Pour répondre a ta question, l'alimentation sont 3 piles de 1,5V

J'ai l'impression que tu as copié le fichier cpp de la bibliothèque dans ton fichier ino. Ce n'était pas nécessaire.

Tu dois mettre le répertoire dont je parlais dans ton répertoire libraries sous le répertoire arduino. Et avec la ligne #include "Redmp3.h" le compilateur trouvera la bibliothèque et ses fonctions. Nul besoin de les inclure dans ton code.

Désolé mais là tu me parle chinois. C'est quoi un fichier cpp ? et mon problème de son tu pense que ça peut venir de la ?

Non ce sont deux choses différentes. On dirait que tu es presque au maximum du son. Essaye de changer le 10 à la ligne
setVolume(10);
Pour voir si ça fait plus ou moins fort. Essaye avec 5 puis avec 15 par exemple.

J'ai essayé, même avec 0 et rien de change malheureusement :frowning: