Sd card test program fails... but more complex program works fine.

I am trying to do some tests with an SD card. I have a Seeeduino Ethernet shield with an SD slot mounted on an Arduino Mega. My code for the Ethernet shield works, and I am able to use it as a server; I can find the files, and read from them just fine.

Since this is a mega, the SS pin is 53. For the shield, the SD card is on pin 4.

I can’t figure out why this code below will not function, it will constantly fail at sd card initialization. HOWEVER the code will run if the last code that ran on it was the more complicated stuff…???

#include <SPI.h>
#include <SD.h>
#include <EthernetV2_0.h>


#define chipSelect   53


void setup() {

  pinMode(chipSelect, OUTPUT);
  digitalWrite(chipSelect, HIGH);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  File myFile;
  
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  } 
  else {
    Serial.println("error opening test.txt");
  }

  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    Serial.print("Where Does this text go?");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
  } 
  else {
    Serial.println("error opening test.txt");
  }

  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  } 
  else {
    Serial.println("error opening test.txt");
  }
}

void loop() {
  delay(400);
}

Does the WebServerWithSD example work properly? it uses pins 10 and 4 put notes pin 53 for the Mega.

I just tried it... and it does. Pin 10 works....

Why does Pin 10 work, I'm so confused. Pin 53 works for everything up to now. What the hell. It's a mega, everything says to use pin 53.

Mega pin 53 is the ss pin for when the mega is a slave to some other device acting as master: making pin 53 a low input will make it a slave. (The library doesnt suport mega as slave which is why pin 53 should be made an output so it wont accidentally be made a low input and be a slave which you dont want.)

The same is true of pin 10 on an Uno.

Now, since pin 10 on an Uno should for safety's sake be an output, it's handy to use that fact to control a slave on an Uno master. (Might as well use the pin which you already made an output. It's only a low input that would make the Uno a slave; it can safely be made a low output to control the slave via the slaves ss input.)

Thats why shields might use pin 10 as their ss: make pin 10 an output for the Unos sake, and while you're at it, use the same pin as the shield's ss.

Since the shield plugs into the "Uno half" of the mega, the shield still needs pin 10 as it's ss. That pin 10 is no longer the host's ss is irrelevant: pin 10 on an Uno or 53 on Mega is the host board's ss pin to control the board being a slave to some other master (which the spi library happens not to support.)

So with your shield plugged onto the "Uno half" of the Mega, pins 4 and 10 are the ss pins for the shield's 2 spi devices as slaves to the Mega as master. You should still make pin 53 an output, since pins default to inputs, and if it accidentally went low your Mega would become a slave to some spurious master.