I recommend you look at my example MP3 File Player and the Web Player.
There are TWO issues:
1) You need to approach this from the point of view appreciating the Arduino does not have enough resources (SRAM) to hold a list of entire SdFAT's directories. Hence my approach was to use the users console for retaining the list. It dumps the directories contents to the console, along with a corresponding number. From which the user could select the number they wish to enter. Similarly the Web Player does the same thing, but when generating the HTML, it generates a link pointing to the corresponding listed item. Hence the list is stored on the console being either the Browser or Serial Monitor.
2) The default provided SD library is not sufficient to do what you want. Recently Arduino incorporated Bill Greiman’s SdFatLib as the under the hood class. But limited it. Where using Bill’s native SdFat library allows you the use of additional methods to access individual objects, such as getFilename()
, not available in SD. This is necessary when going through the directory. The sd.ls(LS_DATE | LS_SIZE)
will only dump directly to serial. Where you need to use access the individual files themselves. As show below or in actual code
SdFile file;
char filename[13];
sd.chdir("/",true);
uint16_t count = 1;
while (file.openNext(sd.vwd(),O_READ))
{
file.getFilename(filename);
Serial.print(count);
Serial.print(F(": "));
Serial.println(filename);
count++;
}
file.close();
Additionally there are buried public methods accessible by references as show in WebPlayer’s ListFiles() function, to get more discrete handling of the files.