• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Time Display strange issue

Status
Not open for further replies.

pigman

New Member
Hey,
I'm messing with this function I found a while back to display milliseconds in a countdown timer and it does something strange when I feed it 6 hours it displays 00:12:14 instead of 06:00:00. Anyone have any ideas, I didn't write it I found it online.
Cheers

Here is the code.

Code:
void print_time(unsigned long t_milli)
{
    char buffer[20];
    int days, hours, mins, secs;
    int fractime;
    unsigned long inttime;

    inttime  = t_milli / 1000;
    fractime = t_milli % 1000;
    // inttime is the total number of number of seconds
    // fractimeis the number of thousandths of a second

    // number of days is total number of seconds divided by 24 divided by 3600
    days     = inttime / (24*3600);
    inttime  = inttime % (24*3600);

    // Now, inttime is the remainder after subtracting the number of seconds
    // in the number of days
    hours    = inttime / 3600;
    inttime  = inttime % 3600;

    // Now, inttime is the remainder after subtracting the number of seconds
    // in the number of days and hours
    mins     = inttime / 60;
    inttime  = inttime % 60;

    // Now inttime is the number of seconds left after subtracting the number
    // in the number of days, hours and minutes. In other words, it is the
    // number of seconds.
    secs = inttime;

    // Don't bother to print days
    //sprintf(buffer, "%02d:%02d:%02d.%03d", hours, mins, secs, fractime);
    sprintf(buffer, "%02d:%02d:%02d", hours, mins, secs);
    lcd.print(buffer);
}
 

dougy83

Well-Known Member
print_time(1000 * 6 * 3600); displays 06:00:00 as expected...

EDIT: but as you're probably using Arduino, the standard 'int' is only 16 bits. Modify the lines for the hours to be
Code:
    // number of days is total number of seconds divided by 24 divided by 3600
    days     = inttime / (24*3600ul);
    inttime  = inttime % (24*3600ul);
It should be fine then. The problem was that 24*3600 is more than 16 bits.
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top