How to read SD card file using an array of filenames

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
D4VE777
 
Posts: 11
Joined: Sat Aug 28, 2021 5:58 pm

How to read SD card file using an array of filenames

Post by D4VE777 »

I am using the Feather Adalogger 32u4. I need to store a list of filenames in flash, then based on the user push-button response, open and read a certain file on the SD card.

I am able to read files from the SD card using the direct approach:
File myFile;
myFile = SD.open("BOOKNAM1.TXT", FILE_READ);

And the indirect approach also works:
File myFile;
char* BookFilename = "BOOKNAM1.TXT";
myFile = SD.open(BookFilename, FILE_READ);

But I cannot read a file from the SD card using an array of filenames that are stored in flash memory (for example):
const char BookFilename[3][13] PROGMEM = {"BOOKNAM1.TXT","BOOKNAM2.TXT","BOOKNAM3.TXT"};
File myFile;
myFile = SD.open(BookFilename[0], FILE_READ); // Doesn't work!

If I move the array to RAM, it takes up more RAM (4.5K) than the processor has (32u4 = 2.5K).
I have searched, but haven't found a similar issue. Thank you.

User avatar
dastels
 
Posts: 15831
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to read SD card file using an array of filenames

Post by dastels »

"Doesn't work" ... that doesn't help much. What actually happens?

Dave

User avatar
D4VE777
 
Posts: 11
Joined: Sat Aug 28, 2021 5:58 pm

Re: How to read SD card file using an array of filenames

Post by D4VE777 »

Hi dastels, the program just hangs up there. I have a print statement after that, and it never gets there.

User avatar
dastels
 
Posts: 15831
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to read SD card file using an array of filenames

Post by dastels »

OK. This works fine:

Code: Select all

const char BookFilename[3][13] PROGMEM = {"BOOKNAM1.TXT", "BOOKNAM2.TXT", "BOOKNAM3.TXT"};

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Starting");
  Serial.println(BookFilename[0]);
  Serial.println(BookFilename[1]);
  Serial.println(BookFilename[2]);
  Serial.println("Done");
}

Outputing:

Code: Select all

Starting
BOOKNAM1.TXT
BOOKNAM2.TXT
BOOKNAM3.TXT
Done
So it isn't the array.

I would see if an exception is being thrown from SD.open.

Dave

User avatar
D4VE777
 
Posts: 11
Joined: Sat Aug 28, 2021 5:58 pm

Re: How to read SD card file using an array of filenames

Post by D4VE777 »

Thanks for your help so far, dastels. Where can I go to learn how to check for SD.open exceptions?

User avatar
dastels
 
Posts: 15831
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to read SD card file using an array of filenames

Post by dastels »

Ah. https://www.arduino.cc/en/Reference/SDopen it returns false if there's a problem. Have a look at the example for reading: https://www.arduino.cc/en/Tutorial/Libr ... s/DumpFile.

Dave

User avatar
D4VE777
 
Posts: 11
Joined: Sat Aug 28, 2021 5:58 pm

Re: How to read SD card file using an array of filenames

Post by D4VE777 »

Thanks dastels, that was good to know. I figured out why I was able to use the array information stored in RAM to read files from the SD card, but NOT able to read the array stored in flash. It is because of PROGMEM. After an array is declared to be in PROGMEM, you have to use a completely different set of instructions to retrieve the data from the array.

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”