Bring RTC value to an integer

Hello,

I am trying to get the value of an RTC clock (DS3231)

How do I do this?

Im using the library DS3232RTC

am I right that NOW() gives a value?

So

RTC() = RTC_VALUE;

To set the time I use

void GetTheTime()
{
  char* ntpServer = "2.us.pool.ntp.org";
  int gmtOffset_sec = -(3600 * 7 );
  int daylightOffset_sec = 3600;
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}

to extract the hour I use

// http://www.cplusplus.com/reference/ctime/strftime/
////
int getHour()
{
  struct tm timeinfo;
  getLocalTime(&timeinfo);
  char _time[ 5 ];
  strftime( _time, 80, "%T", &timeinfo );
  return String(_time).toInt();
}
////

and to print the time I use

void printLocalTime()
{
  struct tm timeinfo;
  getLocalTime(&timeinfo);
  char _time[ 80 ];
  strftime( _time, 80, "%T", &timeinfo );
  log_i( "%s", _time);
}
////

Not sure but hopeful that may help.

You need to provide a lot more detail, such as a link to the library you're using, and which Arduino you are using.

@OP

1. I use the following Jeelab's fantastic RTC library for DS3231 RTC Module.

#include<RTClib.h>

2. I declare the following objects.

RTC_DS3231 rtc;
DateTime nowDT;

3. I execute the following codes to get the time of the day.

nowDT = rtc.now();
byte myHrs = nowDT.hour();
byte myMin = nowDT.minute();
byte mySec = nowDT.second();

Have a look at this thread, I believe you are talking about Epoch time.

Im using the library DS3232RTC

If that is the J.Christensen library it is a fine choice.

It is designed to be integrated with the Time Library, and now() is a time library command.

RTC() = RTC_VALUE;

This makes not sense. Have you tried the library examples SetSerial or TimeRTC?

cattledog:
If that is the J.Christensen library it is a fine choice.

It is designed to be integrated with the Time Library, and now() is a time library command.

RTC() = RTC_VALUE;

This makes not sense. Have you tried the library examples SetSerial or TimeRTC?

Yes, im using Jchristensen's code to set the time. And ive looked at some of his examples, I can't find anything to put the total seconds in a value so you can calculate the time between them.

To be clear the issue you are having is that you cannot get a time, store it in a variable, wait for sometime, and get the differences between the 2 times?

You have not looked at millis()? With millis() you can store a time, in millis seconds, then, later on, get the current millis() and subtract the 2 values to get the milli second time difference.

Are you integrating with the PJRC Time Library and using the DS3232 as the sync provider (as suggested by @cattledog in Reply #5)? If so, then now() is the best way to get the time as an unsigned long. That way you don't waste time with I2C transactions with the hardware every time you want to get the time.

I can't find anything to put the total seconds in a value so you can calculate the time between them.

I am not sure exactly what you are looking for but take a look at this

#include <Time.h>
#include <TimeLib.h>

unsigned long startSeconds;
unsigned long currentSeconds;
unsigned long periodInSeconds = 10;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  startSeconds = now();
}

void loop()
{
  currentSeconds = now();
  if (currentSeconds - startSeconds >= periodInSeconds)
  {
    Serial.println("time to do something");
    startSeconds = currentSeconds;
  }
}

UKHeliBob:
I am not sure exactly what you are looking for but take a look at this

#include <Time.h>

#include <TimeLib.h>

unsigned long startSeconds;
unsigned long currentSeconds;
unsigned long periodInSeconds = 10;

void setup()
{
 Serial.begin(115200);
 while (!Serial);
 startSeconds = now();
}

void loop()
{
 currentSeconds = now();
 if (currentSeconds - startSeconds >= periodInSeconds)
 {
   Serial.println("time to do something");
   startSeconds = currentSeconds;
 }
}

Hello thank you all for your replies!
Ive tested them all but its not actually what I was looking for. Ive found an answer to my question here
calculating time using RTC module - Programming Questions - Arduino Forum (sorry for the new thread)

Why did you start a second topic ?

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