Clock Program Stopped

I've been tinkering around with a little program that will eventually go on a watch. It works perfectly fine for a while, but I left it running overnight just to see what would happen and it froze after 13 and a half hours. I think it may be my program, but I don't really know, here is some sections of my code:

void ClockLoop(){
  menu = 0;
  initTime = millis() + setTime;
  while(true){
    Clock();
  }
void Clock(){
  TIME = millis() + initTime;
  hr = ((TIME / 1000) / 3600) % 24;
  minute = ((TIME / 1000) / 60) % 60;
  sec = (TIME / 1000) % 60;
}

ClockLoop() just runs in loop(), and Clock() does what you see there and then sends it to the display I have farther down. Is the arduino just timing out or something? I read that the clock only overflows after 50 days so I don't think it's that. And yes, Time, initTime, and setTime are all of type long, so no overflow there (I don't think). Thanks!

p.s. - I'm redoing this tonight just to make sure it wasn't some fluke

(deleted)

Overflow and something else.

The millis() returns an unsigned long. If you do every calculation with unsigned long, you can eliminate the rollover trouble.
But you have to careful check every code line, that the result of a calculation may not get negative.
If you do it right, it should run forever.

I don’t know about that 13 and a half hours, I think this is another problem. Is there a device, like a display, that can hold everything ? The default Wire (I2C) library can be blocking in very rare situations.

I've been tinkering around with a little program that will eventually go on a watch. It works perfectly fine for a while, but I left it running overnight just to see what would happen and it froze after 13 and a half hours. I think it may be my program, but I don't really know, here is some sections of my code:

void ClockLoop(){
  menu = 0;
  initTime = millis() + setTime;
  while(true){
    Clock();
  }
void Clock(){
  TIME = millis() + initTime;
  hr = ((TIME / 1000) / 3600) % 24;
  minute = ((TIME / 1000) / 60) % 60;
  sec = (TIME / 1000) % 60;
}

ClockLoop() just runs in loop(), and Clock() does what you see there and then sends it to the display I have farther down. Is the arduino just timing out or something? I read that the clock only overflows after 50 days so I don't think it's that. And yes, Time, initTime, and setTime are all of type long, so no overflow there (I don't think). Thanks!

p.s. - I'm redoing this tonight just to make sure it wasn't some fluke

Time, initTime, and setTime are all of type long,

needs to be unsigned long

Please don't crosspost - putting the same question in multiple places. One topic, one discussion. Have a little patience when posting.

And you need to post the entire sketch so we can see how and where variables are defined.

...R

....And where any memory leaks might be.

CrossRoads:

Time, initTime, and setTime are all of type long,

needs to be unsigned long

And the constants do too.

  hr = ((TIME / 1000UL) / 3600UL) % 24UL;
  minute = ((TIME / 1000UL) / 60UL) % 60UL;
  sec = (TIME / 1000UL) % 60UL;