New to Arduino and am struggling to get logging to SD card working. I've tried everything - really need some help.
I'm using a freetronics etherten and trying to use the standard SD library to print log messages to a txt file in the root folder of an SD card. If I open the file, print the message and close the file, it works fine - however I'm thinking that's not very efficient to close and open the file all the time, so what I wanted to do was print log messages and then only periodically flush them to the card. The issue is flush isn't working for me (when I reset the program no data is written to the card even after calling flush - the file has been created but it's just empty). The flush implementation in SD library returns void (as it needs to comply with the stream implementation), however i've found the underlying operation is failing (put print statements in the code to trace back my issue:
SdFile::sync is returning false, due to this:
dir_t* d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
if (!d) return false;
And so I've traced it through to find the issue is in Sd2Card::writeData
Basically this section:
status_ = spiRec();
if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) ....
status_ for me is 0 so the write data function returns false.
I'm well over my head here - I have no idea what's wrong. I've tried with Arduino 1.0.2 and 1.0.4. I've tried formatted my SD card (1GB Sandisk) with the proper SDFormatter and with windows. I can see that SD is not supplying anywhere near the latest version of sdfat I've tried modifying it to use a later one, but didn't get anywhere with that either. I don't understand what's going wrong - I cant seem to turn up similar problems in google...
Here's the basis of my code:
digitalWrite(SS_ETHERNET_OUT, HIGH); // Supposedly disables the ethernet
if( SD.begin(SS_SD_OUT) )
sdLogFile = SD.open("datalog.txt", O_CREAT | O_WRITE | O_APPEND);
if( sdLogFile )
displayMessage("SD card initialized", mlINFO);
print stuff to sdLogFile
call flush every 3 seconds