[SOLVED] Display time variables

Hi,

I have been struggling with an issue for a while:

I want to display time on an LCD screen. The variable VData.MNTime is a time in seconds. This could be few seconds or thousands.
I calculate how much hours, minutes and seconds this makes and I display the variables on the LCD screen.

The result is that seconds and minutes are working fine. But hours won't appear at all :

This exemple should indicates: Tn: 2: 7: 5

This one should indicates Tn: 2:29:43

Here is my code for calculation and display:

int ss;
    int mm;
    int hh;
    int tt = VData.MNTime;

    ss = tt % 60;
    tt = (tt - ss)/60;

    mm = tt % 60;
    tt = (tt - mm)/60;
    
    hh = tt;

    char h[2];
    dtostrf(hh, 2, 0, h);
    char m[2];
    dtostrf(mm, 2, 0, m);
    char s[2];
    dtostrf(ss, 2, 0, s);

    writeLCD("Tn: ");
    writeLCD(h);
    writeLCD(":");
    writeLCD(m);
    writeLCD(":");
    writeLCD(s);

Thank you for your help.

Post a complete program that compiles so we can test it.

Why do you need to convert the values to strings ?

Your character buffers used in dtostrf() look one character shorter than needed to allow for the terminating 0 of the string

Thank you for your quick answer.

gfvalvo:
Post a complete program that compiles so we can test it.

Here here my folder.

UKHeliBob:
Why do you need to convert the values to strings ?

Your character buffers used in dtostrf() look one character shorter than needed to allow for the terminating 0 of the string

It is some exemple code working to display a single variable so I kept the same logic.
But any other working way is fine to me.

HandShake.ino (211 Bytes)

Input.ino (963 Bytes)

LCD.ino (534 Bytes)

output.ino (8.22 KB)

SerialCOMS.ino (1.97 KB)

TabbKSP-6.ino (7.9 KB)

utilities.ino (14.1 KB)

tabb:
Here here my folder.

Very few people want to wade through a bunch of irrelevant code. Best to make an MRE.

UKHeliBob:
Your character buffers used in dtostrf() look one character shorter than needed to allow for the terminating 0 of the string

I fixed this thanks to you with these changes :

char h[3];
dtostrf(hh, 2, 0, h);
char m[3];
dtostrf(mm, 2, 0, m);
char s[3];
dtostrf(ss, 2, 0, s);

Thanks a lot !

Is everything displaying OK now ?

Yes, perfectly.

Just to polish it, is there a way to add "0" so that values inferior to 10 are displayed like that "04" instead of " 4" ?

if (val < 10) {
  device.print('0');
}
device.print (val);

is there a way to add "0" so that values inferior to 10 are displayed like that "04" instead of " 4" ?

Of course. You could use the sprintf() function to build the char array

You did not answer my question as to why you are converting the ints to chars. Incidentally, why are they ints not bytes in the first place ?

UKHeliBob:
Of course. You could use the sprintf() function to build the char array

I just had a look at this function, but I don't get how it work.
Do you have a simple example to help me understand ?

UKHeliBob:
You did not answer my question as to why you are converting the ints to chars. Incidentally, why are they ints not bytes in the first place ?

I assumed I needed to do this to be able to display them on the LCD.

void setup()
{
  char buffer[3];
  Serial.begin(115200);
  while (!Serial);
  for (byte hour = 0; hour < 20; hour++)
  {
    sprintf(buffer, "%02d", hour);
    Serial.println(buffer);
  }
}
void loop()
{
}

As suggested by AWOL it is easier to understand if you simply test the value of the variable and print a zero if necessary

I assumed I needed to do this to be able to display them on the LCD.

You don't