converting microseconds into readable time

Hello!!!

I'm programming a swimming stopwatch with arduino.
The problem I'm facing now is that the time is exported to the serial monitor in miliseconds. Is there a way to display it in minute.seconds.1/100seconds ? For example: 0.29.18 in stead of 29180.

Is there someone who knows this? :slight_smile:

Greetings from The Netherlands,

Thijs

This is a start, but is does not reserve a fixed number of digits when displayed (i.e., 125232 is displayed as 2.5.232. not 02.05.32). You need to figure that out.

void setup() {

  Serial.begin(9600);
  
}

void loop() {
  char input[10];
  int charsRead;
  unsigned long mil;
  
  if (Serial.available()) {
    charsRead = Serial.readBytesUntil('\n', input, sizeof(input) - 1); // collect number of millis
    input[charsRead] =  '\0';     // make a string
    mil = strtoul(input, NULL, 10);  
    DisplayAsTime(mil);  
  }
}

void DisplayAsTime(unsigned long mil)
{
  int minutes;
  int seconds;
  int hseconds;

  Serial.print("Total milliseconds = ");
  Serial.println(mil);
  minutes = mil / 60000L;   // extract minutes
  mil -= minutes * 60000L;
  seconds = mil / 1000L;     // extract seconds
  hseconds = mil - (seconds * 1000L);

  Serial.print(minutes);
  Serial.print(".");
  Serial.print(seconds);
  Serial.print(".");
  Serial.println(hseconds);
}

The number of microseconds since the Arduino started is not a time.

If you want to display the number of microseconds since the Arduino started in minutes, seconds, milliseconds, etc. that's simple math.

it's a stopwatch, not a clock, so he simply needs to register the start/end values for the event.

Thank you! I think I can use that!
When you push the start button, the current millis() is stored into an int-value. There are three buttons (lane one, two and three), en when these buttons are pushed the difference between the int-value en the current millis() is stored in an int called finishtime1 / finishtime2 and finishtime3.
I think I can use your method to display finishtime as a human readable number. Thank you!

When you push the start button, the current millis() is stored into an int-value.

Shouldn't that be an unsigned long value ?