My Half Life Clock

A simple project using avr-libc and some cheap peripherals to implement a clock which counts down to a major life event.

Similar projects worth following
In February of 1991, I arrived for my first day at my employer, Pixar Animation Studios. Through the years, a number of people at Pixar have been recognized as having spent half their life at the Studio. In November of 2017, I will join their illustrious ranks. I thought it might be fun to make a digital clock which counts down to that day, using a TM1640 16 digit 7 segment display, an inexpensive DS3231 clock module and an $3 Arduino module. While this use is fairly specialized, you can use the ideas and code in this project to implement a count down clock of your own.

I'm calling the project finished!

Compiling it in its final form resulted in a 1020 byte flash image:

And the code is available under an MIT style license from my github page. I was initially going to put it on a small Arduino micro, but I found a nice black box (part of the packaging for an Amazon Echo) so instead I just used that and kept it on the Metro that I was using for the prototype. I slit the side to accomodate the TM1640 display, secured it with a couple dabs of hot glue, and then piled the rest of the electronics inside. Despite the chaos of junk inside, it actually looks pretty nice, and will sit on my office desk for the better part of the next year. I did a simple video showing the final unit below:

  • Addendum

    Mark VandeWettering01/13/2017 at 04:21 0 comments

    Okay, now that the 1K Code Challenge is over, I made a couple of changes that push it over 1K, by a small bit (about 150 bytes). In addition to displaying the time remaining to my landmark half-life anniversary, it now counts down to several other dates: namely the release date for the next two Pixar films (Cars 3 and Coco) as well as the date of the final "digital dailies" for Cars 3 production (which probably marks my wrap date for the film.) Very cool to have that sitting on my desk.

  • Demonstration of the working code...

    Mark VandeWettering12/25/2016 at 20:52 0 comments

  • Scaling back, and some success!

    Mark VandeWettering12/23/2016 at 07:53 0 comments

    I kept working on this project, and quite frankly struggled a bit. When I first imagined this project, I thought that I would make a sort of modal clock: it would alternate between showing:

    • the name of the event ("half life day", which nicely can be rendered legibly on a 7 segment display)
    • the current time and date
    • the number of days remaining until the event, in days, hours minutes and seconds.

    When the time went down to zero seconds, I wanted to trigger an external event (perhaps to blink some lights, or sound a beeper).

    I did a couple of rounds implementing all the functionality, and ended up with a not-quite-fully-functional prototype that was close to 1500 bytes. I did battle with it in a number of different ways.

    The key components of the design are the TM1640 based 16 digit 7 segment display, and the DS3231 I2C clock module. To make this work, we need drivers for both. Luckily for me, Peter Fleury did a lot of heavy lifting with respect to the I2C bus. I used his i2c library:

    which uses the TWI (Atmel-speak for I2C) hardware on the atmega328. It's compact and easy, and is quite compact. Interfacing with the DS3231 only takes a couple hundred bytes.

    I took some time to read the Titan Micro data sheet for the TM1640. It's pretty simple too: a couple of hours reading and tinkering resulted in the very minimal amount of code that I needed to drive the display.

    Together, combined with some of the library overhead, I ended up with about 630 bytes of code for both. That left me just about 400 bytes (less than 200 instructions) to implement the rest.

    The basic problem that I had was that the clock module presents its data in BCD form. That's fine if all you want to do is display, but I needed to compute the number of days until my target date, as well as the number of hours, minutes and second until that date. The Atmel doesn't support BCD arithmetic, which made all of these calculations difficult. I worked out the algorithms, but attempts to squeeze them was clearly getting me no where.

    It wasn't going to happen. I went to bed frustrated.

    But in the morning, with some patient urging from my wife, I rethought the project as a whole. What could I do that could be coaxed into fitting?

    Basically, my clock became a calendar.

    It would still read the time from the clock module, and compute the number of days until the target date.

    • Before the date, it displays "XXXX daYS to", and then "haLF LiFE dAY"
    • On the day, it will display "todAY is", "haLF LiFE daY"
    • and after, it will display "haLF LiFE daY", "haS PaST"

    On the day of the event, it will also enable an output pin to trigger some kind of remote action. I've got some 10mm blinking red LEDs, but I'm also thinking of wiring a small motor with a flag on it that will wave on the day. I'll probably avoid audio signaling, since ultimately this will sit on my desk at work, and I don't want to overtly disturb anyone.

    Another hour of working, and I tweaked it down to just 387 lines of code, and 986 bytes of firmware. Over the next few days I'll work up a YouTube video to demonstrate it, and try to get it mounted into a permanent case.

    I'm actually kind of pleased with the edited result. I learned a lot about trying to squish down C code for the AVR architecture, and the countdown calendar is actually pretty nice.

    In the meantime, I've placed the code (which I consider reasonably complete at the moment) in my github account. I haven't actually tested the "alarm" functionality, but it's straightforward, so I have some confidence. I'll work on some documentation and hope that someone finds it useful.

    A slight annoyance is that I was trying to find a link to a supplier for the LED displays. I've had a couple of these in my junk box for a while. I think I ordered them from deal, but I failed to find a link to them, so reproducing this might be more difficult than I had imagined. I'll keep on the lookout....

    Read more »

  • 1K is not a lot of instructions

    Mark VandeWettering12/22/2016 at 16:13 0 comments

    Okay, I know that 1K is not a lot of instructions. On the ATmega328, it's less 512 instructions. That's not a lot. But if you are coding in C, with the normal startup routines, you lose 26 double words right off the top for the interrupt table. I want to access an I2C squared device and also the TM1640. Peter Fleury's code to do the I2C part is actually pretty small, and the TM1640 isn't too bad, but by the time you have both of those in place, you likely have used somewhere north of 600 of your 1024 bytes. That means you have about 200 instructions left.

    In that, you need to be able to implement all the rest of your logic. Bust apart the BCD numbers from the clock chip, compute days and time remaining, back to BCD and then out to the display.

    I can do it all, but my almost-entirely feature complete version of the code runs about 1492 bytes at the moment.

    I'm wavering in my conviction that it can be done, even in carefully handcrafted assembly code. I'm going to consult with one of my guru friends, and see what he thinks.

View all 4 project logs

Enjoy this project?



greenaum wrote 12/19/2016 at 20:02 point

When the day comes? It has to come to life and climb out the window. Because Pixar!

  Are you sure? yes | no

Thomas wrote 12/19/2016 at 18:46 point

I like your attitude!

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates