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.
How to read SD card file using an array of filenames
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- D4VE777
- Posts: 11
- Joined: Sat Aug 28, 2021 5:58 pm
- dastels
- Posts: 15831
- Joined: Tue Oct 20, 2015 3:22 pm
Re: How to read SD card file using an array of filenames
"Doesn't work" ... that doesn't help much. What actually happens?
Dave
Dave
- D4VE777
- Posts: 11
- Joined: Sat Aug 28, 2021 5:58 pm
Re: How to read SD card file using an array of filenames
Hi dastels, the program just hangs up there. I have a print statement after that, and it never gets there.
- dastels
- Posts: 15831
- Joined: Tue Oct 20, 2015 3:22 pm
Re: How to read SD card file using an array of filenames
OK. This works fine:
Outputing:
So it isn't the array.
I would see if an exception is being thrown from SD.open.
Dave
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");
}
Code: Select all
Starting
BOOKNAM1.TXT
BOOKNAM2.TXT
BOOKNAM3.TXT
Done
I would see if an exception is being thrown from SD.open.
Dave
- D4VE777
- Posts: 11
- Joined: Sat Aug 28, 2021 5:58 pm
Re: How to read SD card file using an array of filenames
Thanks for your help so far, dastels. Where can I go to learn how to check for SD.open exceptions?
- dastels
- Posts: 15831
- Joined: Tue Oct 20, 2015 3:22 pm
Re: How to read SD card file using an array of filenames
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
Dave
- D4VE777
- Posts: 11
- Joined: Sat Aug 28, 2021 5:58 pm
Re: How to read SD card file using an array of filenames
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.
Please be positive and constructive with your questions and comments.