RTCTime and getMinutes() programming problem

Can someone please tell me what I am doing wrong here. This for an Arduino Uno R4 using the embedded RTC.

  RTCTime currentTime;
  RTC.getTime(currentTime);
  Serial.println(currentTime.getMinutes());
  if (currentTime.getMinutes() % LOG_INTERVAL == 0) Serial.println("hello");

I have reduced the code down to the minimum to try and work out what is wrong but it always fails on "Compilation error: expected primary-expression before '==' token"

Should that be

if ((currentTime.getMinutes() % LOG_INTERVAL) == 0) Serial.println("hello");

How is this defined? You left that detail out of your snippet...

Post the whole code. You've obviously put the part you think is right. Now put the part where the mistake is. If you don't know what the mistake is then post the whole thing.

I'd bet money you've got a semicolon at the end of a #define.

2 Likes

No, the extra brackets are not needed, % has a higher precedence than ==

@johnboldon how is LOG_INTERVAL defined?

Thanks for your suggestion. Actually I have tried all combinations of brackets.
Your version fails with "Compilation error: expected primary-expression before ')' token" It seems to me that it's the "primary-expression" which is the problem.

Maybe not needed, but it makes the intention of the code more obvious. Albeit, it is not the problem in this case

Thank you so much all. That was the problem. I had
#define LOG_INTERVAL
instead of
#define LOG_INTERVAL 15
Thanks again. I just couldn't see the problem which should have hit me in the face!

Not sure if will work correctly, tho. The condition will be true for a full 60 seconds, which could result in multiple updates to the log, unless you use millis() or delay() ( :face_with_peeking_eye: ) to avoid that.

You are right of course. I had cut down the sample of code as I was focussed on why the if wasn't working. My stupid mistake!
This is how the code is going to be structured.

if (currentTime.getMinutes() % LOG_INTERVAL == 0) {
    if (!logged) {

      //Log data
      //logFile.print(currentTime);
      //logFile.print(",");
      //logFile.println(Temperature);
      //logFile.flush();
      Serial.print(currentTime);
      Serial.print(",");
      Serial.println(Temperature);
      logged = true;
    }
  } else {
    logged = false;
  }

Thanks again!