Avoid Rollover in calculation of kWh

Hello all! I'm doing a project in which I have a power meter... The formula of energy is

E=(power * time) / (3600.0 * 1000.0)...

How can I avoid the rollover, so that the time doesn't return to zero?

How can I avoid the rollover, so that the time doesn't return to zero?

Do you have a watch? Does the fact that it rolls over every day, or even twice a day, cause you any heartburn?

How can I add the time before rolling over, to the one after the rollover?

It's more normal to subtract the time before the rollover from the time after.
That's generally the way time works.

How can I add the time before rolling over, to the one after the rollover?

Without the result rolling over? Why do you think rollover is an issue?

Get over the idea that ADDING time is reasonable. Now - then always works (well, as long as now and then are less than 7 weeks apart). Now + someTime is not guaranteed. So, don’t do it.

Yes, that's what I meant to say...how can I do that?

What have you tried?

No, because I'm not fully understanding how I define that times after and before rollover..

I have to know the exact time to it rolls over, right?

amlo13:
I have to know the exact time to it rolls over, right?

No. If you record the time 35 seconds before rollover, and again 18 seconds after, and subtract the earlier time (a large number) from the later time (a small number), you will still get the correct interval.

amlo13:
I have to know the exact time to it rolls over, right?

it rolls over exactly between 0xffffffff and zero, always.

Accumulate the energy in time intervals, or steps:

static unsigned long last_time;
static bool     first_time = true;
static float    E, power;

void loop_or_something()
{
  unsigned long current_time = millis();

  if (first_time)
    first_time = false;
  else {
    float dT = (current_time - last_time);
    float dE = (power * dT) / (3600.0 * 1000.0);
    E += dE;
  }

  last_time = current_time;
}

This works across the rollover.

Cheers,
/dev

This type of application implies extended periods of operation as the energy is totalized, hence rollover of the time variable would need to be handled. May even need to consider rollover of accumulated energy. I've seen where this happened with an electricity meter and the customer got a nasty $10K+ bill.

    float dT = (current_time - last_time);

float? WTF?

/dev,

after watching more carefully your sketch, I cannot understand how this will avoid the rollover…

If millis is is the last second before rollover, last_time will have this value! When going back to the begining current_time will be equal to zero and so dT will be calculated by

0-last_time… won’t it be a negative value?

/dev,

after watching more carefully your sketch, I cannot understand how this will avoid the rollover..

If millis is is the last second before rollover, last_time will have this value! When going back to the begining current_time will be equal to zero and so dT will be calculated by

0-last_time... a negative value?

How can you have a negative value in unsigned arithmetic?
(Don't bother answering that, it was a rhetorical question)

Sorry, but I'm not understanding how this will avoid rollover.. :confused:

Rollover is inevitable, so there's no point in trying to avoid it.

Sorry, but I'm not understanding how this will avoid rollover.. :confused:

IT IS IMPOSSIBLE TO AVOID ROLLOVER. Get over that silly idea. It IS possible, and even trivial, to avoid PROBLEMS when rollover happens.