calculating time using RTC module

Hello,

I’m trying to calculate the time between two points but I can’t get it to work.

I am using the same method as when you use millis. To set the time im using SetSerial by JChristensen.

#include <DS3232RTC.h>      // https://github.com/JChristensen/DS3232RTC
#include <Streaming.h>      // http://arduiniana.org/libraries/streaming/



const unsigned long interval = 1000; // 10 SECOND INTERVAL
unsigned long previousTime = 0;

void setup()
{
    Serial.begin(115200);

    // setSyncProvider() causes the Time library to synchronize with the
    // external RTC by calling RTC.get() every five minutes by default.
    setSyncProvider(RTC.get);
    Serial << F("RTC Sync");
    if (timeStatus() != timeSet) Serial << F(" FAIL!");
    Serial << endl;
}

void loop()
{
    static time_t tLast;
    time_t t;
    tmElements_t tm;

    // check for input to set the RTC, minimum length is 12, i.e. yy,m,d,h,m,s
    if (Serial.available() >= 12) {
        // note that the tmElements_t Year member is an offset from 1970,
        // but the RTC wants the last two digits of the calendar year.
        // use the convenience macros from the Time Library to do the conversions.
        int y = Serial.parseInt();
        if (y >= 100 && y < 1000)
            Serial << F("Error: Year must be two digits or four digits!") << endl;
        else {
            if (y >= 1000)
                tm.Year = CalendarYrToTm(y);
            else    // (y < 100)
                tm.Year = y2kYearToTm(y);
            tm.Month = Serial.parseInt();
            tm.Day = Serial.parseInt();
            tm.Hour = Serial.parseInt();
            tm.Minute = Serial.parseInt();
            tm.Second = Serial.parseInt();
            t = makeTime(tm);
            RTC.set(t);        // use the time_t value to ensure correct weekday is set
            setTime(t);
            Serial << F("RTC set to: ");
            printDateTime(t);
            Serial << endl;
            // dump any extraneous input
            while (Serial.available() > 0) Serial.read();
        }
    }

    t = now();
    if (t != tLast) {
        tLast = t;
        printDateTime(t);
        if (second(t) == 0) {
            float c = RTC.temperature() / 4.;
            float f = c * 9. / 5. + 32.;
            Serial << F("  ") << c << F(" C  ") << f << F(" F");
        }
        Serial << endl;
    }

   unsigned long currentTime = now();  
      
   if (currentTime - previousTime >= interval) {

    previousTime = currentTime;
    Serial.println("million"); 
   }
}

// print date and time to Serial
void printDateTime(time_t t)
{
    printDate(t);
    Serial << ' ';
    printTime(t);
}

// print time to Serial
void printTime(time_t t)
{
    printI00(hour(t), ':');
    printI00(minute(t), ':');
    printI00(second(t), ' ');
}

// print date to Serial
void printDate(time_t t)
{
    printI00(day(t), 0);
    Serial << monthShortStr(month(t)) << _DEC(year(t));
}

// Print an integer in "00" format (with leading zero),
// followed by a delimiter character to Serial.
// Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
    if (val < 10) Serial << '0';
    Serial << _DEC(val);
    if (delim > 0) Serial << delim;
    return;
}

This is the bit of code I put in it myself:

const unsigned long interval = 10000; // dit is 10 seconden
unsigned long previousTime = 0;

void setup() {
  Serial.begin(115200);  

}

void loop() {

   unsigned long currentTime = now();  
      
   if (currentTime - previousTime >= interval) {

    previousTime = currentTime;
    Serial.println("million"); 
   }
   
}

I want the word “million” to be printed every 10 seconds (10000ms).
But I can’t get it to work.

now() gives out a value.

when the time is 9 februari 2021 11:16:26 (european time)

    delay(2000);
    Serial.println(now());

    delay(2000);
    int value = now();
    Serial.println(value);

it gives different values is this because it reaches the max capacity of an integer?

the integer (value) gives 28434 and it counts up by 4

the function now() gives 1612869392 and also counts up by 4

I can use the function now() as time. But the problem is what do I use as interval? When using millis I could use an interval of 10000ms wich is 10 seconds and it would work. But I don’t know what to use as interval when the value is so big.

now() gives a time in seconds, so you want to compare with 10 instead of 10000

And, unless you want to print "million" when the arduino is started, you should initialize previousTime to the current time instead of 0, so I suggest something like this:

void loop()
{
    unsigned long currentTime = now();
    static unsigned long previousTime = currentTime;
    
    if (currentTime - previousTime >= interval) {
    ...

guix:
now() gives a time in seconds, so you want to compare with 10 instead of 10000

And, unless you want to print “million” when the arduino is started, you should initialize previousTime to the current time instead of 0, so I suggest something like this:

void loop()

{
   unsigned long currentTime = now();
   static unsigned long previousTime = currentTime;
   
   if (currentTime - previousTime >= interval) {
   …

Thank you for your fast reply!
What do you mean by " so you want to compare with 10 instead of 10000"

Do you mean I change the integer “Interval” to 10 instead of 10000 milliseconds?
I’ll change the value of previousTime to currentTime. However it isn’t really a big deal when it prints out at the start.

rico459:
Do you mean I change the integer "Interval" to 10 instead of 10000 milliseconds?

Yes

guix:
Yes

Thank you very much! It works like intended!

I know :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.