I have stumbled upon a problem that I can't get past and need you assistance. I have been reading everything I could search for and have not found a solution although I am fairly certain it is something trivial.
On setup I am creating a file on the SD card if it doesn't exist which is working fine. Later on I want to get a list of file in the main directory. The issue is that when I create a file in setup and then later try to get a list of the files the files will not list. However, if I comment out the portion in Setup that creates the file I can get a list of files without a problem.
I have modified the listfiles example below to illustrate my point. What am I doing wrong. It's almost like even though I am closing the SD open file it is still open.
#include <SD.h>
File root;
File dataFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
dataFile = SD.open("TEST.CSV", O_CREAT | O_APPEND | O_WRITE | O_READ); //if I comment this out then the list files works
dataFile.close();
Serial.println("Created File: TEST.CSV");
}
void loop()
{
dataFile = SD.open("/");
printDirectory(dataFile, 0);
Serial.println("done!");
delay(1000);
dataFile.close();
}
void printDirectory(File dir, int numTabs) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
Use this to locate files and try to remove stuff which you don't need
File dataFile;
Sd2Card card;
SdVolume volume;
SdFile root;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
pinMode(10, OUTPUT);
if (!card.init(SPI_HALF_SPEED, 4)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card is inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
dataFile = SD.open("examlpe.CSV", FILE_WRITE); //if I comment this out then the list files works
if (dataFile) {
Serial.print(F("Writing to test.txt..."));
dataFile.println("NOW this FILE CREATED");
dataFile.close();
Serial.println(F("done."));
}
Serial.println("Created File: examlpe.CSV");
// re-open the file for reading:
dataFile = SD.open("examlpe.CSV");
if (dataFile) {
Serial.println("examlpe.CSV:");
// read from the file until there's nothing else in it:
while (dataFile.available()) {
Serial.write(dataFile.read());
}
// close the file:
dataFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}
and it prints the "Success" message to the serial monitor, which leads me to believe that the file was removed. However, when I run the "listfiles" sketch from the Arduino SD Library Examples, it says that the file I tried to delete is there with some filesize.
Does SD.remove actually remove the file, and all its contents from the SD card? This may seem like a dumb question, but I've had a reasonable amount of success with the other SD functions. Remove is just not working like I expect it to.