user53837: i am trying to make a code that prints the time hh:mm:ss:ms using RTC,i have made it using millis() but it  is not accurate +/-2 milliseconds
ho can i make it using sq wave and intterupt
here is the code that i have used
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;      // This is the DS1307 hardware RTC
RTC_Millis SoftRTC;   // This is the millis()-based software RTC
long startMS;  // This will hold the start time in milliseconds

void setup ()
{
    Wire.begin();
    RTC.begin();                       // Connect to the DS1307
    SoftRTC.begin(RTC.now());  // Initialize SoftRTC to the current time
    startMS = millis();  // get the current millisecond count
}

void loop()
{
    DateTime now = SoftRTC.now();
    long nowMS = millis();
   
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(':');
    Serial.print((nowMS - startMS)%1000, DEC);  // print milliseconds
    Serial.println();

i tried to put this code that is based on ISR but with no luck
volatile time_t isrUTC;         // ISR's copy of current time in UTC
volatile time_t millisecondsFromISR;         // ISR's copy of current time millisecond offset

void incrementTime()
{
    millisecondsFromISR = millis();  //do first to get most accuracy
    ++isrUTC;
}

int getMilliseconds() {
    // need to make an atomic copy:
    nointerrupts();
    unsigned long t = millisecondsFromISR;
    interrupts();

    return millis() - t;
}


user4143: All you have to do is record the number of milliseconds at the moment the square wave toggles on its 1 second cycle. Then your milliseconds is the difference between that and the current.
In short: update startMS when triggered by the RTC's output.

53837: <p>i am trying to make a code that prints the time hh:mm:ss:ms using RTC,i have made it using millis() but it  is not accurate +/-2 milliseconds</p>
<p>ho can i make it using sq wave and intterupt</p>
<p>here is the code that i have used</p>
<pre><code>#include &lt;Wire.h&gt;
#include &quot;RTClib.h&quot;

RTC_DS1307 RTC;      // This is the DS1307 hardware RTC
RTC_Millis SoftRTC;   // This is the millis()-based software RTC
long startMS;  // This will hold the start time in milliseconds

void setup ()
{
    Wire.begin();
    RTC.begin();                       // Connect to the DS1307
    SoftRTC.begin(RTC.now());  // Initialize SoftRTC to the current time
    startMS = millis();  // get the current millisecond count
}

void loop()
{
    DateTime now = SoftRTC.now();
    long nowMS = millis();
   
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(':');
    Serial.print((nowMS - startMS)%1000, DEC);  // print milliseconds
    Serial.println();
</code></pre>
<p>i tried to put this code that is based on ISR but with no luck</p>
<pre><code>volatile time_t isrUTC;         // ISR's copy of current time in UTC
volatile time_t millisecondsFromISR;         // ISR's copy of current time millisecond offset

void incrementTime()
{
    millisecondsFromISR = millis();  //do first to get most accuracy
    ++isrUTC;
}

int getMilliseconds() {
    // need to make an atomic copy:
    nointerrupts();
    unsigned long t = millisecondsFromISR;
    interrupts();

    return millis() - t;
}
</code></pre>
 4143: <p>All you have to do is record the number of milliseconds at the moment the square wave toggles on its 1 second cycle. Then your milliseconds is the difference between that and the current.</p>
<p>In short: update <code>startMS</code> when triggered by the RTC's output.</p>
