Saturday, 6 October 2012

Welcome to Take Control

Welcome to Take Control, a website where I describe my adventures in programming and controlling devices with computers.  In spite of having a long term relationship with computers, I am fairly new to most of the stuff I'll be describing here and so I'd be happy to have you alongside me on my 'learning journey' through electronics and computing.  Please feel free to leave comments and questions as you see fit.

What I hope to do is create a series of simple projects, easy enough for a novice like me to make and implement using a number of low cost or free resources.  Many of them will be based on stuff I've seen on the web but one or two will be my own ideas.

My main computer of choice at the moment is the Raspberry Pi, although I will also be looking at the Shrimp (an Arduino-compatible microcontroller system) later on.  Checkout the tab at the top of the page for my initial experiences with the Raspberry Pi, which I feel embodies the spirit of the BBC Micro from the early 1980's and 90's as well as my first go making a Shrimp.

Well that's my first post over with, I hope you will enjoy some of the stuff I put here and come back frequently to see what's new.
Martyn Jones
(aka GeekTeacher)

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

Halloween Lantern

This project is my first using the Shrimp, a low-cost Arduino compatible microcontroller board created by Cefn Hoile at Shrimping.it.

It's the spooky season with Halloween approaching fast. Now I don't really go in for all the American 'Trick or Treat' stuff - in fact I find it totally disgusting that shops now sell terror masks, blood-laden hatchets and other such creepy stuff that parents buy their pre-teenage kids. No wonder our society is in the mess it is with murder and rape talked about openly every day on TV as if it were just a fact of life. Of course, I blame the media (films, TV and computer games) for all this, but don't start me off on that one.

I hate it so much, that now my kids are grown up and go out to fancy dress parties on Halloween, I just lock the door and pretend that I'm out. However, as a kid I did enjoy making a good Halloween lantern out of a suede (we never saw pumpkins where I lived) and a candle. Of course, the idea of the lantern is to ward off evil spirits and so I'm totally OK with that.

So now it's time to bring the Halloween lantern into the modern world and what better than to use a battery driven Arduino-type system to produce the flickering lights that won't blow out in the wind.  Even better if it springs in to life when approached by an evil spirit, i.e. a kid.

To make this project the following items are needed:
  • A Shrimp with a battery pack
  • The Arduino IDE on your computer
  • A Halloween lantern sweet bowl bought from a 'pound shop'
  • Seven coloured LEDS
  • Seven 220 ohm resistors
  • A CP2120 PIR sensor
  • Some wire, solder and a soldering iron