Here is what it looks like:
This is an extremely long rant about something extremely simple.
I recently stumbled upon a fairly common joke sign that reads "This is a
Velociraptor free workplace... it has proudly been [blank] days since the
last velociraptor incident. This particular sign had a post-it tacked on that
had the result of some crude calculation e.g. 71 million years x 365 days/year.
Excusing the fact that there are leap years and that it is highly unlikely that
it has been EXACTLY 71 million years since the last Velociraptor incident,
the post-it is static and unchanging. It will read the same number of days every
day unless someone manually changes it (not going to happen).
As I happened to be somewhat bored, I thought about building a sort of clock
that increments by a day for every day--such clocks are common, but this one
would have 11 digits (enough to support the order of magnitude of the number
of days). I figured the perfect platform to build such a device on would
be the Arduino, connected to some kind of binary-coded decimal to
seven-segment decoder. I knew what I needed existed (the seven-segment
decoder was a mini-project in one of my classes). However, given the number of inputs
on the Arduino and basic economics, it wouldn't make sense to use a
separate decoder for every digit.
Instead, I used one of the greatest tricks in the history of human-interface
devices. Multiplexing. Instead of drawing every number at the same time,
I just draw them one at a time. Really, really fast. Fast enough that
it looks like I'm drawing all of them at the same time. All I need to implement
the multiplexing with the Arduino is to control when a given digit is lit
so that the correct one is drawn--this is easily done with a transistor
acting as a switch. In fact, even though a given digit is comprised of
seven distinct LEDs, they share a common path to ground, so an entire
digit can be switched on and off with a single transistor.
At this point, there just needs to be a little housekeeping and part-sourcing
to build the project. I used the following:
4511 Seven-segment BCD-decoder--this just takes in a binary number from in the
range 0000 to 1001 and spits out seven voltages that will drive the seven-segment
display. I was originally using the 4543BE (which has very similar specs), but
it kept malfunctioning (digits weren't showing up correctly) so I switched to
the 4511. Knightbright (RED) Seven-segment displays (number depends on how many
digits you want. As the number of digits lit up per display is not constant, there needs
to be a current limiting resistor for each segment of each digit--in this case
I was driving everything with 5V and a voltage drop of 2V meant I needed
7*(number of digits) worth of 150 ohm resistors to limit the current to 20mA.
For the switching transistor I picked the 2N3904, which can handle the current
requirements easily (max of 7*20mA). I also used a resistor to limit the base
current on the 2N3904 to 1/10th of what I needed from collector to emitter
(in this case 300ohms). Finally there's a 10K reistor for each of the digital
logic inputs so that I can control what the logic values settle on.
Oh yeah. I also used a breadboard pre-cut jumper wire (I'm not a big fan of
stripping a lot of wire by hand).
This is where it stopped becoming a clock and became a ticker. While prototyping
the circuit, I realized that each of the seven-segment digits I bought had
an eighth LED--a decimal point. I had already added five digits and realized
that 5 significant digits and a decimal point are just right to represent
some $XXX.XX dollar value. That's where bitcoin prices came in.
The software side of the project is so simple that I'm not going to go into
too much detail.
I use a 20-line Python script that parses the last price from mtgox.com and
spits it out as serial data. It refreshed mtgox every 30 seconds and will
retry up to 10 times if the page isn't loading.
The Arduino is constantly drawing the digits and checks if it has received
a new number every two seconds, and if the new number is different, it
increments or decrements the old number until it's equal to the new number
(this gives it a slightly cooler look than just changing the display
in a single instant).
Comments:
Unfortunately I'm not crazy enough to program the microcontroller at the assembly
level or use some fancier display process so basically all of Arduino time
is spent drawing the digits. This means that when it's reading new serial
input there's a half a second when the display blanks.
There's also some floating point rounding
quirks on the Arduino where if you do something
like:
If number A is less than number B
add 0.01 to number A
Else if number A is greater than number B
subtract 0.01 from number A
You end up with some oscillation where A never quite settles on a single
value but jumps between B and B + 0.01. You can solve this by simply
deciding to stop incrementing something when the difference is withing
some number larger than 0.01.
When drawing the digits, there's a brief period (1ms) where everything is switched
off before drawing the next digit, otherwise there is ghosting from the
next digit being drawn bleeding onto the current digit. The 1ms period
is not visible to the human eye.
Getting Arduino to work in Arch Linux is a pain. The IDE doesn't render correctly
if you don't use a full-fledged desktop environment that supports Java's
crap and there's some serial communication configuration that you have to go
through. As a result, I did all of the Arduino stuff in Winblows and later
moved the code over to GNU/Linux when I knew it was working.
Notes:
If you are really dying to see my horrible and ugly code (I have no sense of style, especially Python style), here it is:
https://github.com/eqy/bitticker