Arduino MEGA stucks while opening file in SD card after being idle

I have a Mega and SD shield connected. There are two push buttons at pin 2 & 3 which open a valve at pin 8 for different duration. And then logs are written to a SD card file. It all works fine for an hour. But if this system is left idle for an hour and then when valve is triggered, it stucks after failing to open log file. I have tried writing to SD after a delay but didn't work.

dataFile = SD.open(filename, FILE_WRITE);        
if (dataFile) 
{

//writing data

}
else
{
Serial.println("Error opening file");  //shows this error
}
//.....stucks here
Serial.println("This doesn't happen");

Please suggest any solution or share any similar experience you had.

Please suggest any solution

I suggest that you visit http://snippets-r-us.com for help with your snippet.

alabdulrehman:
I have a Mega and SD shield connected. There are two push buttons at pin 2 & 3 which open a valve at pin 8 for different duration. And then logs are written to a SD card file. It all works fine for an hour. But if this system is left idle for an hour and then when valve is triggered, it stucks after failing to open log file. I have tried writing to SD after a delay but didn't work.

Simplest explanation, Out of Memory Problem.

without your code we can only guess.

Try using this library, you will be surprise at how little RAM is available when SD's are used.

Chuck.


Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560's. It adds 1MB of RAM for those projects where 8KB is not enough.

you will be surprise at how little RAM is available when SD's are used.

On a Mega?

PaulS:
On a Mega?

The MEGA has 8704 bytes of RAM many are uses for ISR jump vectors, All quoted String are copied into RAM. If you program Serial.println("now is the time for all good men to come to ..."); you just wasted 48 bytes of RAM. Serial.println(F(""now is the time for all good men to come to ...")); Uses many fewer.
The SDcard library needs as minimum 512 for a sector buffer, plus structures for FAT and Directory navigation.

Call this piece of code to see free RAM from Lady Ada: Memory Usage.

int freeRam () 
{
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

Chuck.


Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560's. It adds 1MB of RAM for those projects where 8KB is not enough.