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);
}