Evaluating enum for if statement

Hi, I think this code (see loop) runs regardless of the state of the timeStatus() enum. If I lose time sync I want it to stop (and eventually do something different). Code attached as I assume it’s too long for tags. I had to use numbers instead of enums for my case at line 188 to work. What am I not seeing?
Sorry, meant to say. Uno WiFi Rev 2.

lights.ino (9.71 KB)

There is no ENUM defined in your program and timeStatus() appears to be a function but it is not in your program - is it in one of your libraries?

...R

In the time.h library it appears the enum is defined as follows:

typedef enum {timeNotSet, timeNeedsSync, timeSet

}  timeStatus_t ;

Therefore timeNotSet = 0, timeNeedsSync = 1, timeSet = 2

Yes, the enum is in the Time.h (actually in the TimeLib.h that's called from Time.h as far as I can tell).
So why does the if statement in my loop keep evaluating as true if the enum is timeNotSet? I triggered this condition by killing my WiFi and waiting for a sync to fail.

Modify your loop as follows and see what it prints:

void loop() {
  if (timeStatus() != timeNotSet) {
    Serial.print("Time status is: ");
    Serial.println(timeStatus());
    digitalClockDisplay();
    statusDisplay();
    digitalWrite(LED_BUILTIN, CHANGE);
    Alarm.delay(1000);
  }
}

Done that. It spits out '2' for when there is a time sync and '1' when it's not synched. Not the enum 'timeSet' etc. that one would expect. I had to use numbers like that in my case statement.
The main loop of this code was lifted from Time library examples. Do you think it makes any difference if you inlclude TimeLib.h rather that just Time.h? (Edit- it didn't)

siddyboy:
Done that. It spits out '2' for when there is a time sync and '1' when it's not synched. Not the enum 'timeSet' etc. that one would expect. I had to use numbers like that in my case statement.
The main loop of this code was lifted from Time library examples. Do you think it makes any difference if you inlclude TimeLib.h rather that just Time.h? (Edit- it didn't)

2 for timeSet and 1 for timeNeedsSync is correct. If you do a Serial.println(timeStatus()) you get an integer because enums are basically integer constants. Therefore, I don't see a problem in what it is printing out.

I do see a problem in your case statement. You were associating the wrong integers with the enum values. Your case statement should read like this:

  switch (timeStatus()) {
    case timeNotSet: // 0
      Serial.print("timeNotSet");
      break;
    case timeNeedsSync: // 1
      Serial.print("timeNeedsSync");
      break;
    case timeSet: // 2
      Serial.print("timeSet");
      break;
    default:
      Serial.print("Unknown State");
      break;
  }

Yes, agreed. Problem solved. Thanks. A combination of my misunderstanding of enums starting at zero and the double negative logic in loop().

if (timeStatus() != timeNotSet) {...