Int overflow?

Hi,

I’m communicating with an arduino over a serial connection, and one of the function in my sketch sends back some condensed information to the computer. After ~10 hours, the calculated minutes on the arduino appears to overflow.

I’ve attached a screen shot of the resulting printout in PuTTY.

////////////////////////////////////////////////
// Statuser - Sends system status to Serial
/////////////////////////////////////////////
int statuser ()
{
  wdt_reset();
  Serial.println("$");
  Serial.println("[Start]"); //Start Of Transmission
  delay(15);
  unsigned long currentSeconds = (millis()/1000);
  refresh();
  Serial.print("-Time Alive: ");
  int hr = (currentSeconds/3600);
  int mn = (((currentSeconds)-(hr*3600))/60);
  int sc = ((currentSeconds)-((hr*3600)+(mn*60)));
  Serial.print(hr);
  Serial.print(":");
  Serial.print(mn);
  Serial.print(":");
  Serial.println(sc);
  Serial.print("-Num of Program Cycles: ");
  Serial.println(progCycles);
  Serial.print("-Avg Cycles per Second: ");
  int cps = (progCycles/(currentSeconds));
  Serial.println(cps);
  Serial.print("-DoorState: ");
  Serial.println(doorState);
  Serial.print("-Bitch Seconds: ");
  if (bitchTime == 0)
  {
    Serial.println("Not Active"); // The timer isn't running
  }
  else
  {
    Serial.println((bitchTime - millis()) / 1000); // Print the number of seconds left before the door locks
  }
  Serial.print("-Servo Position: ");
  Serial.println(servState);
  Serial.print("-Last Serial Byte: ");
  Serial.println(lastSerial);
  delay(15);
  Serial.println("[End]"); //End Of Transmission
  return(0);
}

I’d appreciate any input on what may be causing the problem, or suggestions on debugging.

Thanks,
-BBX

hr*3600 will overflow - read "integer promotion".

Your way to calculate time is not optimal.

The best is to use time.h - not available here.

the 2nd best is to calculate hr/min/sec separately:

  tmp = millies() / 1000; //cumulative time
  time.sec = tmp % 60; //calculate seconds
  tmp /= 60; 
  time.min = tmp % 60;  //calc minutes
  tmp /= 60; 
  time.hour = tmp % 24; //calc hour
  time.day = tmp / 24; //calc day

It will not overflow.

dhenry,

Thank you for the correction and clarification!

I couldn't find any resources matching "integer promotion", but I believe I understand what you're saying, between unsigned long integers and the standard integer definition.

The code you provide does make a lot more sense than the system I tried to use, I'll adjust my sketch and set the arduino back up tomorrow.

Thanks!,
-BBX