CronoDot RTC and Catalex MicroSD card reader/writer hooked up as a datalogger

Hi,

I have a CronoDot RTC and Catalex MicroSD card reader/writer hooked up as a datalogger.

Everything works great except when I added this small part to write the initialize text below to the SD card. If I comment it out, it writes to the Serial Monitor and the data goes to the monitor and SD card but the initialize text does not. If I put the dataFile lines in, it will not generate a SID_log.txt file at all.

The same basic code for writing to the SD card works bell down further iun the code.

Looks pretty straight forward, I must be missing something simple.

Any ideas?

Thanks,

Sid

=========================================

// File dataFile = SD.open(“SID_log.txt”, FILE_WRITE);
// if (dataFile)
// {
Serial.print(“Initializing 32 GB microSDHC card…”);// initialize serial communication:
// dataFile.print(“Initializing 32 GB microSDHC card…”);
if (!SD.begin(chipSelect)) // see if the card is present and can be initialized:
{
Serial.println(“Card failed, or not present”);// don’t do anything more:
// dataFile.print(“Card failed, or not present”);
return;
}
Serial.println(“Card initialized.”);
// dataFile.print(“Card initialized.”);
// dataFile.close();
// }

test_4_10_2015.ino (9.79 KB)

stryzbiak:
Looks pretty straight forward, I must be missing something simple.

Any ideas?

This line of code must be executed exactly ONCE and ONLY ONCE AFTER EACH RESET in your program:

if (!SD.begin(chipSelect)) // see if the card is present and can be initialized:

(And not every time you want to write something to a file.)

Hi,

Thanks. I looked through the code and I only saw it written once in the setup(). Did I miss something.

The more I am looking at it, I think that every time I open the file, I think its erasing my data. I want to keep all the data for years and just append more data to the file.

Any ideas?

Sid

stryzbiak:
Thanks. I looked through the code and I only saw it written once in the setup(). Did I miss something.

OK, I saw what you posted looked like:

//       dataFile.print("Initializing 32 GB microSDHC card...");
         if (!SD.begin(chipSelect)) // see if the card is present and can be initialized:

And the line commented out looked to me as if you possibly wanted to write to a file before the SD card even was initialized. So I assumed, that you already had initialized the SD card in another part of your program.

But if you initialize the SD card in the setup() function and only in the setup() function, everything is fine.

But of course, you can only access the SD card AFTER the SD card had been initialized successfully.

So you initialize the SD card in the setup().
You then see the Serial message "Serial.println("Card initialized.");".
And then, what's the problem accessing a file after the card initialization is finished successfully?

Or don't you see the "Card initialized." message?