SDfat, cant restart sd card once removed

(started a new thread as the last one I could find was back in 2014)

I have looked at many forums and I think it was one written in 2014 that started that with SDfat, if the card is removed you can just use the sd.begin and it will re-initialize the sd card.

I cant get it to work.

it works fine at the beginning if the card is present but can get it to work if the card is inserted after the begin. see my simple sketch.

(using an arduino mega 2560, with sd card module with level shifters)

#include <SPI.h>             // f.k. for Arduino-1.5.2
#define USE_SDFAT
#include <SdFat.h>           // Use the SdFat library

SdFat sd;
SdFatSoftSpi<50, 51, 52> SD; //Bit-Bang on the Shield pins
#define SD_CS     53







bool good = SD.begin(SD_CS,SPI_FULL_SPEED);
    for (;!good;) {
        
        
        Serial.print("cannot start SD");
        //while (1); // this is a loop
       delay(5000);
       bool good = SD.begin(SD_CS,SPI_FULL_SPEED);

        
}

so if the card is present it works fine, as expected but if I insert the card just after the "cant start sd" during the five second delay (just before the SD.begin) it wont work.

what am I missing?

why do you use software SPI on hardware SPI pins. what for do you include SPI.h? What for is the variable sd?

Juraj:
why do you use software SPI on hardware SPI pins. what for do you include SPI.h? What for is the variable sd?

To be honest I took it from an example for the ili 9486 display, it uses pins 50-53 for the built in sd card. It wouldn't work as standard, I had to modify the SDfat.config file so that it became compatable with the mega 2560.

just double checked, hardware spi wont work if using a shield on pins 50-53, have to use software spi on the mega, the SDfat example even states

This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13

unless I'm doing something wrong.

just used the same sketch on my uno except it uses hardware spi on pins 11-13 or something like that.

#include <SPI.h>
//#include <SD.h>
#include "SdFat.h"
SdFat SD;

#define SD_CS_PIN SS
File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  for (;!SD.begin(SD_CS_PIN);) {
    Serial.println("initialization failed!");
    delay(5000);
    SD.begin(SD_CS_PIN);
  }
  Serial.println("initialization done.");

modified the example, I can now leave the SD card out to begin with, then fit it and will initialize, just cant get it work when using software spi with the mega.

it works!!!

not sure why (still learning) but changed this

bool good = SD.begin(SD_CS,SPI_FULL_SPEED);
    for (;!good;) {
        
        
        Serial.print("cannot start SD");
        //while (1); // this is a loop
       delay(5000);
       bool good = SD.begin(SD_CS,SPI_FULL_SPEED);

        
}

to this

    for (;!SD.begin(SD_CS,SPI_FULL_SPEED);) {
        
          
        Serial.print("cannot start SD");
        //while (1); // this is a loop
delay(5000);

SD.begin(SD_CS,SPI_FULL_SPEED);

        
}

so it was the "bool good", some one smarter than that reads this can may be tell me why.

AM_Spark:
it works!!!

not sure why (still learning) but changed this

bool good = SD.begin(SD_CS,SPI_FULL_SPEED);

for (;!good;) {
       
       
        Serial.print("cannot start SD");
        //while (1); // this is a loop
      delay(5000);
      bool good = SD.begin(SD_CS,SPI_FULL_SPEED);

}




to this 



for (;!SD.begin(SD_CS,SPI_FULL_SPEED):wink: {
       
         
        Serial.print("cannot start SD");
        //while (1); // this is a loop
delay(5000);

SD.begin(SD_CS,SPI_FULL_SPEED);

}





so it was the "bool good", some one smarter than that reads this can may be tell me why.

because you had two bool good. two variables. one inside for and one outside

But still it's weird. Declaring two variables with the exact same name and in the same scope should have thrown a compiler error.
The previous (wrong) code shouldn't even upload.

Lucario448:
But still it's weird. Declaring two variables with the exact same name and in the same scope should have thrown a compiler error.
The previous (wrong) code shouldn't even upload.

it is valid. if you can create a variable with the same name in a block ( {} ). it 'shadows' the variable from the wider scope.

1 Like