Improving write speed + millisecond accuracy?

Hello all,

As a disclaimer, I'm quite new to the arduino and I'm running into a couple of issues with which I'm hoping I could get a bit of assistance.
I'm using an arduino uno r3 with the datalogging shield from Adafruit, which comes with the DS1307 rtc.

I have two sensors hooked up and a separate interrupt for each one, which sets a flag and and then prints the current time on an SD card (with precision to milliseconds). The interrupts are working fine, but I'm encountering two main problems.

The first being write speed onto the SD card. The following is part of my code:

void loop() { 
  if (flag == 1){
    File dataFile = SD.open("datalog.txt", FILE_WRITE); 
    DateTime now = RTC.now();
    dataFile.print("med-");
    dataFile.print(now.year(), DEC);
    dataFile.print('/');
    dataFile.print(now.month(), DEC);
    dataFile.print('/');
    dataFile.print(now.day(), DEC);
    dataFile.print(' ');
    dataFile.print(now.hour(), DEC);
    dataFile.print(':');
    dataFile.print(now.minute(), DEC);
    dataFile.print(':');
    dataFile.print(now.second(), DEC);
    dataFile.print(':');
    dataFile.print((millis() % 1000), DEC);
    dataFile.println();   
    flag=0;
    dataFile.close();}}

I believe that it is the opening and closing of dataFile that is slowing me down, as I would like to be able to write to the card as quickly as possible. Is there any workaround for this that would avoid me corrupting datalog.txt?

My second problem is related to millisecond drift. DS1307 does not have millisecond accuracy, so my rough solution was to use millis() and floor divide by 1000. I know that this isn't ideal though. Is there some way that I can sync millis to my rtc so that my value for millisecond will roll over to 0 whenever the second changes?

Thanks.

most RTC's have an pulsed output, which you can connect to an interrupt routine.
if that signal is 32kHz you just count to 32 and then increment your myMillis variable.
When it is 1000 reset it to 0
be aware that the int should be declared volatile.

robtillaart:
most RTC's have an pulsed output, which you can connect to an interrupt routine.
if that signal is 32kHz you just count to 32 and then increment your myMillis variable.
When it is 1000 reset it to 0
be aware that the int should be declared volatile.

Could you please give me a hand (either with a bit of code or a link) as to how to accomplish this? I've done quite a lot of googling to no avail. Perhaps I don't know what to actually search... I found that the DS1307 seems to have a 32.768kHz quartz crystal (according to http://datasheets.maximintegrated.com/en/ds/DS1307.pdf). Would I have to account for this in some way?

I figured out the SD card write speed, but still can't get this to work...

Thanks.

gtrtt:
I found that the DS1307 seems to have a 32.768kHz quartz crystal (according to http://datasheets.maximintegrated.com/en/ds/DS1307.pdf). Would I have to account for this in some way?

Increment a counter by 1000 on each pulse. When the counter reaches or exceeds 32768, decrement the counter by 32768 and increment your myMillis variable by one.

My apologies - perhaps I should have clarified. What code do I use to actually detect each pulse?
I’m looking at the datasheet, but I can’t seem to get anything that works (please keep in mind that I’m still quite inexperienced in the world of electronics).

gtrtt:
My apologies - perhaps I should have clarified. What code do I use to actually detect each pulse?
I'm looking at the datasheet, but I can't seem to get anything that works (please keep in mind that I'm still quite inexperienced in the world of electronics).

You should have an interrupt routine that increments and resets mymillis.
the clock out of the RTC should be connected to pin2 or 3 (UNO)
check the attachInterrupt() example