3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Arduino Binary Alarm Clock

Step 4Time keeping

To keep track of time I used only the arduino, and the standard arduino function millis(). This method won't be completely exact, because the 16Mhz crystal oscillator, that decides the clock frequency of the arduinos CPU, probably  won't be exactly 16 000 000 Hz. The good news is that you can measure how inexact your clock is and compensate for it in your code, because the offset should be constant for a given period of time.

Measure how inexact your arduino is:
As said earlier, the arduino will have a small time error, this error depends on the crystal oscillator and will be different for every arduino, to measure how much my arduino clock differed from the correct time, I programmed it to print out the time ( the hour, minute and second variables) via serial. I then let it run for a long time (Over night and more) and compared the arduino time with a clock I knew was exact, at the start and the end of my measuring period. Divide the time difference with the time the test took to calculate the error each hour. I found that my arduino is about 0.4 seconds to fast every hour. I used exacttimenow.com to get exact time, but if you have a watch you know is very exact, feel free to use that instead.
The code I used to keep the time is an adaptation of some code I found on the arduino forums. I rewrote it with if-statements instead of division and modulo, to see if there would be any speed difference and found that the if-version is more than 15 times faster (although both are still quite fast, more info about the test here).

Since I want the other stuff in my main loop (like checking the touch sensor, checking for button presses, etc.) to happen as often as possible, I used the faster version.

The code:

Every time the clock function is called it adds the time in milliseconds since last it was called to a variable m, when one second has passed (m>999) the second variable is increased by one and m is reset. When the seconds variable reaches 60, the minute variable will be increased by one, and seconds will be set to zero. The same thing happens with the minutes variable; when it reaches 60, add 1 to hours and reset minutes. The hour variable will be reset when it reaches 24.

To compensate that my arduino is 0.4 seconds faster evey hour, I decrease the seconds with two seconds every fifth hour.

____________________________________________________________________

The clock() function:

// CLOCK VARIABLES:
#define MAX_MILLIS_VALUE 34359738
unsigned long current_millis_value = 0;
unsigned long previous_millis_value = 0;
unsigned long m = 0;
int seconds = 0;
int minutes = 0;
int hours = 0;

void clock()
{
  current_millis_value = millis();
  if (current_millis_value < previous_millis_value) // if millis overflows
  {
    m += MAX_MILLIS_VALUE - previous_millis_value + current_millis_value;
  }
  else // if millis has not overflown
  {
    m += current_millis_value - previous_millis_value;
  }
  if (m>999)
  {
    seconds++;
    m = m-1000;
  }
  if (seconds>59) // if seconds == 60
  {
    minutes++;
    seconds = 0;
  }
  if (minutes>59) // if minutes == 60
  {
    hours++;
    minutes = 0;
    if(hours%5==0) // adjust the time with -2 seconds every 5th hour.
      seconds = seconds - 2;  // this will cause seconds to be -2,
                             // therefore seconds can't be unsigned.
  }
  if (hours>23) // if hours == 24
  {
    hours = 0;
  }
 
  previous_millis_value = current_millis_value;
}

______________________________________________________________________

« Previous StepDownload PDFView All StepsNext Step »
4 comments
Jan 10, 2010. 6:50 PMLaserman595 says:
When ever you do find out please post it so it would be possible to  compensate for it every 6 or so months.  =-)
Nov 26, 2009. 10:49 PMdaltore says:
Have you thought about using ternary operators?  They're basically if() statements, but use a slightly lower-level structure.  I'm not sure if they run faster under the Arduino, they may actually end up compiled the same since it's an embedded environment, but basically, it goes like this:

condition ? run if true : otherwise run this;

So you could do that first if() statement like this:

current_millis_value < previous_millis_value ? m += MAX_MILLIS_VALUE - previous_millis_value + current_millis_value : m += current_millis_value - previous_millis_value;

It's not as pretty, but as always, C ignores whitespace, so you can write it like this if you want to organize it a little:

current_millis_value < previous_millis_value ?
m += MAX_MILLIS_VALUE - previous_millis_value + current_millis_value :
m += current_millis_value - previous_millis_value;

It might run faster and eliminate some of the timing error.

Also, you might be able to spread out the time correction a little more evenly than 2 seconds every 5 hours.  Since it's .4 seconds, that's 400 milliseconds every 60 minutes.  That goes down to 20 milliseconds every 3 minutes.  Since you're counting milliseconds directly, and the overall count of seconds is a multiple of 3, it might be overall a little more accurate to the user to do the correction more often.

Great tutorial though, I might have to try this!
Nov 27, 2009. 5:13 PMdaltore says:
Leave it unsigned.  You could just as easily have it count up to 1001 instead of 999.  This would save issues of the clock wondering what time it really is.

Yeah, I'll have to see how ternary operators and if() statements compare when I finally get an Arduino (Christmas is coming, as my plan currently stands).  For a lot of embedded environments, all those conditional structures get compiled down the same way anyway.  I think I recall that the C18 compiler (for PIC microcontrollers, like the one in my VEX robot) compiles if() and switch() statements to the same thing.  I wouldn't be surprised if it takes while() loops down to if()-goto structures.  Or I guess I could just look at the avr-gcc manual, but that would be too easy :D

I think I might eventually try this project, I've wanted a binary clock for a while, and I love the AVR line.  One thing that I found last night is that the Arduino bootloader still allows you to use the lower-level avr-gcc commands.  It turns out that digitalWrite() takes about 10 times longer to run than accessing the PORT register directly, because with each call of digitalWrite(), it turns off all of the PWM timers (and in the comments in the code, the guy is asking why that couldn't be moved up to pinMode(), which is interesting).  Basically, digitalWrite() is just some safety mechanisms and a parser for the PORT register.  It sets the PORT of your choice (lets say pin 13, which is under PORT B, pin 5) to an entire byte all at once (you can toggle 8 pins just about simultaneously).  This would work like this, IIRC:

PORT_B = 0b00010000;

Or, to only affect that pin, you can use the | operator:

PORT_B |= 0b00010000;

You don't have to make these modifications, I realize it would take a long time to recalibrate, just thought you might be interested.  I still love the tutorial.  I think it might also be interesting to make a multiplexed or charlieplexed version, just for funsies.
Nov 29, 2009. 4:11 PMdaltore says:
Oh, yeah, with the "1001 instead of 999" thing, I was thinking 2 milliseconds instead of 20.  And actually, you could tack on 7 milliseconds every minute, and then 6 every 3 minutes, like so:

if(seconds==59 && m > (!minutes%3 ? 999+6 : 999+7)) {  //If divisible by 3, add
    seconds++;                                                                              //18 ms, otherwise 19
    m = 0;
}
else if(m>999) {  //Normally, a second is just 1000ms
    seconds++;
    m = 0;
}

I think how you do it with the 2 seconds every 5 hours is fine by the way, this is more just a point of discussion.
Nov 15, 2009. 4:12 PMsimonrichards150 says:
Just wondering, why didn't you use a RTC for timekeeping? Nice guide btw.

-Simon
Nov 17, 2009. 5:57 PMcyrozap says:
Yeah, it'll start drifting by a lot after a while. You should use this. I read that it is very good. You can even get one that is the electrical equivalent, but the headers are soldered on the wrong way, for half of the price.

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
2
Followers
1
Author:njakol