Comparing time on different days

I am controlling some relays based in Minutes Since Midnight (MSM)

  if (msmNow >= msmStart && msmNow < msmStop)
  {...

When the Start time is greater than the stop time all is OK, but if the Start time is say 700(MSM) and the Stop is 150(MSM) the next morning that code fails. I did some searching but can't find a good search term for what I think I need. :slight_smile:

This does not seem like an elegant approach... or is it?

  if (msmStart > msmStop)
  { msmStop += 24;
  }
  if (msmNow >= msmStart && msmNow < msmStop)
  {...

If your minutes roll over at midnight I think you can calculate the length of the interval by subtracting the recent time from the earlier time. Try it with a simple Arduino program. It may only work with byte or unsigned int values (ie rollover at 255 or 65535).

Look at how this is dealt with using millis() in the demo Several Things at a Time

If that does not deal with your problem (or does not work for your values) you will need a flag to signal that there has been a change of day.

...R

I've run into this one before. You need an if statement that checks if the start time is less than the end time. In each case you calculate the length of time differently.

Use seconds-since-1970?

These snippets tells us nothing about how your msmNow value is measured or derived. "minutes since midnight" suggests to me that the value should be the amount of minutes since the last midnight, resetting to 0 at the start of each day. The fact that it isn't working at the immediate next morning suggests that that is not the case, and it is probably "minutes since this specific midnight on this specific day", and it never resets to 0. This kind of thing is why the rules require posting a full sketch that demonstrates the problem.

I'm just spitballing though. I failed the remote viewing and telepathy classes in college.

Jiggy-Ninja:
These snippets tells us nothing about how your msmNow value is measured or derived.

Stay calm jiggy. Minutes since Midnight are the number of minutes since Midnight. There can only be one version of that since there is only one Midnight in my 24-hour days. But if it really matters with the problem here's the Black Magic and Voodoodoo to how I do it.

#include <time.h>
time_t timeNow;
uint16_t msmNow;

void setup()
{ msmNow = (hours(timeNow) * 60) + minutes(timeNow);
}

And the answer is...

When the Start time is greater than the stop time all is OK

Why is this "all OK", and if so, why should it fail the next day?

MarkT:
Use seconds-since-1970?

DUH! Slapping my forehead, thanks Mark.

I already have that from the time Library. I'll just drop the seconds. In fact I won't even bother with that as 59-seconds error either way will not matter.

I am old is my only excuse. :slight_smile:

I have code like this in my fishtank program. I use seconds since midnight. The code has a moonrise time, a moonset time, and a current time.

Whenever the moonrise or moonset time is altered, I compute the length of the night in seconds

    void deriveNightlen() {
      nightLenSec = (moonsetSec - moonriseSec + SECSPERDAY * 2) % SECSPERDAY;
    }

I add SECPERDAY before taking the modulus to force moonsetSec - moonriseSec to be a positive value (because the modulus operator does odd things with negative values, and making it unsigned won't help). I multiply SECPERDAY by 2 just to be on the safe side :slight_smile: .

Once the night length is calculated, the decision as to whether its daytime or night time is here

  void drawMoon() {
    pixels.clear();

    long tt = (timeOfDaySec - moonriseSec + SECSPERDAY * 2) % SECSPERDAY;

    if (tt <= nightLenSec) {
      // moon gets drawn here
    }

    pixels.show();
  }

The full project is here.

WaitSome:
Stay calm jiggy. Minutes since Midnight are the number of minutes since Midnight. There can only be one version of that since there is only one Midnight in my 24-hour days. But if it really matters with the problem here's the Black Magic and Voodoodoo to how I do it.

#include <time.h>

time_t timeNow;
uint16_t msmNow;

void setup()
{ msmNow = (hours(timeNow) * 60) + minutes(timeNow);
}




And the answer is...

You have a problem with your code. You aren't able to fix that problem because you don't know what or where it is. That's why you come here to ask.

We know even less about your code than you do. We didn't write it, and it's our first time looking at it. That's why we need the whole picture.

First you show us a snippet that uses variables of unknown origin and unspecified meaning. We have to infer what they are for from the name. You're also adding some weird constant to one of the values, with no explanation for why you are doing that and how you came up with that value. There's also nothing obviously wrong with the window check you're doing below that.

I asked you to post the full sketch in bold. You apparently didn't read past the first sentence, and posted another snippet, one that shows you setting the msmNow variable in setup(), which is only ever run once and never again for the duration of the program. Is that the problem? I don't know, because I don't see where else you're updating this value because you refuse to post the full program.

There is a limit to how much obtuseness I'm willing to put up with.