How to use time in if statements

Hey
I have a DS3231 time module and I want to make events happen based upon the time, but it can't read it as a variable and doesn't work. "invalid use of member function"

if (rtc.getTimeStr == 20:00:00){
}
thanks

What data type does rtc.getTimeStr() return ?
What data type is 20:00:00 ?

Hi there!

I have never used strings in Arduino before so forgive me if I say something wrong.

With string elements, a simple "==" command does not always work as intended, and some setup is needed before it will operate correctly. To compare two strings, the strings must be set up properly, like shown below.

stringTime = String("20:00:00");

stringRTC = rtc.getTimeStr; //Assuming this command returns a string

if(stringTime == stringRTC)
{

}

Try this and see if it works

willman:
Hey
I have a DS3231 time module and I want to make events happen based upon the time, but it can't read it as a variable and doesn't work. "invalid use of member function"

if (rtc.getTimeStr == 20:00:00){
}
thanks

Just guessing, but two things look sus. First, the syntax of the function call should be

rtc.getTimeStr()

Note the brackets.

Second, it looks like rtc.getTimeStr should return a string, so the comparison would look like this

if(rtc.getTimeStr() == "20:00:00"){}

Note the quotes.

Pogo

You haven't called the method, you've compared it!

To call the method, which is what you want, do this: rtc.getTimeStr()

string constants are surrounded by double quotes: "20:00:00"

If it returns a string, use strcmp. strcmp - C++ Reference

  if (strcmp (rtc.getTimeStr(), "20:00:00") == 0)
  {
    ..
  }

This still won't do what you want, of course, as the test will fire every time round loop for a whole second
(which could be thousands of times). Study the StateChangeDetection example in the IDE.

You don't use == for string comparison, as its pointer equality, not value equality.

Assuming you get that all to work one way or another, I'd caution against checking time to the second. Although a second is aaaaages to a uC, and it's likely that loop() is loop()-ing quickly enough, it's not impossible that you might not check the time during that second. (I say that not knowing what the rest of the code does: there may (for instance) be use of delay()...)

I'd think it safer to look for the time to be between two limits.

On the other hand, it may be that loop() is going so well that the test passes 100000 times, and you do the event over and over during that second. To prevent that, you need a flag variable which would start false, and only do the event if the time is right and the flag's false. Then when you do the event, make the flag true so if the time is checked again in the same second, the flag's state will prevent the event firing again. Set it false some time later so it works again tomorrow.

I have never used strings in Arduino before so forgive me if I say something wrong.

and you aren't using them now.

There is a very important distinction to be made between Strings (capital S) and strings (lowercase s)

A String is an object created using the String library. Using them can fragment memory causing bugs that are difficult to track down. This is exacerbated by the limited amount of memory available on most Arduinos.

A string is a zero terminated array of chars. Using them causes no memory fragmentation

Do not use the "String" class - it will cause memory fragmentation and cause your gizmo to lock up. In stead of relying on strings, use numeric values:

Time t = rtc.getTime();
if ((t.hour == 20) && (t.min == 0) && (t.sec == 0))
{
  Serial.println(F("Eight 'o clock, mate!"));
}

That is.. If you are using this library.

Life will be very much easier if the time can be represented as the number of seconds since some time in the past - maybe midnight, or maybe 10 years ago.

If the RTC library cannot produce a value like that then IMHO it is worth converting the hours minutes and seconds.

Then you can check the time with simple comparisons of two unsigned long values.

...R

I have a DS3231 time module and I want to make events happen based upon the time

By far the easiest way to do this is to use the Arduino time library (TimeLib.h), which internally keeps track of time in seconds since Jan 1, 1970.

Time comparisons then become a straightforward "if" statement, and importantly, the RTC can be used to automatically update the internal time, so you don't have to constantly call the RTC functions to get the time.

By far the easiest way to do this is to use the Arduino time library (TimeLib.h), which internally keeps track of time in seconds since Jan 1, 1970.

If you are going to use the TimeLib.h, I strongly recommend that you change the rtc library used to Jack Christensens DS3232RTC. This library works with the DS3231 as well.

It is available through the library manager, is designed to integrate with the Time Library, works for all the functions of the DS3231, is bug free, and in my experience is the best library to use with the DS3231. The syntax is the same as the very commonly used basic DS1307 library by Margolis and sketches are cross compatible.

Second, it looks like rtc.getTimeStr should return a string, so the comparison would look like this
Code: [Select]
if(rtc.getTimeStr() == "20:00:00"){}

Note the quotes

Thanks Pogo your code was the closest and really play, I played around with it and found thee code that actually work!

String timeString = rtc.getTimeStr();
if (timeString == "20:00:00")
{
}

found thee code that actually work!

It will work if you happen to read the clock at exactly 20:00:00. You won't have much luck with that approach in the long term, though.