Code for playing random MP3 files from Catalex Serial Mp3 Player Module

I have set up a Catalex Serial Mp3 Player Module to play a chosen MP3 file triggered by one distance sensor using the code from this page: Arduino Mp3 player + Distance sensor = FUN - Arduino Project Hub

However, I would like to make the file that it plays each time to be randomly chosen (from one of 8 files stored on the Mini SD card). There's some guidance here - Help with understanding the example code of a Serial MP3 player module. - Programming Questions - Arduino Forum - the second last post on the page (before my request for help!) but as a newbie, I can't work out how to merge that code with the code I am currently using. Any help or tips would be much appreciated.

I have used the Catalex Serial Mp3 Player Module for several projects, but I did not need a random play function yet.
According to the manual of the player there is a command called "Shuffle Play". I could imagine that this is just the function you need.
The command to start shuffle play is: 7E FF 06 18 00 00 00 EF
So 0x18 starts shuffle play.

If you put it in the code of your link it would look something like this:

sendCommand(0x18, 0x00);   // 18 is shuffle play

I have no Catalex player with me at the moment, so I can not test it under real conditions.
Maybe after the "shuffle pay" command you have to start playing by:

sendCommand(0x0D, 0x00);   // play

Other option
Not using "shuffel play" but just using the Arduino command random:

int randomTrack = random(1, 9);   // will give random number between 1....8
sendCommand(0x03, randomTrack);

Just try it :slight_smile:

If you would post your code it would be much more easy to help.
So post your code!

Thank you. Really helpful. I got it working from using your example and another example (although) some tracks don’t seem to play but the serial monitor shows that they have been selected, so I need to check the encoding of those). The code I used in the end was this:

#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

#define trigPin 13//for the distance module
#define echoPin 12

SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);//init the serial protocol, tell to myserial wich pins are TX and RX

////////////////////////////////////////////////////////////////////////////////////
//all the commands needed in the datasheet(http://geekmatic.in.ua/pdf/Catalex_MP3_board.pdf)
static int8_t Send_buf[8] = {0} ;//The MP3 player undestands orders in a 8 int string
//0X7E FF 06 command 00 00 00 EF;(if command =01 next song order)
#define NEXT_SONG 0X01
#define PREV_SONG 0X02

#define CMD_PLAY_W_INDEX 0X03 //DATA IS REQUIRED (number of song)

#define VOLUME_UP_ONE 0X04
#define VOLUME_DOWN_ONE 0X05
#define CMD_SET_VOLUME 0X06//DATA IS REQUIRED (number of volume from 0 up to 30(0x1E))
#define SET_DAC 0X17
#define CMD_PLAY_WITHVOLUME 0X22 //data is needed 0x7E 06 22 00 xx yy EF;(xx volume)(yy number of song)

#define CMD_SEL_DEV 0X09 //SELECT STORAGE DEVICE, DATA IS REQUIRED
#define DEV_TF 0X02 //HELLO,IM THE DATA REQUIRED

#define SLEEP_MODE_START 0X0A
#define SLEEP_MODE_WAKEUP 0X0B

#define CMD_RESET 0X0C//CHIP RESET
#define CMD_PLAY 0X0D //RESUME PLAYBACK
#define CMD_PAUSE 0X0E //PLAYBACK IS PAUSED

#define CMD_PLAY_WITHFOLDER 0X0F//DATA IS NEEDED, 0x7E 06 0F 00 01 02 EF;(play the song with the directory \01\002xxxxxx.mp3

#define STOP_PLAY 0X16

#define PLAY_FOLDER 0X17// data is needed 0x7E 06 17 00 01 XX EF;(play the 01 folder)(value xx we dont care)

#define SET_CYCLEPLAY 0X19//data is needed 00 start; 01 close

#define SET_DAC 0X17//data is needed 00 start DAC OUTPUT;01 DAC no output
////////////////////////////////////////////////////////////////////////////////////

void setup()
{
Serial.begin(9600);//Start our Serial coms for serial monitor in our pc
mySerial.begin(9600);//Start our Serial coms for THE MP3
delay(500);//Wait chip initialization is complete
sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
delay(200);//wait for 200ms
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}

void loop()
{
if(measureDistance(trigPin,echoPin)<10){
int randTrackNo;
randTrackNo = random(1, 9);
sendCommand(0x03,randTrackNo);
Serial.print(“Playing track:”);
Serial.println(randTrackNo);
delay(1000);//wait to avoid errors
}
delay(300);
}

void sendCommand(int8_t command, int16_t dat)
{
delay(20);
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0xff; //version
Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
Send_buf[3] = command; //
Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
Send_buf[5] = (int8_t)(dat >> 8);//datah
Send_buf[6] = (int8_t)(dat); //datal
Send_buf[7] = 0xef; //ending byte
for(uint8_t i=0; i<8; i++)//
{
mySerial.write(Send_buf*) ;//send bit to serial mp3*
Serial.print(Send_buf*,HEX);//send bit to serial monitor in pc*
}
Serial.println();
}
long measureDistance(int trigger,int echo){
* long duration, distance;*

* digitalWrite(trigger, LOW); //PULSE |—|
_
delayMicroseconds(2);_
_
digitalWrite(trigger, HIGH);_
_
delayMicroseconds(10);_
_
digitalWrite(trigger, LOW);*_

* duration = pulseIn(echo, HIGH);*
* distance = (duration/2) / 29.1;*
* Serial.println(“distance:”);*
* Serial.println(distance);*
* return distance;*
}

Have realised that it only seems to play the odd numbered tracks 1,3,5 & 7). Any thoughts why this might be?

I have made a setup with:

  • Arduino UNO
  • Catalex Serial Mp3 Player Module
  • Ultrasonic Distance Sensor

I put 8 audio tracks on SD card (mp3, 44.1 kHz, 192 kbps constant bitrate, Stereo).
I used your code from #2.

It workes out of the box:
If an object comes closer than about 10 cm to the distance sensor, it will play a random track.
All 8 tracks are played (sooner or later).

So I can not reproduce your problem - sorry :slight_smile:
Your code ist just working … :slight_smile:

Thoughts:
If you use “random” without randomSeed, the random numbers will always have the same sequence.
To get “some more randomness” you can put this line of code in setup:

randomSeed(analogRead(A0));   // use an unconnected analog pin

The forum software interprets some sequences of characters as formatting or special characters,
like: Send_buf[5] = (int8_t)(dat >> 8);
Also the [i] is missing 2 times in the for loop (as it is interpreted as “switch to italic”).

So it is usfull to use code tags when posting code. Code tags will prevent these effects.
You can insert code tags by clicking this icon </> in the upper left corner of the editor
or just by writing [code] at the beinning of your code and
[/code] after the end of your code.

I will post your code with code tags. I already added “randomSeed” and put the [i] on the right place:

#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

#define trigPin 13  //for the distance module
#define echoPin 12

SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);//init the serial protocol, tell to myserial wich pins are TX and RX

////////////////////////////////////////////////////////////////////////////////////
//all the commands needed in the datasheet(http://geekmatic.in.ua/pdf/Catalex_MP3_board.pdf)
uint8_t Send_buf[8] = {0} ;//The MP3 player undestands orders in a 8 int string
//0X7E FF 06 command 00 00 00 EF;(if command =01 next song order)
#define NEXT_SONG 0X01
#define PREV_SONG 0X02

#define CMD_PLAY_W_INDEX 0X03 //DATA IS REQUIRED (number of song)

#define VOLUME_UP_ONE 0X04
#define VOLUME_DOWN_ONE 0X05
#define CMD_SET_VOLUME 0X06//DATA IS REQUIRED (number of volume from 0 up to 30(0x1E))
#define SET_DAC 0X17
#define CMD_PLAY_WITHVOLUME 0X22 //data is needed  0x7E 06 22 00 xx yy EF;(xx volume)(yy number of song)

#define CMD_SEL_DEV 0X09 //SELECT STORAGE DEVICE, DATA IS REQUIRED
#define DEV_TF 0X02 //HELLO,IM THE DATA REQUIRED

#define SLEEP_MODE_START 0X0A
#define SLEEP_MODE_WAKEUP 0X0B

#define CMD_RESET 0X0C//CHIP RESET
#define CMD_PLAY 0X0D //RESUME PLAYBACK
#define CMD_PAUSE 0X0E //PLAYBACK IS PAUSED

#define CMD_PLAY_WITHFOLDER 0X0F//DATA IS NEEDED, 0x7E 06 0F 00 01 02 EF;(play the song with the directory \01\002xxxxxx.mp3

#define STOP_PLAY 0X16

#define PLAY_FOLDER 0X17// data is needed 0x7E 06 17 00 01 XX EF;(play the 01 folder)(value xx we dont care)

#define SET_CYCLEPLAY 0X19//data is needed 00 start; 01 close

#define SET_DAC 0X17//data is needed 00 start DAC OUTPUT;01 DAC no output
////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);  //Start our Serial coms for serial monitor in our pc
  mySerial.begin(9600);  //Start our Serial coms for THE MP3
  delay(500);  //Wait chip initialization is complete
  sendCommand(CMD_SEL_DEV, DEV_TF);  //select the TF card
  delay(200);  //wait for 200ms
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  randomSeed(analogRead(A0));
}

void loop() {
  if (measureDistance(trigPin, echoPin) < 10) {
    int randTrackNo;
    randTrackNo = random(1, 9);
    sendCommand(0x03, randTrackNo);
    Serial.print("Playing track:");
    Serial.println(randTrackNo);
    delay(1000);  //wait to avoid errors
  }
  delay(300);
}

void sendCommand(int8_t command, int16_t dat) {
  delay(20);
  Send_buf[0] = 0x7e; //starting byte
  Send_buf[1] = 0xff; //version
  Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;  //0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);  //datah
  Send_buf[6] = (int8_t)(dat);  //datal
  Send_buf[7] = 0xef;  //ending byte
  for (uint8_t i = 0; i < 8; i++) {
    mySerial.write(Send_buf[i]) ;  //send bit to serial mp3
    Serial.print(Send_buf[i], HEX);  //send bit to serial monitor in pc
    Serial.print(" ");
  }
  Serial.println();
}

long measureDistance(int trigger, int echo) {
  long duration, distance;

  digitalWrite(trigger, LOW);  //PULSE ___|---|___
  delayMicroseconds(2);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);

  duration = pulseIn(echo, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print("distance: ");
  Serial.println(distance);
  return distance;
}

(You could also edit your post #2 and add code tags afterwards.)

That's amazing uxomm. I will try this tonight. Thank you. Not sure why it only played odd numbered tracks, but glad it works for you.

Hello I have the same problem. he plays odd :confused:

#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

#define trigPin 13  //for the distance module
#define echoPin 12

SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);//init the serial protocol, tell to myserial wich pins are TX and RX

#define NEXT_SONG 0X01
#define PREV_SONG 0X02

#define CMD_PLAY_W_INDEX 0X03 //DATA IS REQUIRED (number of song)

#define VOLUME_UP_ONE 0X04
#define VOLUME_DOWN_ONE 0X05
#define CMD_SET_VOLUME 0X06//DATA IS REQUIRED (number of volume from 0 up to 30(0x1E))
#define SET_DAC 0X17
#define CMD_PLAY_WITHVOLUME 0X22 //data is needed  0x7E 06 22 00 xx yy EF;(xx volume)(yy number of song)

#define CMD_SEL_DEV 0X09 //SELECT STORAGE DEVICE, DATA IS REQUIRED
#define DEV_TF 0X02 //HELLO,IM THE DATA REQUIRED

#define SLEEP_MODE_START 0X0A
#define SLEEP_MODE_WAKEUP 0X0B

#define CMD_RESET 0X0C//CHIP RESET
#define CMD_PLAY 0X0D //RESUME PLAYBACK
#define CMD_PAUSE 0X0E //PLAYBACK IS PAUSED

#define CMD_PLAY_WITHFOLDER 0X0F//DATA IS NEEDED, 0x7E 06 0F 00 01 02 EF;(play the song with the directory \01\002xxxxxx.mp3

#define STOP_PLAY 0X16

#define PLAY_FOLDER 0X17// data is needed 0x7E 06 17 00 01 XX EF;(play the 01 folder)(value xx we dont care)

#define SET_CYCLEPLAY 0X19//data is needed 00 start; 01 close

#define SET_DAC 0X17//data is needed 00 start DAC OUTPUT;01 DAC no output
////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);  //Start our Serial coms for serial monitor in our pc
  mySerial.begin(9600);  //Start our Serial coms for THE MP3
  delay(500);  //Wait chip initialization is complete
  sendCommand(CMD_SEL_DEV, DEV_TF);  //select the TF card
  delay(200);  //wait for 200ms
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  randomSeed(analogRead(A0));
}

void loop() {
  if (measureDistance(trigPin, echoPin) < 10) {
    int randTrackNo;
    randTrackNo = random(1, 73);
    sendCommand(0x03, randTrackNo);
    Serial.print("Playing track:");
    Serial.println(randTrackNo);
    delay(1000);  //wait to avoid errors
  }
  delay(300);
}

void sendCommand(int8_t command, int16_t dat) {
  delay(20);
  Send_buf[0] = 0x7e; //starting byte
  Send_buf[1] = 0xff; //version
  Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;  //0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);  //datah
  Send_buf[6] = (int8_t)(dat);  //datal
  Send_buf[7] = 0xef;  //ending byte
  for (uint8_t i = 0; i < 8; i++) {
    mySerial.write(Send_buf[i]) ;  //send bit to serial mp3
    Serial.print(Send_buf[i], HEX);  //send bit to serial monitor in pc
    Serial.print(" ");
  }
  Serial.println();
}

long measureDistance(int trigger, int echo) {
  long duration, distance;

  digitalWrite(trigger, LOW);  //PULSE ___|---|___
  delayMicroseconds(2);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);

  duration = pulseIn(echo, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print("distance: ");
  Serial.println(distance);
  return distance;
}

emorej:
Hello I have the same problem. he plays odd :confused:

Can you please elaborate: what do you mean by "odd".

  1. Try to get to work the distance sensor only (without mp3 player).
  2. Try to get to work the mp3 player only.
    You can find basic examples for both devices.
  3. Try to combine distance sensor and mp3 player.