Go Down

Topic: Writing to SD Card after removing and replacing it (Read 653 times) previous topic - next topic

ks1000

I have a data logging project where I post some data every 15 minutes to an SD card. 

While the project was running (way before the next write) I removed the card for a minute to look at the data already written and then inserted it again.  Once I remove the card and put it back it never writes to the card again.  The sketch continues to run as I see the output on the serial monitor but nothing shows up on the card.

I am using the Deek-Robot shield

Is this the way it works / a known issue / or might I be missing something?

Code: [Select]

String FormattedDateTime(DateTime UnformattedDateTime)
{
   String LocalFormattedDateTime = "";
   
   if (UnformattedDateTime.month() < 10)
       LocalFormattedDateTime += "0"; 
   LocalFormattedDateTime += UnformattedDateTime.month();
   LocalFormattedDateTime += '/';
   if (UnformattedDateTime.day() < 10)
       LocalFormattedDateTime += "0";
   LocalFormattedDateTime += UnformattedDateTime.day();
   LocalFormattedDateTime += '/';
      LocalFormattedDateTime += UnformattedDateTime.year();
   LocalFormattedDateTime += ' ';
   if (UnformattedDateTime.hour() < 10)
       LocalFormattedDateTime += "0";
   LocalFormattedDateTime += UnformattedDateTime.hour();
   LocalFormattedDateTime += ':';
   if (UnformattedDateTime.minute() < 10)
       LocalFormattedDateTime += "0";
   LocalFormattedDateTime += UnformattedDateTime.minute();
   LocalFormattedDateTime += ':';
   if (UnformattedDateTime.second() < 10)
       LocalFormattedDateTime += "0";
   LocalFormattedDateTime += UnformattedDateTime.second();
     
   return LocalFormattedDateTime;
} // FormattedDateTime

void WriteDataToLogFile(float Distance)
{
   myFile = SD.open("test.txt", FILE_WRITE);
   
   if (myFile) // If the file opened okay, write to it:
      {
         myFile.print(FormattedDateTime(RTC.now()));
         myFile.print(",");
         myFile.println(Distance);

         myFile.close();
      }
}  // WriteDataToLogFile


PaulS

Quote
or might I be missing something?

I think so. Something like setup() and loop().

And, of course, some way of telling the Arduino to NOT try to write to the card because you need to remove it. And, possibly, some way for the Arduino to know that it is OK to write again. And, possibly, some way for the Arduino to know that it needs to reinitialize the SD card.

All those Strings don't help, and are not needed. sprintf() will generate the whole output (the time portion, anyway) in one statement. Not that using just one statement is important.

el_supremo

I hooked up the card detect signal from the SD and when the Arduino detects that a card has been inserted the code forces a soft reboot (jmp 0). I haven't yet found a way to handle card reinsertion on-the-fly.

Pete

fat16lib

All file must be closed before removing the SD card to avoid data loss. SD cards require an initialization sequence when power is applied.  Files must be reopened and repositioned as necessary.

Unfortunately the standard SD.h library has a bug that prevents calling SD.begin() a second time to initialize the card a second time.

New versions of SdFat are designed to allow changing SD cards.  The bench.ino example allows cards to be changed after each test.

Here is how to run bench on two cards without a reboot.

Place the first card in the SD socket and load the bench.ino example. Start the Serial monitor and type a character.

You should see something like this:
Quote

Use a freshly formatted SD for best performance.
Type any character to start
Free RAM: 1030
Type is FAT16
File size 5MB
Buffer size 100 bytes
Starting write test.  Please wait up to a minute
Write 354.28 KB/sec
Maximum latency: 64928 usec, Minimum Latency: 88 usec, Avg Latency: 277 usec

Starting read test.  Please wait up to a minute
Read 361.19 KB/sec
Maximum latency: 1924 usec, Minimum Latency: 84 usec, Avg Latency: 271 usec

Done

Type any character to start


Remove the first card, insert the second card, and type a character. 

You should see the second test like this:
Quote

Free RAM: 1030
Type is FAT16
File size 5MB
Buffer size 100 bytes
Starting write test.  Please wait up to a minute
Write 354.71 KB/sec
Maximum latency: 65012 usec, Minimum Latency: 88 usec, Avg Latency: 276 usec

Starting read test.  Please wait up to a minute
Read 361.25 KB/sec
Maximum latency: 1928 usec, Minimum Latency: 84 usec, Avg Latency: 271 usec

Done

Type any character to start


You can continue to remove and insert cards after each test.

Go Up