Countdown Timer Accuracy

I’ve got several Arduino projects going on all at once, but I wanted to see if I could tackle this one a little faster. From the obvious title I want to design and build a countdown timer that is accurate as possible. I know timing from the ATMEGA328 is not always entirely accurate when working on several tasks. So what I’m looking for is suggestions to improving the accuracy through coding/hardware as I’ve seen a lot have people use different methods.

What I plan for the timer to use so far…

4xQty 7 segment displays (common cathode)
4xQty 74HC595 shiftout registers
1xQty 12Vdc strobe light
4xQty momentary contact switches (either tact or micro)

So essentially I’ll have used 8 pins already…

The timer when powered on will automatically set itself to 10:00 (mm:ss), and will allow the user to increase or decrease that time minute by minute. The timer can be started or stopped using the same button, and reset via another. Within the last 60 seconds the timer will pulse the strobe light once as a warning, and when it reaches 00 the strobe will flash continually until the reset button has been depressed.

For the strobe I know I will need a transistor to control the higher voltage 12Vdc circuit. The strobe itself flashes 90 to 110 every minute so just flipping a high pulse to the transistor for half a second should allow only one flash to go off, but I’m not to specific about how many flashes really.

Right now I’m learning coding for using the 7 segment displays with the 595’s, but I will still need something to help provide accurate timing of the countdown portion of the coding. I’d rather have a method that doesn’t involve using a shield or breakout board because I want to later design this into it’s own PCB layout which I will have printed from a circuit board manufacturer. Unless, the individual parts of that shield/breakout are easy to find and use on mostly a through hole type config.

Here is a sample code I made for countdown of one 7 segment display using a user made library to control the 595 with less coding on screen.

#include <AH_74HC595.h>  //shift register 7 segment display library

#define SER_Pin   11   //Serial data input
#define RCLK_Pin  8   //Register clock
#define SRCLK_Pin 12   //Shift register clock


AH_74HC595 seven_segment(SER_Pin, RCLK_Pin, SRCLK_Pin);  //initialize

void setup(){
 seven_segment.clear(); //clears display
}

void loop(){
  for (int N = 9; N > 0; N--){
    seven_segment.showNumber(N);  //shifts out number
    delay(1500);
  }
  seven_segment.clear();
}

Once I learned his pinouts used it was real easy to program…though within the last minute I refined the code using a “for” statement instead of two “if” statements. I’m picking back up on my programming class lessons…glad I took C++.

Take a look at blink-without-delay in the examples, it is a lot more accurate

Boffin1: Take a look at blink-without-delay in the examples, it is a lot more accurate

Would you suggest using this coding to possibly send a high or low state to an input pin to account for timing accuracy? Or are you suggesting something only coding implemented with no additional hardware needed?

It's 3 am so I may not be thinking about this correctly lol.

So the millis() function returns the amount of time since the program started in milliseconds....hrm

I suppose that would work, but lets say I wanted to make a power on test to be performed in our setup() before beginning the loop(). Could that throw off it's timing accuracy?

I dont know how accurate you want, but if you are only displaying down to seconds, over 10 minutes, the timing should be very accurate .

I use this sort of timer to countdown sports clocks for 90 minutes say, and its accurate to within a second. ( including any pauses by the ref )

Think of it as noting the time of a wall clock when you start a timed task, adding your interval you need, and then checking for this time every now and again as you get on with other work.

With the sketch it will be checked every loop, many times a second, and the new time noted.

If you choose your interval as 1000 it can deduct a second from your time, and reset the counter for the next second.

There are examples you can search for, but give it a try first, you don't need any hardware, just get the Arduino to serialPrint the time every second, and you can check it on the monitor.

I know timing from the ATMEGA328 is not always entirely accurate when working on several tasks.

It is not entirely accurate due entirely to poor programming.

So what I'm looking for is suggestions to improving the accuracy through coding/hardware as I've seen a lot have people use different methods.

With an interrupt, it can be as accurate as the crystal used over a long period of time.

rikitheshadow: I know timing from the ATMEGA328 is not always entirely accurate when working on several tasks. So what I'm looking for is suggestions to improving the accuracy through coding/hardware as I've seen a lot have people use different methods.

The common way of handling timing within Arduino gives you millisecond resolution, which is a couple of orders of magnitude better than you need.

Poor sketch design can prevent the sketch from noticing timed events have occurred promptly (it's no good having a millisecond resolution timer if you only check it once an hour) but there are well known and very simple techniques to design your code so that it handles timed events promptly. In Arduino terms, the sort of timing you're describing will be very easy to achieve.