Thursday 19 July 2018

Child's play

“We don’t stop playing because we grow old; we grow old because we stop playing.”
George Bernard Shaw

A small child by the name of Joseph is fascinated by switches and spends a fair proportion of time at church going around and flicking on and off the power outlets. Looking for a diversion ourselves, we hatch the following scheme: a box with five switches (one is on/off) and four lights, each time one of four switches is pressed, the lights change in some random fashion.

Meanwhile the debate about micro-controllers vs logic gate chips rages, with the old guard making amazing projects with combinations of logic chips and 555 timers, whilst the modern bunch write a few lines of code on an Arduino Nano and slam out a project in quick smart time.

So starting with a micro-controller (the amazingly cheap and competent Attiny13a) we have the following circuit up and running.

Four bright LEDs and a button (red wire)
The code was written and tested, including an interesting approach to the random number seed; taking a wound coil and attaching it to the A0 input pin, then polling three input pins and adding them together for a seed (myrand = analogRead(A0)+analogRead(A1)+analogRead(A2)) - not perfect but enough to create the appearance of randomness.

The problem remains, and it's a doozy - how do you then add three more switches when we have effectively run out of pins? Well as it happens a bunch of logic gate chips arrived last week, and among the offerings was a CD4077B - Quad Exclusive-NOR Gate, with the following truth table:


Regardless of how useful this gate might be in other situations, what was evident is that every time a switch is changed (X,Y), the output changes (Z). So theoretically two buttons could link up via a 2-input gate and have a single output of either High or Low (1 or 0). With two such couples of buttons linking to a third gate, four buttons should trigger the interrupt in the micro-controller from a single input - nice!

Which is what this looks like firstly as a diagram.


And what it looks like wired into the (almost) finished toy.


The buttons themselves are either "High" or "Low" and the diagram for their connection is shows how to achieve this.


Before sealing it and subjecting it to the ravishes of a toddler, here is the final(ish) product - some cleanup required on the copious amounts of hot glue!


Happy button pressing ya' little tyke.




1 comment: