SD.open always fail

I'm trying to interleave the sd read/write with the ethernet usage on an ethernet shield with arduino uno.

Ethernet works fine after i've found a note on the pins 4 and 10, but the SD.open function fails every time even if the file is created on the sd card. The card is a 8gb microsd with a 3gb partition formatted in fat16. The Sd library is the default library of arduino ide 1.0.5

Here it is the startup code:

Serial.begin(9600);
  
  thisday=0;
  thismonth=0;
  pulseCount=0;
  dayPulse=0;
  loops=0;
  saved=false;
  pinMode(signalPin,OUTPUT); 

  pinMode(SS_SD_CARD, OUTPUT);
  pinMode(SS_ETHERNET, OUTPUT);
  digitalWrite(SS_SD_CARD, HIGH); 
  digitalWrite(SS_ETHERNET, HIGH);
    
  if (!SD.begin(SS_SD_CARD)) {
  
    return;
  }
  
  
  byte ip1,ip2,ip3,ip4,m1,m2,m3,m4,g1,g2,g3,g4;
  boolean test1=readEEPROMIp(0,&ip1,&ip2,&ip3,&ip4);
  boolean test2=readEEPROMIp(6,&m1,&m2,&m3,&m4);
  boolean test3=readEEPROMIp(12,&g1,&g2,&g3,&g4);
  
    
  if (test1 && test2 && test3){ 

          ip[0]=ip1;
         ....cut....
          gateway[3]=g4;

  }
         
  
  
  Ethernet.begin(mac, ip,gateway,subnet);
  digitalWrite(SS_ETHERNET, HIGH); 
  server.begin();
  Wire.begin();
  rtc.begin();
  if (!rtc.isrunning()) {
       rtc.adjust(DateTime(__DATE__, __TIME__));
        
  }  
  
  digitalWrite(SS_ETHERNET, LOW);

The interesting function is:

void logCurrentPulses(){
  char name[14];
  memset(name,'\0',14);
  DateTime now = rtc.now();
  sprintf(name,"%d%d.TXT",now.month(),now.year());
  
  
  char logLine[20];
  sprintf(logLine,"%d,%d:%d,%d",now.day(),now.hour(),now.minute(),pulseCount);
  
  digitalWrite(SS_ETHERNET, HIGH);
  digitalWrite(SS_SD_CARD, LOW); 
  
  Serial.println("opening file for write");
  
  File dataFile = SD.open(name, FILE_WRITE);
  
  
  if (dataFile) {
    Serial.println("opened!");
    dataFile.println(logLine);
    dataFile.close();
    pulseCount=0;
    
  }else{
    Serial.println("failed open file for write!");
  }
  
  
  
  digitalWrite(SS_SD_CARD, HIGH); 
  digitalWrite(SS_ETHERNET, LOW);
   

}

As i've read in some other posts, i disable the ethernet chip before weakup the sd chip and then use it. At the end the same operations are called in reverse order.

Maybe i'm doing something wrong?

Thanks. ivan

  if (!SD.begin(SS_SD_CARD)) {
  
    return;
  }

Return? Printing an error message might be useful. More useful that skipping the rest of setup(), anyway.

Changed to

if (!SD.begin(SS_SD_CARD)) {
    Serial.println("Sd card init failed!");
  }

My bad. Too fast copy& paste from tutorial…

Do you see that message in the Serial Monitor?

What, exactly, ends up in name?

No, the message does not appear in console.

these are the current log messages: (consider that i've placed the message "sd card init success" on the else of "if !sd.begin...")

Sd card init success!
opening file for write
failed open file for write!

The filename is like MMYYYY.TXT where MM is the current month (0-12) and YYYY is the current year (4 digit format). I'm a little stuck about the usage of digitalWrite( 10 or 4, high or low) to manage the sd instead of eth and viceversa. In some posts some guy ask to use it, in other posts other guys doesn't write nothing and works fine . Is correct what i'm doing?

The SD library and the Ethernet library manage making the appropriate device active by setting the device's chip select pin correctly. You should not need to.

If you comment out the Ethernet.begin() call, can you create a file?

opening file for write
failed open file for write!

same error... with all digitalWrite on pin 4 or 10 removed

same error... with all digitalWrite on pin 4 or 10 removed

You need to post ALL of your code.

Problem found. I was just running out of memory. Optimizing the code and memory usage solved the problem.

grisson87: Problem found. I was just running out of memory. Optimizing the code and memory usage solved the problem.

Thanks grisson87,

I had similar problem. Based on your suggestion, I commented out part of my unnecessary codes and "SD.open" works fine now. I'm wondering how can we identify this kind of RAM errors!?