4 Boutons 4 Sons

L’idée est d’avoir 4 boutons et d’y associer un son à chacun en utilisant un Arduino.

On a utilisé DFPlayer-Mini-MP3-Player pour lire les fichiers de la mini carte SD.

Suivant le schéma du circuit ci-dessous:

Voici le code:

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(2, 3); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

int boutonA = 8;
int boutonB = 9;
int boutonC = 10;
int boutonD = 11;

int val;

/////////////////////////////////////

void printDetail(uint8_t type, int value);

/////////////////////////////////////

void setup() {
  pinMode(boutonA, INPUT_PULLUP);
  pinMode(boutonB, INPUT_PULLUP);
  pinMode(boutonC, INPUT_PULLUP);
  pinMode(boutonD, INPUT_PULLUP);

  mySoftwareSerial.begin(9600);

  Serial.begin(115200);

  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true);
  }

  Serial.println(F("DFPlayer Mini online."));
  myDFPlayer.volume(30);  //Set volume value (0~30).
  //myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
  //myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
  //myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);

}

/////////////////////////////////////

void loop() {

  int boutonValeurA = digitalRead(boutonA);
  int boutonValeurB = digitalRead(boutonB);
  int boutonValeurC = digitalRead(boutonC);
  int boutonValeurD = digitalRead(boutonD);

  if (boutonValeurA == 0 ) {
    myDFPlayer.play(1);
    val = 0;
  } else if (boutonValeurB == 0) {
    myDFPlayer.play(2);
    val = 1;
  } else if (boutonValeurC == 0) {
    myDFPlayer.play(3);
    val = 2;
  } else if (boutonValeurD == 0) {
    myDFPlayer.play(4);
    val = 3;
  }
Serial.print(" A = ");
  Serial.print(boutonValeurA);
  Serial.print(" B = ");
  Serial.print(boutonValeurB);
  Serial.print(" C = ");
  Serial.print(boutonValeurC);
  Serial.print(" D = ");
  Serial.println(boutonValeurD);

  delay(100);
}

Démo à voir ici: https://youtube.com/shorts/-2hG8b7A4vc