GPS - SD Card Fails to Open File After Two Writes

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
CPARKTX
 
Posts: 11
Joined: Mon May 12, 2014 6:13 pm

GPS - SD Card Fails to Open File After Two Writes

Post by CPARKTX »

I need suggestions on where to look next to debug the issue I am having. Using Arduino Mega with Adafruit Ultimate GPS Logging Shield. In my project, I periodically open a file, write some text, and close the file; however, the card consistently fails to re-open the file after the second write...that is the SD initializes fine, I open the file write to it, close it, open/write/close a second time, but then all attempts from then on the card will not open again.

What I have tried so far:

1. I have a very simple test sketch that repeatedly opens, writes to and closes a test file; that works fine, so probably not a hardware or wiring issue or issue with the SD card itself.

2. I have tons of free memory (4K), so memory or memory not a memory leak.

3. I have tried using a static filename and static text, so not related to the name of the file or what I am actually writing to the file.

4. I have tried turning off the GPS interrupt before open the SD file and turning back on after the write is complete.

The full sketch is attached, and the key code is below:

Code: Select all

File positionLog;	
positionLog = SD.open(positionFileName, FILE_WRITE);
        if (positionLog) 							// was open was successful?
      		{
      			positionLog.println(myString);		// write it to the file
      			positionLog.close();
      			Serial.println(myString);
      		}
	     else
      		{
      				Serial.println(F("SD CARD ERROR"));
          }
Any suggestions on where to look next??
Attachments
RC_Car_Test_2014_07_20_002.ino.txt
(27.06 KiB) Downloaded 129 times

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: GPS - SD Card Fails to Open File After Two Writes

Post by adafruit_support_rick »

Does it keep printing "SD Card Error" over and over, or does it do nothing at all?

CPARKTX
 
Posts: 11
Joined: Mon May 12, 2014 6:13 pm

Re: GPS - SD Card Fails to Open File After Two Writes

Post by CPARKTX »

It keeps printing "SD Card Error" every sequential time that it is called; so two successful writes to the file, and then repeated failures no matter how many additional times it is called.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: GPS - SD Card Fails to Open File After Two Writes

Post by adafruit_support_rick »

Very strange. I'm not seeing anything obvious.
Is there some reason you're closing and reopening the file? Can't you just use flush to force the write?

CPARKTX
 
Posts: 11
Joined: Mon May 12, 2014 6:13 pm

Re: GPS - SD Card Fails to Open File After Two Writes

Post by CPARKTX »

I was closing the file after writing because I don't know when the program will end and I didn't want to leave the file in an unstable state; after a flush, is a close eventually necessary?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: GPS - SD Card Fails to Open File After Two Writes

Post by adafruit_support_rick »

Hmmm ... offhand, I don't remember what it does if you flush but don't close. Can you give it a try?

I don't know why it doesn't work the way you have it. I can't see anything wrong with it.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: GPS - SD Card Fails to Open File After Two Writes

Post by adafruit_support_rick »

Here's something to try - you have the system set up to use software SPI, even though you're using the hardware SPI pins.
Change your sd.begin:

Code: Select all

  // SD Logging
  #ifdef LOG_POSITION
    pinMode(SS, OUTPUT);    // set pin to output to make sure SD library works okay
    if (!SD.begin(chipSelect) )   //use hardware SPI
      {
 

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”