I've put some files in a folder on my SD card. I've wrote a function using the folder name, and now my goal is to be able to select a random file then play it on the Wave Shield.
Unfortunatly, I meet a problem with my algorythm:
Code: Select all
dir_t dirBuf;
int folderSize = 0;
while (folder.readDir(dirBuf) > 0)
{
folderSize ++;
}
folder.rewind();
int randomFile = random(1, folderSize+1); //random() exclude the max value
int selected = 1; //There is at least 1 file to play
while (folder.readDir(dirBuf) > 0)
{
if (selected == randomFile)
{
/*
* Now, translate the dirBuf.name (8.3 format) into
* a char array comaptible with FatReader open() function
*
* Count real characters contained in dirBuf.name:
* 8.3 mean 8 characters for name and 3 for extension
* if the file name is under 8 characters, some spaces are added
*/
int fileNameLength = 1; //dirBuf.name does not contain the dot
for (int i = 0; i < 11; i++) //11 because a 8.3 file name has 8+3 characters
{
if (dirBuf.name[i] == ' ')
continue;
else
fileNameLength++;
}
char fileName[fileNameLength];
int letters = 0;
for (int i = 0; i < 11; i++)
{
if (dirBuf.name[i] == ' ')
continue;
if (i == 8)
{
fileName[letters] = '.';
letters++;
}
fileName[letters] = dirBuf.name[i];
letters++;
}
Serial.println(sizeof(fileName)/sizeof(char));
Serial.println(fileName);
if (!file.open(folder, fileName))
{
error("open the file has failed");
}
break;
}
selected++;
}
Code: Select all
Searching for Serial monitor (picocom) ... /usr/bin/picocom
Guessing serial port ... /dev/ttyACM0
picocom v1.7
port is : /dev/ttyACM0
flowcontrol : none
baudrate is : 9600
parity is : none
databits are : 8
escape is : C-a
local echo is : no
noinit is : no
noreset is : no
nolock is : yes
send_cmd is : sz -vv
receive_cmd is : rz -vv
imap is :
omap is :
emap is : crcrlf,delbs,
Terminal ready
8
R2_D.WAVR2_D WAV
Error: open the file has failed
I've tried many things, like putting a println just before and after (to see if a loop could be the reason), or making my own "for()" instead of using println on the array.
I can't understand what happening, could you help me?