Hello, Sir.
I am trying to create functions for recording and playing MP3 files. I am using an Arduino Uno with a VS1053 shield. However, when I try to play a recorded MP3 from the SD card using the VS1053 shield, it doesn’t work.
This is my code. *********************************************************
#include <SPI.h>
#include <SdFat.h>
#include <SFEMP3Shield.h>
SdFat sd;
SFEMP3Shield MP3player;
const char* TARGET_FILE = "1.mp3";
char actualFileName[13]; // Variables to store file names found in real SD
bool fileFound = false;
byte volumeState = 30;
#define VOLUME_PIN A0
// [Add] A function that lists all the files on the SD card and checks if you have TARGET_FILE
void listAndFindFile() {
SdFile root;
SdFile entry;
root.open("/");
Serial.println(F("--- [SD Card File List] ---"));
while (entry.openNext(&root, O_RDONLY)) {
char name[13];
entry.getName(name, sizeof(name));
Serial.print(F("File found: "));
Serial.println(name);
// Find the name
if (strcasecmp(name, TARGET_FILE) == 0) {
strcpy(actualFileName, name);
fileFound = true;
}
entry.close();
}
Serial.println(F("---------------------------"));
if (fileFound) {
Serial.print(F("Match Found! Using file: "));
Serial.println(actualFileName);
} else {
Serial.println(F("ERROR: Target file NOT FOUND on SD card."));
}
root.close();
}
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println(F("Initializing SD & MP3 Shield..."));
// 1. Init SD card
if (!sd.begin(9, SPI_HALF_SPEED)) {
Serial.println(F("SD initialization failed!"));
sd.initErrorPrint();
while (1);
}
Serial.println(F("SD Card OK."));
// 2. Print File List
listAndFindFile();
if (!fileFound) {
Serial.println(F("Please check your SD card and restart."));
while (1); // 파일을 못 찾으면 여기서 중단
}
// 3. Init MP3 Player
uint8_t result = MP3player.begin();
if (result != 0) {
Serial.print(F("VS1053 Error Code: "));
Serial.println(result);
while (1);
}
Serial.println(F("VS1053 OK."));
MP3player.setVolume(volumeState, volumeState);
// 4. Play with a founded record.
result = MP3player.playMP3(actualFileName);
if (result != 0) {
Serial.print(F("Play Error: "));
Serial.println(result);
}
}
void loop() {
// When the song is over, repeat it
if (!MP3player.isPlaying()) {
MP3player.playMP3(actualFileName);
}
// Control Volume
static uint32_t lastVolTime = 0;
if (millis() - lastVolTime > 200) {
int potValue = analogRead(VOLUME_PIN);
byte newVol = map(potValue, 0, 1023, 10, 100);
if (abs(newVol - volumeState) > 2) {
volumeState = newVol;
MP3player.setVolume(volumeState, volumeState);
}
lastVolTime = millis();
}
}
This is the result **************************************************
17:19:15.505 -> --- [SD Card File List] ---
17:19:15.537 -> File found: System Volum
17:19:15.569 -> File found: 1.mp3
17:19:15.569 -> File found: 2.mp3
17:19:15.601 -> File found: 3.mp3
17:19:15.633 -> File found: 4.mp3
17:19:15.633 -> ---------------------------
17:19:15.666 -> Match Found! Using file: 1.mp3
17:19:15.881 -> VS1053 Error Code: 6.
I also use SFEMP3SHIELD FilePlayer Example. But it also not working when I choose the mp3. How can I solve this problem.
