Project tutorial
Vowels Game with Arduino and YX5300 MP3 Module Catalex

Vowels Game with Arduino and YX5300 MP3 Module Catalex © LGPL

You'll learn how to create a Vowels Game with Arduino to teach children.

  • 11 views
  • 1 comment
  • 1 respect

Components and supplies

About this project

Are you able to read this question? That's weird! I asked this question on purpose. If you can read this text, it is because you know the whole alphabet and, of course, learned about all the vowels.

Vowels are present in all words. It is impossible to escape from each of them. Now, let me ask you a question.

Was your childhood learning fun and involved technological resources?

I'm sure that the learning resources were few and you used traditional methods to learn the vowels and the alphabet.

After all, is it possible to use some technological resources to learn vowels?

In this article, I will teach you how to teach your students and children vowels through a game.

I will teach you how to create a system with a voice, where your child/student will hear the sound of the letter and must press a button to indicate the correct letter.

Thus, they will learn while playing and will always be motivated to study.

Now, I'm going to show you the step-by-step process for you to create your own game, and teach the vowels to children.

Developing the Game of Vowels with Arduino

The heart of the game is the JLCPCB printed circuit board of the Vowels. You can access this link and download the project files. It has 5 buttons. You will use each button to represent a vowel and connect it to your Arduino.

The printed circuit board is shown in Figure 1.

With this PCB Project, you can connect it with the Arduino and create your game. Next, I will offer you an electronic schematic for you to assemble or build the project on your protoboard.

From this schematic, we set up the layout of the electronic board. It is shown in Figure 2 and you can download the files and make your project.

Choose 5 pins from the Arduino and connect the jumpers on the board with the Arduino. Or otherwise, you can assemble the following electronic diagram.

The Project Idea

I will teach you how to assemble an MP3 sound system with Arduino. This system will be responsible for reproducing the voice speaking the letter. The sound of each letter will be drawn using a value from 1 to 5, where 1 represents A and 5 represents U.

Thus, when the child hears the sound, he must look at the keyboard, recognize the spelling of the vowel, and press the correct key.

If it fails, the system will flash the Red LED 3 times. Otherwise, the system will activate a buzzer for 5 seconds and draw a new vowel.

To do this, you must assemble the following circuit.

In this circuit, you will connect the MP3 Module and the vowel board on the Arduino. The Bluetooth module was used to represent the Catalex MP3 Module.

The Arduino will be responsible for sort the 5 numbers and then sending the command to activate the drawn vowel.

After that, we will wait until the child hears and presses a button, as shown in the Figure below.

Each button above represents a vowel of the alphabet. Next, I'll show you how you will build the programming logic for this project.

Building the Game's Programming Logic

The vowel game system is based on the operation of the YX5300 module. This module has some functions, however, we will focus on presenting the game's working structure through the main functions of the YX5300 module.

Below I am providing you with all the programming logic of the project.

In the following, I will explain the step by step to building the logic of this fun game for children.

#include <SoftwareSerial.h>

#define ARDUINO_RX 5 //should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6 //connect to RX of the module

SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX);

static int8_t Send_buf[8] = {0}; // Buffer for Send commands. // BETTER LOCALLY
static uint8_t ansbuf[10] = {0}; // Buffer for the answers. // BETTER LOCALLY

String mp3Answer; // Answer from the MP3.

String sanswer(void);
String sbyte2hex(uint8_t b);

/************ Command byte **************************/
#define CMD_NEXT_SONG 0X01 // Play next song.
#define CMD_PREV_SONG 0X02 // Play previous song.
#define CMD_PLAY_W_INDEX 0X03
#define CMD_VOLUME_UP 0X04
#define CMD_VOLUME_DOWN 0X05
#define CMD_SET_VOLUME 0X06

#define CMD_SNG_CYCL_PLAY 0X08 // Single Cycle Play.
#define CMD_SEL_DEV 0X09
#define CMD_SLEEP_MODE 0X0A
#define CMD_WAKE_UP 0X0B
#define CMD_RESET 0X0C
#define CMD_PLAY 0X0D
#define CMD_PAUSE 0X0E
#define CMD_PLAY_FOLDER_FILE 0X0F

#define CMD_STOP_PLAY 0X16 // Stop playing continuously.
#define CMD_FOLDER_CYCLE 0X17
#define CMD_SHUFFLE_PLAY 0x18 //
#define CMD_SET_SNGL_CYCL 0X19 // Set single cycle.

#define CMD_SET_DAC 0X1A
#define DAC_ON 0X00
#define DAC_OFF 0X01

#define CMD_PLAY_W_VOL 0X22
#define CMD_PLAYING_N 0x4C
#define CMD_QUERY_STATUS 0x42
#define CMD_QUERY_VOLUME 0x43
#define CMD_QUERY_FLDR_TRACKS 0x4e
#define CMD_QUERY_TOT_TRACKS 0x48
#define CMD_QUERY_FLDR_COUNT 0x4f

/************ Opitons **************************/
#define DEV_TF 0X02

/*********************************************************************/
int numero;
byte estado;
byte buzzer = 2;
byte pin = 0;
byte SortNumber = 0;

bool button = 0;

void setup()
{
Serial.begin(9600);
mp3.begin(9600);
delay(500);

for(pin = 8; pin < 13; pin++)
{
pinMode(pin, INPUT);
}

sendCommand(CMD_SEL_DEV, 0, DEV_TF);
delay(500);
}

void loop()
{
pin = 8;

randomSeed(analogRead(A0));
numero = random(8,12);
SortNumber = numero;
numero = numero - 7;

Serial.println(numero);
sendCommand(0x03, 0, numero);
delay(1000);

do
{
button = digitalRead(pin);
Serial.println(button);
pin++;

if(pin > 13)
{
pin = 8;
}

Serial.println("Varrendo...");
Serial.println(pin);
//delay(1000);

}while(button != 1);

Serial.println("Saiu...");

if(button == 1 && (pin-1) != SortNumber)
{
sendCommand(0x03, 0, 6);
delay(3000);
}

if(button == 1 && (pin-1) == SortNumber)
{
sendCommand(0x03, 0, 7);
delay(3000);
}

// Check for the answer.
if (mp3.available())
{
Serial.println(decodeMP3Answer());
}
delay(100);
//Serial.println("Tocando musica...");
}

/********************************************************************************/
/*Function sendMP3Command: seek for a 'c' command and send it to MP3 */
/*Parameter: c. Code for the MP3 Command, 'h' for help. */
/*Return: void */

void sendMP3Command(char c) {

switch (c) {
case '?':
case 'h':
Serial.println("HELP ");
Serial.println(" p = Play");
Serial.println(" P = Pause");
Serial.println(" > = Next");
Serial.println(" < = Previous");
Serial.println(" s = Stop Play");
Serial.println(" + = Volume UP");
Serial.println(" - = Volume DOWN");
Serial.println(" c = Query current file");
Serial.println(" q = Query status");
Serial.println(" v = Query volume");
Serial.println(" x = Query folder count");
Serial.println(" t = Query total file count");
Serial.println(" f = Play folder 1.");
Serial.println(" S = Sleep");
Serial.println(" W = Wake up");
Serial.println(" r = Reset");
break;

case 'p':
Serial.println("Play ");
sendCommand(CMD_PLAY);
break;

case 'P':
Serial.println("Pause");
sendCommand(CMD_PAUSE);
break;

case '>':
Serial.println("Next");
sendCommand(CMD_NEXT_SONG);
sendCommand(CMD_PLAYING_N); // ask for the number of file is playing
break;

case '<':
Serial.println("Previous");
sendCommand(CMD_PREV_SONG);
sendCommand(CMD_PLAYING_N); // ask for the number of file is playing
break;

case 's':
Serial.println("Stop Play");
sendCommand(CMD_STOP_PLAY);
break;

case '+':
Serial.println("Volume Up");
sendCommand(CMD_VOLUME_UP);
break;

case '-':
Serial.println("Volume Down");
sendCommand(CMD_VOLUME_DOWN);
break;

case 'c':
Serial.println("Query current file");
sendCommand(CMD_PLAYING_N);
break;

case 'q':
Serial.println("Query status");
sendCommand(CMD_QUERY_STATUS);
break;

case 'v':
Serial.println("Query volume");
sendCommand(CMD_QUERY_VOLUME);
break;

case 'x':
Serial.println("Query folder count");
sendCommand(CMD_QUERY_FLDR_COUNT);
break;

case 't':
Serial.println("Query total file count");
sendCommand(CMD_QUERY_TOT_TRACKS);
break;

case 'f':
Serial.println("Playing folder 1");
sendCommand(CMD_FOLDER_CYCLE, 1, 0);
break;

case 'S':
Serial.println("Sleep");
sendCommand(CMD_SLEEP_MODE);
break;

case 'W':
Serial.println("Wake up");
sendCommand(CMD_WAKE_UP);
break;

case 'r':
Serial.println("Reset");
sendCommand(CMD_RESET);
break;
}
}

/********************************************************************************/
/*Function decodeMP3Answer: Decode MP3 answer. */
/*Parameter:-void */
/*Return: The */

String decodeMP3Answer() {
String decodedMP3Answer = "";

decodedMP3Answer += sanswer();

switch (ansbuf[3]) {
case 0x3A:
decodedMP3Answer += " -> Memory card inserted.";
break;

case 0x3D:
decodedMP3Answer += " -> Completed play num " + String(ansbuf[6], DEC);
//sendCommand(CMD_NEXT_SONG);
//sendCommand(CMD_PLAYING_N); // ask for the number of file is playing
break;

case 0x40:
decodedMP3Answer += " -> Error";
break;

case 0x41:
decodedMP3Answer += " -> Data recived correctly. ";
break;

case 0x42:
decodedMP3Answer += " -> Status playing: " + String(ansbuf[6], DEC);
break;

case 0x48:
decodedMP3Answer += " -> File count: " + String(ansbuf[6], DEC);
break;

case 0x4C:
decodedMP3Answer += " -> Playing: " + String(ansbuf[6], DEC);
break;

case 0x4E:
decodedMP3Answer += " -> Folder file count: " + String(ansbuf[6], DEC);
break;

case 0x4F:
decodedMP3Answer += " -> Folder count: " + String(ansbuf[6], DEC);
break;
}

return decodedMP3Answer;
}

/********************************************************************************/
/*Function: Send command to the MP3 */
/*Parameter: byte command */
/*Parameter: byte dat1 parameter for the command */
/*Parameter: byte dat2 parameter for the command */

void sendCommand(byte command){
sendCommand(command, 0, 0);
}

void sendCommand(byte command, byte dat1, byte dat2){
delay(20);
Send_buf[0] = 0x7E; //
Send_buf[1] = 0xFF; //
Send_buf[2] = 0x06; // Len
Send_buf[3] = command; //
Send_buf[4] = 0x01; // 0x00 NO, 0x01 feedback
Send_buf[5] = dat1; // datah
Send_buf[6] = dat2; // datal
Send_buf[7] = 0xEF; //
Serial.print("Sending: ");
for (uint8_t i = 0; i < 8; i++)
{
mp3.write(Send_buf[i]) ;
Serial.print(sbyte2hex(Send_buf[i]));
}
Serial.println();
}



/********************************************************************************/
/*Function: sbyte2hex. Returns a byte data in HEX format. */
/*Parameter:- uint8_t b. Byte to convert to HEX. */
/*Return: String */


String sbyte2hex(uint8_t b)
{
String shex;

shex = "0X";

if (b < 16) shex += "0";
shex += String(b, HEX);
shex += " ";
return shex;
}


/********************************************************************************/
/*Function: shex2int. Returns a int from an HEX string. */
/*Parameter: s. char *s to convert to HEX. */
/*Parameter: n. char *s' length. */
/*Return: int */

int shex2int(char *s, int n){
int r = 0;
for (int i=0; i<n; i++){
if(s[i]>='0' && s[i]<='9'){
r *= 16;
r +=s[i]-'0';
}else if(s[i]>='A' && s[i]<='F'){
r *= 16;
r += (s[i] - 'A') + 10;
}
}
return r;
}


/********************************************************************************/
/*Function: sanswer. Returns a String answer from mp3 UART module. */
/*Parameter:- uint8_t b. void. */
/*Return: String. If the answer is well formated answer. */

String sanswer(void)
{
uint8_t i = 0;
String mp3answer = "";

// Get only 10 Bytes
while (mp3.available() && (i < 10))
{
uint8_t b = mp3.read();
ansbuf[i] = b;
i++;

mp3answer += sbyte2hex(b);
}

// if the answer format is correct.
if ((ansbuf[0] == 0x7E) && (ansbuf[9] == 0xEF))
{
return mp3answer;
}

return "???: " + mp3answer;
}

First, we define all the program variables and the access register addresses of the YX5300 module.

#include <SoftwareSerial.h>

#define ARDUINO_RX 5 //should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6 //connect to RX of the module

SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX);

static int8_t Send_buf[8] = {0}; // Buffer for Send commands. // BETTER LOCALLY
static uint8_t ansbuf[10] = {0}; // Buffer for the answers. // BETTER LOCALLY

String mp3Answer; // Answer from the MP3.

String sanswer(void);
String sbyte2hex(uint8_t b);

/************ Command byte **************************/
#define CMD_NEXT_SONG 0X01 // Play next song.
#define CMD_PREV_SONG 0X02 // Play previous song.
#define CMD_PLAY_W_INDEX 0X03
#define CMD_VOLUME_UP 0X04
#define CMD_VOLUME_DOWN 0X05
#define CMD_SET_VOLUME 0X06

#define CMD_SNG_CYCL_PLAY 0X08 // Single Cycle Play.
#define CMD_SEL_DEV 0X09
#define CMD_SLEEP_MODE 0X0A
#define CMD_WAKE_UP 0X0B
#define CMD_RESET 0X0C
#define CMD_PLAY 0X0D
#define CMD_PAUSE 0X0E
#define CMD_PLAY_FOLDER_FILE 0X0F

#define CMD_STOP_PLAY 0X16 // Stop playing continuously.
#define CMD_FOLDER_CYCLE 0X17
#define CMD_SHUFFLE_PLAY 0x18 //
#define CMD_SET_SNGL_CYCL 0X19 // Set single cycle.

#define CMD_SET_DAC 0X1A
#define DAC_ON 0X00
#define DAC_OFF 0X01

#define CMD_PLAY_W_VOL 0X22
#define CMD_PLAYING_N 0x4C
#define CMD_QUERY_STATUS 0x42
#define CMD_QUERY_VOLUME 0x43
#define CMD_QUERY_FLDR_TRACKS 0x4e
#define CMD_QUERY_TOT_TRACKS 0x48
#define CMD_QUERY_FLDR_COUNT 0x4f

/************ Opitons **************************/
#define DEV_TF 0X02

/*********************************************************************/
int numero;
byte estado;
byte buzzer = 2;
byte pin = 0;
byte SortNumber = 0;

bool button = 0;

These register addresses are used to configure the module's operation. For example, see this registration address below.

#define CMD_PLAY_W_INDEX  0X03

Address 0x03 is defined with the name CMD_PLAY_W_INDEX. It is used to trigger a song from its number, that is, you enter the number of the sound and it will be played.

It will be with these values that we will use them and configure the functioning of our project.

After you have defined the various addresses that will be used, we will enter the setup function and configure the pins and serial communication for our project.

The void setup() function

Next, see the void setup function. I did all the settings of the button pins, serial communication of the MP3 module, and initialization of the card module in the MP3.

void setup()
{
Serial.begin(9600);
mp3.begin(9600);
delay(500);

for(pin = 8; pin < 13; pin++)
{
pinMode(pin, INPUT);
}

sendCommand(CMD_SEL_DEV, 0, DEV_TF);
delay(500);
}

I started the serial communication to print data on the computer's serial and then we started the serial communication through the mp3 object.

Serial.begin(9600);
mp3.begin(9600);
delay(500);

The mp3 module is controlled through commands received by the Arduino serial. In this process, we used the SoftwareSerial library and emulated a serial on the Arduino digital pins.

Thus, you will be able to use the Arduino to control the MP3 module through commands sent to it.

In addition, we made the configuration of the digital pins and initialization of the MP3 Card module.

for(pin = 8; pin < 13; pin++)
{
pinMode(pin, INPUT);
}

sendCommand(CMD_SEL_DEV, 0, DEV_TF);
delay(500);

After making the configuration, we must go to the main logic in the void loop function.

The main function void loop( )

The code is very simple and the entire logical structure is being presented below. In the following, I will explain to you the complete logic of the main function.

void loop()
{
pin = 8;

randomSeed(analogRead(A0));
numero = random(8,12);
SortNumber = numero;
numero = numero - 7;

Serial.println(numero);
sendCommand(0x03, 0, numero);
delay(1000);

do
{
button = digitalRead(pin);
Serial.println(button);
pin++;

if(pin > 13)
{
pin = 8;
}

Serial.println("Varrendo...");
Serial.println(pin);
//delay(1000);

}while(button != 1);

Serial.println("Saiu...");

if(button == 1 && (pin-1) != SortNumber)
{
sendCommand(0x03, 0, 6);
delay(3000);
}

if(button == 1 && (pin-1) == SortNumber)
{
sendCommand(0x03, 0, 7);
delay(3000);
}

// Check for the answer.
if (mp3.available())
{
Serial.println(decodeMP3Answer());
}
delay(100);
//Serial.println("Tocando musica...");
}

At each start of the loop function cycle, we will generate a new value between 8 and 12 to generate the sound of a vowel. The value from 8 to 12 refers to the digital pin of the vowel.

The code for generating the random value is shown below.

  pin = 8;

randomSeed(analogRead(A0));
numero = random(8,12);
SortNumber = numero;

In addition, we subtract 7 from the amount drawn between 8 and 12. This will allow us to point to the positions of 1 to 5 of the songs recorded on the memory card.

numero = numero - 7;

After that, I reproduced the sound of the vowel drawn on the line below.

sendCommand(0x03, 0, numero);
delay(1000);

Now the important time has come: the moment when we are going to read the button pressed by the child. The code portion is being presented below.

  do
{
button = digitalRead(pin);
Serial.println(button);
pin++;

if(pin > 13)
{
pin = 8;
}

Serial.println("Varrendo...");
Serial.println(pin);
//delay(1000);

}while(button != 1);

This loop will be executed until the user presses the buttons. The loop allows you to scan the 5 digital pins and the moment the child presses one of the buttons, it will come out of the loop and check if the child has responded correctly.

You will do the verification using the code below.

if(button == 1 && (pin-1) != SortNumber)
{
sendCommand(0x03, 0, 6);
delay(3000);
}

if(button == 1 && (pin-1) == SortNumber)
{
sendCommand(0x03, 0, 7);
delay(3000);
}

The first condition will be executed when the user makes a mistake because a button was pressed and the triggered value of the pin was different from the drawn pin (SortNumber).

At this point, you must execute the command below.

sendCommand(0x03, 0, 6);
delay(3000);

This command is used to trigger the wrong response tone. Finally, we have the second condition that will be used to check if the child is right.

if(button == 1 && (pin-1) == SortNumber)
{
sendCommand(0x03, 0, 7);
delay(3000);
}

If a button was pressed and the digital pin that was pressed is the same as the drawn pin, the system will trigger a correct answer sound.

As I explain to you, this code is very simple and will help any child develop their knowledge of vowels through a game with Arduino. In figure 7 the soundbox is executing the song stored in SD Card of the MP3 Module YX5300.

Through fun and entertaining activities, your students or your child can learn while playing. If you want to have fun with your students, get the board with vowels, and set up this project to have fun.

Conclusion

Classroom education needs to be changed constantly and the Arduino can be a great ally in creating fun tasks.

Through this project, it was possible to develop a simple activity that can develop children's skills through knowledge of the sound and spelling of each vowel.

Unlike traditional teaching methods, children will learn through classroom fun through games and electronics.

Acknowledgment

This project was developed thanks to the support and encouragement of the company JLCPCB. They encouraged education and invited us to develop the game of vowels for teaching children in the classroom.

If you want to purchase the electronic plates of the Game of Vowels, you can access this link and purchase 10 units for $ 2 at JLCPCB.

Code

Source CodeArduino
#include <SoftwareSerial.h>

#define ARDUINO_RX 5  //should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6  //connect to RX of the module

SoftwareSerial mp3(ARDUINO_RX, ARDUINO_TX);

static int8_t Send_buf[8] = {0}; // Buffer for Send commands.  // BETTER LOCALLY
static uint8_t ansbuf[10] = {0}; // Buffer for the answers.    // BETTER LOCALLY

String mp3Answer;           // Answer from the MP3.

String sanswer(void);
String sbyte2hex(uint8_t b);

/************ Command byte **************************/
#define CMD_NEXT_SONG     0X01  // Play next song.
#define CMD_PREV_SONG     0X02  // Play previous song.
#define CMD_PLAY_W_INDEX  0X03
#define CMD_VOLUME_UP     0X04
#define CMD_VOLUME_DOWN   0X05
#define CMD_SET_VOLUME    0X06

#define CMD_SNG_CYCL_PLAY 0X08  // Single Cycle Play.
#define CMD_SEL_DEV       0X09
#define CMD_SLEEP_MODE    0X0A
#define CMD_WAKE_UP       0X0B
#define CMD_RESET         0X0C
#define CMD_PLAY          0X0D
#define CMD_PAUSE         0X0E
#define CMD_PLAY_FOLDER_FILE 0X0F

#define CMD_STOP_PLAY     0X16  // Stop playing continuously. 
#define CMD_FOLDER_CYCLE  0X17
#define CMD_SHUFFLE_PLAY  0x18 //
#define CMD_SET_SNGL_CYCL 0X19 // Set single cycle.

#define CMD_SET_DAC 0X1A
#define DAC_ON  0X00
#define DAC_OFF 0X01

#define CMD_PLAY_W_VOL    0X22
#define CMD_PLAYING_N     0x4C
#define CMD_QUERY_STATUS      0x42
#define CMD_QUERY_VOLUME      0x43
#define CMD_QUERY_FLDR_TRACKS 0x4e
#define CMD_QUERY_TOT_TRACKS  0x48
#define CMD_QUERY_FLDR_COUNT  0x4f

/************ Opitons **************************/
#define DEV_TF            0X02

/*********************************************************************/
int numero;      
byte estado;      
byte buzzer = 2;
byte pin = 0;
byte SortNumber = 0;

bool button = 0;

void setup()
{
  Serial.begin(9600);
  mp3.begin(9600);
  delay(500);
  
  for(pin = 8; pin < 13; pin++)
  {
    pinMode(pin, INPUT);
  }
  
  sendCommand(CMD_SEL_DEV, 0, DEV_TF);
  delay(500);
}

void loop()
{
  pin = 8;

  randomSeed(analogRead(A0));
  numero = random(8,12);
  SortNumber =  numero;
  numero = numero - 7;
  
  Serial.println(numero);
  sendCommand(0x03, 0, numero);
  delay(1000);
  
  do
  {
     button = digitalRead(pin);
     Serial.println(button);
     pin++;

     if(pin > 13)
     {
        pin = 8;
     }

     Serial.println("Varrendo...");
     Serial.println(pin);
     //delay(1000);
     
  }while(button != 1);

  Serial.println("Saiu...");

  if(button == 1 && (pin-1) != SortNumber)
  {
    sendCommand(0x03, 0, 6);
    delay(3000);
  }

  if(button == 1 && (pin-1) == SortNumber)
  {
    sendCommand(0x03, 0, 7);
    delay(3000);
  }
  
  // Check for the answer.
  if (mp3.available())
  {
    Serial.println(decodeMP3Answer());
  }
  delay(100);
  //Serial.println("Tocando musica...");
}

/********************************************************************************/
/*Function sendMP3Command: seek for a 'c' command and send it to MP3  */
/*Parameter: c. Code for the MP3 Command, 'h' for help.                                                                                                         */
/*Return:  void                                                                */

void sendMP3Command(char c) {
  
  switch (c) {
    case '?':
    case 'h':
      Serial.println("HELP  ");
      Serial.println(" p = Play");
      Serial.println(" P = Pause");
      Serial.println(" > = Next");
      Serial.println(" < = Previous");
      Serial.println(" s = Stop Play"); 
      Serial.println(" + = Volume UP");
      Serial.println(" - = Volume DOWN");
      Serial.println(" c = Query current file");
      Serial.println(" q = Query status");
      Serial.println(" v = Query volume");
      Serial.println(" x = Query folder count");
      Serial.println(" t = Query total file count");
      Serial.println(" f = Play folder 1.");
      Serial.println(" S = Sleep");
      Serial.println(" W = Wake up");
      Serial.println(" r = Reset");
      break;

    case 'p':
      Serial.println("Play ");
      sendCommand(CMD_PLAY);
      break;

    case 'P':
      Serial.println("Pause");
      sendCommand(CMD_PAUSE);
      break;

    case '>':
      Serial.println("Next");
      sendCommand(CMD_NEXT_SONG);
      sendCommand(CMD_PLAYING_N); // ask for the number of file is playing
      break;

    case '<':
      Serial.println("Previous");
      sendCommand(CMD_PREV_SONG);
      sendCommand(CMD_PLAYING_N); // ask for the number of file is playing
      break;

    case 's':
      Serial.println("Stop Play");
      sendCommand(CMD_STOP_PLAY);
      break;

    case '+':
      Serial.println("Volume Up");
      sendCommand(CMD_VOLUME_UP);
      break;

    case '-':
      Serial.println("Volume Down");
      sendCommand(CMD_VOLUME_DOWN);
      break;

    case 'c':
      Serial.println("Query current file");
      sendCommand(CMD_PLAYING_N);
      break;

    case 'q':
      Serial.println("Query status");
      sendCommand(CMD_QUERY_STATUS);
      break;

    case 'v':
      Serial.println("Query volume");
      sendCommand(CMD_QUERY_VOLUME);
      break;

    case 'x':
      Serial.println("Query folder count");
      sendCommand(CMD_QUERY_FLDR_COUNT);
      break;

    case 't':
      Serial.println("Query total file count");
      sendCommand(CMD_QUERY_TOT_TRACKS);
      break;

    case 'f':
      Serial.println("Playing folder 1");
      sendCommand(CMD_FOLDER_CYCLE, 1, 0);
      break;

    case 'S':
      Serial.println("Sleep");
      sendCommand(CMD_SLEEP_MODE);
      break;

    case 'W':
      Serial.println("Wake up");
      sendCommand(CMD_WAKE_UP);
      break;

    case 'r':
      Serial.println("Reset");
      sendCommand(CMD_RESET);
      break;
  }
}

/********************************************************************************/
/*Function decodeMP3Answer: Decode MP3 answer.                                  */
/*Parameter:-void                                                               */
/*Return: The                                                  */

String decodeMP3Answer() {
  String decodedMP3Answer = "";

  decodedMP3Answer += sanswer();

  switch (ansbuf[3]) {
    case 0x3A:
      decodedMP3Answer += " -> Memory card inserted.";
      break;

    case 0x3D:
      decodedMP3Answer += " -> Completed play num " + String(ansbuf[6], DEC);
      //sendCommand(CMD_NEXT_SONG);
      //sendCommand(CMD_PLAYING_N); // ask for the number of file is playing
      break;

    case 0x40:
      decodedMP3Answer += " -> Error";
      break;

    case 0x41:
      decodedMP3Answer += " -> Data recived correctly. ";
      break;

    case 0x42:
      decodedMP3Answer += " -> Status playing: " + String(ansbuf[6], DEC);
      break;

    case 0x48:
      decodedMP3Answer += " -> File count: " + String(ansbuf[6], DEC);
      break;

    case 0x4C:
      decodedMP3Answer += " -> Playing: " + String(ansbuf[6], DEC);
      break;

    case 0x4E:
      decodedMP3Answer += " -> Folder file count: " + String(ansbuf[6], DEC);
      break;

    case 0x4F:
      decodedMP3Answer += " -> Folder count: " + String(ansbuf[6], DEC);
      break;
  }

  return decodedMP3Answer;
}

/********************************************************************************/
/*Function: Send command to the MP3                                             */
/*Parameter: byte command                                                       */
/*Parameter: byte dat1 parameter for the command                                */
/*Parameter: byte dat2 parameter for the command                                */

void sendCommand(byte command){
  sendCommand(command, 0, 0);
}

void sendCommand(byte command, byte dat1, byte dat2){
  delay(20);
  Send_buf[0] = 0x7E;    //
  Send_buf[1] = 0xFF;    //
  Send_buf[2] = 0x06;    // Len
  Send_buf[3] = command; //
  Send_buf[4] = 0x01;    // 0x00 NO, 0x01 feedback
  Send_buf[5] = dat1;    // datah
  Send_buf[6] = dat2;    // datal
  Send_buf[7] = 0xEF;    //
  Serial.print("Sending: ");
  for (uint8_t i = 0; i < 8; i++)
  {
    mp3.write(Send_buf[i]) ;
    Serial.print(sbyte2hex(Send_buf[i]));
  }
  Serial.println();
}



/********************************************************************************/
/*Function: sbyte2hex. Returns a byte data in HEX format.                       */
/*Parameter:- uint8_t b. Byte to convert to HEX.                                */
/*Return: String                                                                */


String sbyte2hex(uint8_t b)
{
  String shex;

  shex = "0X";

  if (b < 16) shex += "0";
  shex += String(b, HEX);
  shex += " ";
  return shex;
}


/********************************************************************************/
/*Function: shex2int. Returns a int from an HEX string.                         */
/*Parameter: s. char *s to convert to HEX.                                      */
/*Parameter: n. char *s' length.                                                */
/*Return: int                                                                   */

int shex2int(char *s, int n){
  int r = 0;
  for (int i=0; i<n; i++){
     if(s[i]>='0' && s[i]<='9'){
      r *= 16; 
      r +=s[i]-'0';
     }else if(s[i]>='A' && s[i]<='F'){
      r *= 16;
      r += (s[i] - 'A') + 10;
     }
  }
  return r;
}


/********************************************************************************/
/*Function: sanswer. Returns a String answer from mp3 UART module.          */
/*Parameter:- uint8_t b. void.                                                  */
/*Return: String. If the answer is well formated answer.                        */

String sanswer(void)
{
  uint8_t i = 0;
  String mp3answer = "";

  // Get only 10 Bytes
  while (mp3.available() && (i < 10))
  {
    uint8_t b = mp3.read();
    ansbuf[i] = b;
    i++;

    mp3answer += sbyte2hex(b);
  }

  // if the answer format is correct.
  if ((ansbuf[0] == 0x7E) && (ansbuf[9] == 0xEF))
  {
    return mp3answer;
  }

  return "???: " + mp3answer;
}

Comments

Similar projects you might like

Arduino Mp3 player Catalex

Project tutorial by javier muñoz sáez

  • 32,397 views
  • 9 comments
  • 32 respects

Thirst Alert Plant Alarm

Project tutorial by Patchr

  • 6,832 views
  • 3 comments
  • 42 respects

MOSMusic

Project tutorial by Victor Altamirano and EdOliver

  • 4,634 views
  • 10 comments
  • 18 respects

Minesweeper Game with Arduino

Project tutorial by Lima Moreira

  • 1,854 views
  • 1 comment
  • 1 respect
Add projectSign up / Login