Simple problem that I cant seem to solve

I have a simple timer using millis() and I want to create a string of the form “mm:ss.sss” to be sent to an LCD later. The minute prints to serial just fine, but the seconds displays garbage values from what I can tell. I believe my arithmetic is correct, am I using the wrong format specifier in this case, or could it be a bad mix of data types?

sketch_jan09a.ino (316 Bytes)

void setup() {
  Serial.begin(1200);
}

void loop() {
  unsigned long time = millis();
  double seconds = time/1000.0;
  int minutes = seconds/60;
  seconds = seconds-minutes*60;
  char timeSinceStart[100];
  sprintf(timeSinceStart, "%02d:%06.3d", minutes, seconds);
  Serial.println(timeSinceStart);
}

In retrospect I probably could've just posted this instead of attaching a file.

02d:%06.3d

seconds is an integer. No decimals.

TheMemberFormerlyKnownAsAWOL:

02d:%06.3d

seconds is an integer. No decimals.

seconds is a double. "%06.3f" would be correct, but Arduino's printf doesn't support floating point by default, so it still won't work even with the format string corrected.

Try this:

  sprintf(timeSinceStart, "%02d:%02d.%03d", 
      minutes, (int)seconds, (int) ((seconds - (int) seconds) * 1000));