Hello, it's me once again!
I've made a stereo WAV player with almost just my Arduino board; the only extra is an SD card module.
I'm using the TMRpcm library to accomplish this; and it playbacks the audio files very well (even the stereo ones), no complains for now.
Now I tried that, but playing all the files in a single directory (folder), one-by-one (like a MP3 player does). Now here's the problem: the player is in the 13th song (file); but when it attempts to go further, the board resets itself if the previous song plays until the end; or "freezes" and makes a funny (and very very loud) noise in the audio outputs, if I skip that 13th song. Clarifying: all the directory's files are valid for the library I'm using.
In conclusion, that's definitely a software problem, not a hardware problem. And so, here's my current sketch of that audio player I was talking about.
/*
This sketch demonstrates playing a stereo audio track using a single timer.
16-24kHz sample rate recommended
Steps:
1. Edit pcmConfig.h
a: On Uno or non-Mega boards, uncomment #define buffSize 128. May need to increase or lower sample rate.
b: Uncomment #define STEREO_OR_16BIT
2. Usage is as below. See https://github.com/TMRh20/TMRpcm/wiki/Advanced-Features#wiki-stereo-and-16-bit-playback for
additional information.
*/
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
//#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 4 //using digital pin 4 on arduino nano 328, can use other pins
TMRpcm audio; // create an object for use in this sketch
File songDir;
const char musicPath[] = "/music/";
const byte nextButton = 2;
void setup() {
audio.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
pinMode(10, OUTPUT); //Pin pairs: 9,10 Mega: 5-2,6-7,11-12,46-45
pinMode(nextButton, INPUT);
if (!SD.begin(SD_ChipSelectPin)) {
while (true) {} // Do nothing until reset
}
songDir = SD.open("/music");
if (! playFirstSong()) {
while (true) {} // Do nothing until reset
}
}
void loop() {
if (digitalRead(nextButton) == HIGH) {
playNextSong();
delay(300); // De-bouncer delay
}
if (! audio.isPlaying()) { // Plays the next song automatically when the current one finishes.
playNextSong();
}
}
boolean playFirstSong() {
File song = songDir.openNextFile(); // Open the first file of the directory
if (! song) { // If no file in the directory (even a "invalid" file will bypass the validation)
return false;
}
while (song.isDirectory()) { // Keep going until we find an actual file
song = songDir.openNextFile();
if (! song) {
return false;
}
}
String name = String(musicPath);
name.concat(song.name()); // I don't know how to concatenate character arrays directly
byte length = name.length() + 1;
char finalName[length];
name.toCharArray(finalName, length);
audio.play(finalName);
// This function only receives a character array (file's path) instead of a File object, which is annoying
// when dealing with a "dynamic" file list
return true;
}
void playNextSong() {
if (audio.isPlaying()) { // I don't know if the TMRpcm player would "overlap" one song to another.
audio.stopPlayback();
}
File song = songDir.openNextFile();
if (! song) { // If no more files in the directory
songDir.rewindDirectory(); // Go back to the first file
song = songDir.openNextFile(); // May this skip the very first file in the directory?
}
while (song.isDirectory()) {
song = songDir.openNextFile();
if (! song) {
songDir.rewindDirectory();
song = songDir.openNextFile();
}
}
String name = String(musicPath);
name.concat(song.name());
byte length = name.length() + 1;
char finalName[length];
name.toCharArray(finalName, length);
audio.play(finalName);
}
I know, there's at least one thing to optimize. I got a big question:
Does the "objects" created in a function get actually "destroyed" when that function finishes?
If the answer to my question is "yes", then probably is the SD card's data corrupted?
If the answer is "no", then how to destroy those objects in order to always have free space?
I'm pretty sure that the auto-resetting and the "freezing" are caused by running out of memory (RAM).
Any help will be appreciated!