Saturday, 6 October 2012

Electronic Die

!!! Warning, Warning - Still Under construction .... unfinished !!!!

With Christmas coming it will soon be time to roll out the party games, most of which will need a die of some sort.  If you're wondering about the word 'die', it is in fact the correct word for a single item - as my father always told me, 'dice' is the plural. Why not make this simple electronic project with your Raspberry Pi to give your games a little more excitement?  You'll learn a little about binary numbers in the process. Plus there'll be no more cheating and no hunting for lost dice under the sofa on Christmas Day.

What you need:

Essential:
  • Raspberry Pi with Python and GPIO or PiFace libraries installed
  • Seven red LEDs (usually 2.2v)
  • Push to make non-latching button
  • Red bell wire or similar and an old network cable
  • Solder and soldering iron - (although you might be able to just twist and tape)
  • Seven 150 ohm resistors (unless you are using 5 volt LEDs)
  • Cardboard square to mount the LEDs and switch
  • Small plastic, wooden or even cardboard box for finished project.
Either:
  • PiFace or another suitable 'buffer-board' type interface
          or
  • Breadboard with eight male to female jumper leads

A Little Binary Theory:

We need to be able to control each of the LEDs individually to be able to display a 'matrix' version of each number in the traditional die dot pattern in like so:


So, to be able to create each of these patterns with lights, a matrix of seven LEDs is needed like this:


Each led will be attached to an output port numbered as follows:


So using the die number patterns shown in the first graphic, the LEDs need to be turned on like this to display each number:

Die Number
LEDS
1
LED 4
2
LEDs 3 and 5
3
LEDs 3, 4 and 5
4
LEDs 1, 2, 6 and 7
5
LEDs 1, 2, 4, 6 and 7
6
LEDs 1, 2, 3, 5, 6 and 7

Each LEDs is controlled by writing a 1 to turn it on and a 0 to turn it off to each of the output lines. Thus by writing the decimal equivalent of a binary number to the port, the LEDs will be turned on or off as required.

A little bit of binary is needed now to generate the number to write to the output port which will turn on the individual LEDs.

LEDs > 1 2 3 4 5 6 7
1 =
000100 0
2 =
001010 0
3 =
001110 0
4 =
110001 1
5 =
1101011
6 =
111011 1
Binary >1 2 4 8 16 32 64

The table below shows how the required binary number is converted into a decimal number need to be written to the output port in order to switch on the required LEDs.

Die Number
Binary
Calculation Decimal
1
0001000  = 8
8
2
0010100  = 4 + 16
20
3
0011100  = 4 + 8 + 16
28
4
1100011  = 1 + 2 + 32 + 64
99
5
1101011  = 1 + 2 + 8 + 32 + 64
107
6
1110111  = 1 + 2 + 4 + 16 + 32 + 64 
119


Construction:

Building the electronic die is quite suitable for beginners. Although soldering is the best way of making connections, it may be possible to just twist and tape the wires together, provided a good tight connection is made.

Here's the circuit diagram schematic:


Please note that this is a schematic diagram and your physical wiring may look slightly different - mine certainly did! Look at this:

INSERT PHYSICAL WIRING PHOTO HERE

A good tip here is to use an old network cable (you know, the ones with broken plugs that won't stay in) which has eight individual wires in a plastic sheath - this saves messing about with lots of separate wires and helps keep the cabling organised.  Just strip off about 5cm of the sheath for the RPi end and about 12cm for the LED end.  Then bare about 5-10mm or each wire and 'tin' it with solder if possible. 'Tinning' means melting a thin layer of solder on to the wire which makes joining it to another 'tinned' wire much easier.  See here for more detail about soldering and tinning.

Next, mount each of your seven LEDs on a piece of cardboard in the matrix pattern as shown above.  I did this by simply making a small hole and then pushing each LED through in turn.  Be careful not to make the hole too big or the LED will keep falling out.  Doing this helps get the wire lengths sorted as well as giving you a platform for soldering.

Tin each of the LED legs before soldering a 150 ohm resistor to the cathode leg of each LED.  The orientation of the resistor doesn't matter, and the cathode leg is either slightly longer, kinked or on the 'flat' side of the LED lens plastic.  Then solder a 10cm piece of red wire to the other end of each resistor.  Finally take each of these wires, tin them, twist all the ends together and solder them as a block.

Now cut the plugs off the old network cable, strip the cable sheath off each end to expose the eight wires and solder one of the wires to the anode leg of each LED.  This will use seven wires -  the last remaining wire can be used for supplying the power to the LED - solder it to the block of red wires you made earlier.

It is useful to make a note of which colour goes to each LED number at this point as well as which colour wire is supplying the 5 volts. If using a PiFace interface, connect the 5 volt wire to the leftmost port first of all, and then connect each LED wire in turn following the numbering on the schematic diagram.  If using GPIO you can connect to the RPi via a breadboard with jumper leaders to make the connection without soldering.  This diagram show how this is done:

INSERT BREADBOARD DIAGRAM HERE

Testing:

To make sure that the connections are good, test the LEDs in Python as follows:

PiFace:

INSERT PIFACE PYTHON CODE HERE

GPIO:

INSERT GPIO PYTHON CODE HERE

Software:

A random number between 1 and 6 is generated and this code will determine which bit-pattern needs to be output.  Controlling a PiFace interface in Python, this is done as shown below:

if random.randint(1,6) = 1 pfio.write_output = 8
if random.randint(1,6) = 2 pfio.write_output = 20
if random.randint(1,6) = 3 pfio.write_output = 28
if random.randint(1,6) = 4 pfio.write_output = 99
if random.randint(1,6) = 5 pfio.write_output = 107
if random.randint(1,6) = 6 pfio.write_output = 119

When the <Enter> key is pressed, a set of random numbers are generated for 30 seconds, the last of which is the number rolled.

Program 1:


import piface.pfio as pfio
import random, os
from time import sleep

pfio.init()
random.seed(None)

while True:
        raw_input("Press Enter to roll the die...")
        os.system('clear')

        for i in range (1,30):
                rnum = random.randint(1,6);
                if rnum == 1:
                        rnum = 8
                elif rnum == 2:
                        rnum = 20
                elif rnum == 3:
                        rnum = 28
                elif rnum == 4:
                        rnum = 99
                elif rnum == 5:
                        rnum = 107
                elif rnum == 6:
                        rnum = 119
                pfio.write_output(rnum);
                sleep(0.1)

        n = random.randint(1,6)

        def one():
  pfio.write_output(8);
                return
        def two():
                pfio.write_output(20);
                return
        def three():
                pfio.write_output(28);
                return
        def four():
                pfio.write_output(99);
                return
        def five():
                pfio.write_output(107);
                return
        def six():
                pfio.write_output(119);
                return

        options = {
                "1" : one,
                "2" : two,
                "3" : three,
                "4" : four,
                "5" : five,
                "6" : six,}
       
print "XXXXXXXXX"

        num = str(n)
        print num

        print "XXXXXXXXX"

        options[num]()




INSERT YOUTUBE VIDEO OF THE ELECTRONIC DIE IN ACTION HERE


*********************************************************************************

Possible extensions:

Microphone / sound sensor (to detect a clap)
Speaker (to hear the numbers roll)
Sound 'tick' for every number
Advanced - 'rolling' numbers speeds up and then slows down