Posts from: December '12
Chinese precision FTW!
More importantly, I only ordered one of these (the one on the right), but the chinese figured out they can just ship me a second one, just in case. And they didn't charge me for the second one. Talk about precision!
Posted in category Images -- 14 Dec 2012, 13:34, 5 comments

Hardware random generator
I'm getting better at electronics :) When I decide to build something, it now takes me around six hours to complete it. This weekend was in a I'm-somewhat-sick-n-bored mood, so I decided to build a random generator.
And not just a random generator, but an arcade monster! With ridiculously big indicators and buttons! Blinking brightly and stuff...
It would be utilized in board games like The Settlers Of Catan, where you need to throw a pair of dice and sum their roll. In fact, the generator just "rolls two dice", and a jumper determines what to do with the outcomes.
Hardware random generator
Computers usually employ algorithms like LCPRNG (or more sophisticated), with the side effect that whenever a "supposedly random" lottery draw is done on a computer, you'd have a very hard time convincing the average joe that the drawing isn't manipulated :) Luckily, having a more close-to-the-metal access to the hardware allows you to code something like this:
while not keypressed() { counter++; } return (counter MOD 36);
What do we have here? A counter is incremented extremely fast (thousands and millions times per second). When the button is depressed, the counter immediately freezes at its last value, which is used to determine how did "the dice roll".
Now, imagine highway driving. And in additional to the usual per-kilometre signs, the asphalt is ruled finely, into metres and centimetres. Each centimetre tick has a number associated, and the numbers increase cyclically: 0, 1, 2, 3, ..., 34, 35, 0, 1, 2, ... While driving, you throw a steel ball out of the car. The ball lands somewhere and you take the number, associated with the closest centimetre tick. This is roughly what the code above does - the outcome is essentially random. Then one has to apply some 1:1 mapping between the 36 possible pair-of-six-sided-dice rolls and the numbers, and we're ready.
Design
Here I'm testing some possible layout configurations. I got the huge button from SparkFun.
Development
The mandatory breadboard design phase:
Here I put a miniature counterpart of the big red button:
Board
I swore that I'd never use a perfboard again, right? But this project would've been an overkill to do with a PCB, so I used something simpler: a stripboard (a.k.a. Veroboard). This is a prototyping board, which has parallel copper-clad strips. The elements are usually placed perpendicular to the strips, which leads to a characteristic order in the design - all paths are straight (in coherence with the inner peace of someone who grew up in Manhattan or Stara Zagora!).
My first job was to drill four holes with my new toy:
In retrospection, I wonder how I'd lived without a Dremel :)
Soldered some sockets:
The strips in these these boards are very long, but are easily teared with a simple tool, so one horizontal may be used to connect several different groups of components. Here: we have to isolate the two sides of the MCU pins, and same for the indicators:
On the plus side for stripboards are: less soldering, very few custom connections, and error tolerance. If you mess up something, it can usually be fixed without desoldering. Run the tool to isolate the wrong part and reroute it somewhere else. On the other side: be careful with soldering, it's easy to introduce tiny "whiskers" of solder, that inadvertently connect neighbouring strips. I had two errors of this kind and debugged them for quite some time...
Filling up with resistors:
...and elements:
Please, install software:
Visibly progresses to near-finished:
I decided to put the 7-segment indicators on sockets - they could be soldered directly, too, but that would make the huge space below them unusable.
Finishing touches
Test blink on the segments...:
... and some test digits...:
I imagined the finished thing to be like this:
However, I didn't like the power switch. It's big, and accessing it conveniently would've required drilling a big hole, and would need some redesign due to space constraints. However, I had these cute toggle switches...
So I put into action the other new toy:
Seemed like the drill bit was rotating lazily, but it drilled into the plastic as if it was butter...
Nice:
Ready
The timer is at 6 hours and a half after the first photo. Here's how it looks like with the lid closed:
Please, press the button:
Here, you're lucky:
And an useless sign:
something like the electrical equivalent of the code i++; /* incrementing i by one */
Here's it in action: YouTube
In the end, I wanted to make a random generator, but it turned like a lot like a timer-activated bomb (or probably has the looks that Hollywood formed in my imagination).
In conclusion
Pros:
- A dice roll is much quicker and doesn't take that much space on the table;
- Guaranteed uniform distribution. "Hey, you didn't roll the dice properly..." is history;
- I coded in a statistic-gathering funciton. So one can inspect the histogram after the game is finished (hold the button for more than 6 seconds to go to the printout).
Cons:
- Drains the battery quite fast, I guess it will last for a two or three games;
- I suspect I'd have to tell the highway analogy a lot of times, and I'm still not sure it would convince prejudiced people. You know, of the kind with loathe all modernisation and computrization of things, because it "spoils" everything. It's irrelevant that their whole life depends on computers, and, leave them without facebook for two days and they'd go mental;
- I can never fly with this on a plane :D It's quite possible they'd hold me and question me for a loooong time :D
The C source code is available here.
I'm looking forward to the upcoming holyday season, there would be a lot of Catan, and I'm impatient to test my generator :)
Posted in category Hardware -- 9 Dec 2012, 16:09, 3 comments