datalogger does not report, when I take out the SD-card

Hi
I want to be informed, when the SD-card is mission or was taken out by the script.
I tried that with the example file called “datalogger” and that seemed to work.

But when I change that script very lightly so that the log is only done every 15 seconds, then taking out the SD-card of the slot is not recognized and reported by the script.

See here, the few added lines are marked by a comment with three slashes: ///

/*
  Modified datalogger.ino to show, that the removal of the SD-card is not recognized and reported 
  by the last Serial.println command.
  Only a few lines are added and preceded by /// comments of 3 slashes  
  
  SD card datalogger

 This example shows how to log data from three analog sensors
 to an SD card using the SD library.

 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4

 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
#include <SD.h>

/// 3 lines:
#define LOG_PERIOD 15000  //Logging period in milliseconds, recommended value 15000-60000.
#define MAX_PERIOD 60000  //Maximum logging period without modifying this sketch
unsigned long previousMillis;  //variable for time measurement

const int chipSelect = 10;

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


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

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {

/// 4 lines:
  unsigned long currentMillis = millis();
  // nur alle Minute oder so einmal auszuführen:
  if(currentMillis - previousMillis > LOG_PERIOD){  // Beginn der Minutenaktion
    previousMillis = currentMillis;

  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
  /// 1 line:
  }
}

What is my error or how can I get the info, that the card is not writeable at the moment? The terminal prints undisturbed the dataString, although it can not be written to the datafile.

thanks in advance!

Andreas

You have two issues. Most card readers have a card detect switch. You can read this switch to determine that a card has been removed.

SD cards must be reinitialized when they have lost power, so when the card is reinserted, you will need to call sd.begin(). The calling of sd.begin() after the first initialization is straightforward with SdFat but SD.h requires some additional code. See http://forum.arduino.cc/index.php?topic=239157.msg1718831#msg1718831

You can use a button to trigger the call to sd.begin() when the card has been reinserted. If you try to use the card detect switch it will require good debounce and an understanding of the timing of when the card trips the switch and when the contacts are actually made.

You need to be careful about when you pull the card to be sure that the file is closed, and that maybe one second has passed to allow the actual write to take place.