I have about the same problem but not with the same case... datalogger example works perfect for me. It creates the file and store data inside.
However, when i use my own code, it creates the file, but does not write data on it ("error opening data file").
This is the code of the procedure containg the file management:
void startSD(){
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
// Check and/or create the logdata file on SD Card
if (!SD.exists(filename1)) {
Serial.println("Data file does not exist on SD card");
datafile = SD.open(filename1, FILE_WRITE);
if (datafile){
datafile.println("Header1");
datafile.println("Header2");
datafile.println();
datafile.println("Header3");
datafile.close();
Serial.println("'Data.csv' header is ready");
}
else {
Serial.println("error opening data file");
}
Serial.println("'Data.csv' file has been created");
}
if (! SD.exists(filename1)) {
error("couldn't create the file");
}
}
I call it from SETUP section, and, of course, datafile and filename1 are defined at the header.
All the pther part of my program works great, except this part...
This is the result on serial port:
card initialized.
Data file does not exist on SD card
error opening data file
'Data.csv' file has been created
Could you see what i am doing wrong with this code?
Thanks!