time counter

I want to make a 7 segment display that will count up since it is terned on I want it to count minutes hours days and years
Im just asking advise on how I can do the time counting like a rtc or something
And has anyone else done something similar so I can modify the code
I want the years to be double digits so it will have to count a long time
It will be I'm a case and pluged in I will put and small sort of ups with a battery that chagers when it is pluged and uses the battery when the power is of

Thanks for any help

You can use the millis function, which is the number of milliseconds since the Arduino was powered or, or the millis counter resets (every 54 days or so). To get the number of seconds, divide by 1000. From there you divide by 60 to get number of minutes, etc. You will have to account for months, and ultimately leap years.

You can get real time clocks with their own battery that you can attach to your Arduino to keep track of time even when the Arduino is powered off. One such vendor is: http://arduino-direct.com/sunshop/index.php?l=product_detail&p=278.

If you have a GPS unit attached, the GPS stream includes an accurate time stream.

If your arduino is connected to the internet either via the ethernet sheild or a wifi shield, it can presumably do a network time request to get the initial time when booting up.

I personally would use a MAX7219 chip that can drive 8x 7 segments. See here for info Arduino Playground - HomePage and couple this with an accurate RTC module based on something like the DS3231 (Not DS1307) ChronoDot V2.1 High Precision RTC - Macetech Electronics Store Battery backup of time from something like this is a must as chances of device running for years without power glitch is slim.

I am going to use the max7219 to drive the segments as I already have one
I just have one question
I know how to set the time on a rtc but how would I make it tell me how long since it was turned on

I would use something like this :

unsigned long lastSecond = 0;
int seconds = 0;
int minutes = 0;
int hours = 0;

if(millis() - lastSecond >= 1000)
{
    lastSecond += 1000;
    seconds++;
}
if(seconds > 59)
{
    seconds = 0;
    minutes++;
}
if(minutes > 59)
{
    minutes = 0;
    hours++;
}

i will use that code but change it so it will run more then 50 days because millis resets to zero after about 50 days

i just have 1 more question
i know how to print 1 number at a time on a 7 seg with a max7219

mydisplay.setDigit(chip, digit_position, number, dot);

but how do i display a more then 1 digit number

Even though the value stored in millis() rolls over after a certain length of time, you're only checking to see if it's increased by 1000... so it doesn't matter! It's not as if your code will drop dead as soon as it rolls over, it'll just start ticking over from 0 again and your other variables will stay how they were.

To print more than one number, just repeat that statement! The 7219 will latch it's digits so they won't change until you write something else to that location. I did have some example code somewhere, but I can't find it :frowning: Sorry.

josh97531:
i will use that code but change it so it will run more then 50 days because millis resets to zero after about 50 days

No, do NOT change it. As given, it correctly handles the rollover situation. (Whether your changed version would handle it correctly, I have no idea.)