Datasheet
Serial.begin(9600);
Serial.println();
#if WAIT_TO_START
Serial.println("Type any character to start");
while (!Serial.available());
#endif //WAIT_TO_START
K now we are onto the code. We begin by initializing the Serial port at 9600 baud. If we set
WAIT_TO_START to anything but 0, the Arduino will wait until the user types something in.
Otherwise it goes ahead to the next part
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// 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.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
Now the code starts to talk to the SD card, it tries to initialize the card and find a
FAT16/FAT32 partition.
Next it will try to make a logfile. We do a little tricky thing here, we basically want the files to
be called something like LOGGERnn.csv where nn is a number. By starting out trying to
© Adafruit Industries https://learn.adafruit.com/adafruit-data-logger-shield Page 78 of 85