No answer? Well, maybe I wasn't specific enough. So here's an example you can try yourself:
#include <SPI.h>
#include <SD.h>
void clearSD()
{
byte sd = 0;
digitalWrite(SS, LOW);
while (sd != 255)
{
sd = SPI.transfer(255);
Serial.print("sd=");
Serial.println(sd);
}
digitalWrite(SS, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(SS,OUTPUT);
SPI.begin();
delay(10);
boolean b;
// *** SD Card **
b = SD.begin(SS);
if (!b)
{
delay(100);
clearSD();
delay(100);
b = SD.begin(SS);
}
if (b)
Serial.println("SD Card started.");
else
Serial.println("SD Card failed to start!");
Serial.println();
}
void loop() {};
Most likely, the SD Card will correctly start as soon as the script installs (but you can't see the response yet). So press the reset button, and on the Serial Monitor window, you'll see:
sd=255
SD Card failed to start!
Tap the reset button again, and you'll see the same response:
sd=255
SD Card failed to start!
Now remove and reinsert the SD Card (a split-second out it all the time it takes). And again, tap the reset button, and you'll see:
SD Card started.
Now Tap the reset button again, one more time, and you'll again see:
sd=255
SD Card failed to start!
The StackOverflow.com page titled Initializing SD card in SPI issues suggests (right under the heading "4 Answers") to send the number "255" to the card, over and over, until it sends "255" back, meaning you've successfully cleared it.
So that's what I'm doing in this sketch. And although the card responds with a "255" on the very first reply, nothing changes for me. The problem persists.
Why does briefly removing the card work to reset everything?
What spi code can I send the card that will do the same job as briefly removing it?