SD card stops working after the second insertion

I'm using the Arduino Shield on the Arduino Uno. When storing data to the SD card from a rotary encoder, the first time works but if I remove the SD card and reinsert it and start to try to record data again it no longer works. What could be the issue for this? Thanks in advance!

void setup() {
  Serial.begin(115200);
  attachInterrupt(digitalPinToInterrupt(2), pulseDetected, RISING);
  startMillis = millis();
  
  pinMode(CS_pin, OUTPUT);

  if (!SD.begin(CS_pin)) {
    Serial.println("Card Failed");
    return; 
  }
  
  Serial.println("Card Ready");

  
}

void loop(){

encoderState = digitalRead(2);
  //set current time monitor to millis()
  currentMillis = millis();

  
  if (currentMillis-startMillis>=period){ 
    String name = "Summary" + String(nameTracker) + ".txt";

    File dataFile = SD.open("Summary" + String(nameTracker) + ".txt", FILE_WRITE);
    Serial.println(dataFile);

    if(dataFile){
      dataFile.println("# Pulses: " + String(pulses));
      dataFile.close();
      Serial.println("Success");
      nameTracker+=1;
    } else {
      Serial.println("Couldn't access file");
    }

  startMillis = currentMillis;
  }
}

void pulseDetected(){
 pulses++;
 File dataFile = SD.open("Details" + String(nameTracker) + ".txt", FILE_WRITE);
 
 //Serial.println(pulses);
 calculateRotations();
 startMillis = currentMillis;
}

Edit: I think it might have something to do with the fact that the SD card is only initialized in the setup and not initialized again even when the card is reinserted. Still not too sure how to detect the insertion/removal of the SD card though

Instead of posting a stupid picture, read How to post code properly and then paste your code as text using the </> icon to create

[code]...[/code]

tags around your code so that it is easier to read.

Pete

And were is nameTracker declared?

Further be aware of the 8 dot 3 limitation for filenames on SD cards.

el_supremo:
Instead of posting a stupid picture,

Is this type of language really necessary?
Helpful?
Friendly?

It's his first post, how do you think he now feels about the entire community?

@gchen-cshl, we are not all that full of ourselves.

1 Like

@jgmiller500: OP clearly had not read the sticky information threads at the top of each forum otherwise they would have posted the code in code tags the first time. And with just four posts under your belt, who made you the conscience of the Arduino forums?

@gchen-cshl: @jgmiller500 is full of himself.
And thanks for replacing the image with code in code tags.

Pete

As you guessed, you need to initialize the card again when it is reinserted. Some SD card adapters have a separate pin which indicates whether the card is present or not. If your SD card adapter has one, connect the extra pin to an Arduino pin. You can either choose to connect it to an interrupt pin and then you only need to do anything about the card if the interrupt occurs. Or you can just read the state of the pin before each write to the card. If the card isn't there, wait until it has been reinserted and then reinitialize the card and proceed with the write.

Which SD card adapter are you using?

Pete

I've had a closer look at your code and there are some things wrong with it.

  • The pulseDetected90 function is an Interrupt Service Routine (ISR). It is a very bad idea to do something as complex as opening an SD file in an ISR.

  • If you expect your code to run for long periods of time, it is also not a good idea to use the String class. On those Arduinos which have a small amount of ram (such as UNO or NANO with only 2kB), the String class will eventually fragment its memory to the point that it crashes the processor. You aren't doing any complex string anyway and would be much better off using null-terminated C strings.

Which Arduino are you using?

What is connected to pin 2?

Pete