Go Down

Topic: SDfat, cant restart sd card once removed (Read 224 times) previous topic - next topic

AM_Spark

(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)



Code: [Select]


#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?

Juraj

why do you use software SPI on hardware SPI pins. what for do you include SPI.h? What for is the variable sd?
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

AM_Spark

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.


AM_Spark

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


Code: [Select]

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.

Code: [Select]

#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.

AM_Spark

it works!!!

not sure why (still learning) but changed this

Code: [Select]
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

Code: [Select]
    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.

Juraj

#5
Jan 18, 2019, 11:02 am Last Edit: Jan 18, 2019, 11:03 am by Juraj
it works!!!

not sure why (still learning) but changed this

Code: [Select]
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

Code: [Select]
    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.
because you had two bool good. two variables. one inside for and one outside
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

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.

Juraj

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.

https://en.wikipedia.org/wiki/Variable_shadowing
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Go Up