Thursday, 18 December 2014

Dice reading device

About a million years ago (and on another, earlier Nerd Club blog) Evil Ben was working on an electronic cube reader. The idea was to have a cube with a number of pre-printed faces on it, which could be placed into a dedicated cube reader and read back not just the face showing (by inferring it from which face was down) but also the orientation in the reader.

Having demonstrated how our electronic board game works to a few people even nerdier and geekier than we are, a few people have suggested that we might like to allow players to roll their dice and enter the results into the game rules.

Originally we dodged this as an idea- it just seemed like a whole world of hurt and extra work. But then we thought "hey, why not?" and so made a few changes to our wifi connector device.

Now, our board game was deliberately designed to use nothing more than a single serial pin for tranferring data - specifically so that we can develop extra add-ons for the hardware without having to redesign it from the ground up. And one add-on that immediately springs to mind is a "dice reading module"; a module which can detect the presence of  dice, read which face is down (and from that, infer which face is pointing upwards) and send this information back to the host via serial.

We've already built this into our first game app - pausing the game at key points and waiting for the user to enter the dice roll result using the enhanced wifi connector. Instead of using the rotary dial to select a dice result, we can simply send the same "data message" back to the host, over serial, but from a different device plugged into one of the game board pieces.

So how do we read the value of a dice face?

These clever little QRE1113 things from Farnell are just the ticket - they are reflective sensors and are often used by robotics hobbyists for black/white line following. It's simply an IR LED and an IR phototransistor in a single package. When the sensor "sees" IR light, it activates the internal transistor. We can use this, connecting an input pin (pulled high using internal pull-up resistors) to the phototransistor collector, with the emitter connected to ground:


The simple idea is that placing an array of these sensors on a board, and placing a dice over them will result in some sensors activating (where no spot is found immediately above the sensor) and some not (where a black dot appears above the sensor, the IR will not be reflected back).

Thinking about how the spots on a dice are laid out, and allowing for the dice to  be "read" in any orientation, the first thing that springs to mind is a grid of 3x3 sensors. However, with a bit of thought, it turns out that we need only 5 sensors to be able to read every possible number on a dice face:


The image above shows how the spots on a dice could possibly be presented to a 3x3 grid of sensors. But if we use only the sensors as show on the right, we can still identify each possible number uniquely (and still allow for the dice to be rotated either horizontally or vertically).

For example, if the number two were face down, either the bottom-right or top-right sensor would be "inactive" and all other sensors would see reflected IR light from the (white/light-coloured) face of the dice. If the number three were face down, either of these sensors would be inactive, along with the  sensor in the "centre" of the 3x3 grid.

The patterns of active/inactive sensors are unique for each number on the die, as shown above. Now we're just waiting for our sensors to arrive from Farnell and we can put the theory into practice!