Linear Clock
22 Steps

## Step 12: Prototype version - software

The software isn't complicated, but make sure you have the most recent version of Adafruit's own Motorshield driver.  There's a fix in it that is important for this code.

The code:
There is an internal time representation, consisting of three ints: currentSeconds, currentMinutes and currentHours.  This time is pre-set at start up and incremented while the program runs.

The main loop of the program makes a note of the start time, then continuously checks the current time to see if it's 1000 milliseconds later or not.  If it is, then it increments the internal clock time (doTick()) and hits a procedure to change the position of the indicators on the clock (renderTime()).

The procedure doTick() deals with incrementing seconds into minutes, and minutes into hours, and resetting those values when necessary.

The procedure renderTime() deals with converting the time in H:M:S into positions on the clock face, or more correctly, positions of the stepper.  The program has a concept of the current position of the indicators (preset on startup at 00:00 - the far left position), and uses this initial start position to decide whether to move the stepper forwards or backwards.

Using the internal time keeping of the Arduino is fairly dodgy ground (see this instructable for a good run down on why, and a possible solution).  There's lots of ways round this if it's important: The main Arduino pages have a few ways to make it accurate, as well as stuff about using the Time library.  I went for a more simple solution, given that it's a prototype (yes, that is my excuse for every time I make a hack).

I am using the AFMotor library for driving the steppers, because I am using the Adafruit motorshield.  If you run the steppers through another circuit, you can probably use the regular Arduino stepper libraries with minimal code chopping.  If you download the code, the file should be saved with a .pde on the end.  Not sure how to change the filename on instructables!
Remove these ads by Signing Up