I modified a function from the UTFT lib to use SdFat, and a sample sketch is attached. It is supposed to load a map tile. It works, but at some random point will quit loading and return 65535. Perhaps I'm not using SdFat correctly. Is there a different method rather than reading one sector at a time that would work better? Thanks for any help.
I changed the sketch to use SdFile, not SdBaseFile, and to print the result of myFile.read. It is always 0, until the sketch crashes, then is 65535. I believe the problem is how I'm reading the file data, not the display code as that is lifted from UTFT and works with that. The function needs to reliably read binary data, 512 bytes at a time, and I don't understand why this works for some random length of time, then stops. I hope that someone knowledgable in SdFat will look at this code. Thanks very much if it is you.
Thank you for helping. I fixed the (stupid) errors you pointed out, and simplified the sketch to remove all references to the display (attached). It should now open the file, read repeatedly into the buffer, then close the file, moving on to the next.
When I run the attached sketch, it will open and print the names of the files up to some random file, then will print file error and return 0. So there is apparently some problem with
res=myFile.open(mapname, O_READ);
I tried setting the SPI speed to half and quarter, and used the SdFat formatting tools to reformat the card, with no joy. Am I using the open command correctly? Could it be the card? (2GB, 50X Kingston brand) I commented out the section to stop the sketch if a file did not open, and learned that once open fails, it will not work again.
Add the following to check the nature of the open error.
else {
//file did not open
// Add these three lines
Sd.errorPrint(mapname);
Serial.print("exists: ");
Serial.println(Sd.exists(mapname));
return res;
}
It should print something like this if you are opening "TEST.BIN".
error: TEST.BIN
SD errorCode: 0X19,0X0
exists: 0
The SD errorCode line will only appear if there is a hardware problem with the SD.
I swapped cards for one by Transend, and I'm happy to say it's working; maps have been loading for more than an hour without a crash. Given my level of coding ability, it probably would not have occurred to me that it might be a hardware problem. Thank you again for your assistance.