Friday 26 April 2013

Blue Raspberry Pi

There was much excitement in the Geek household yesterday when my limited edition Blue Raspberry Pi arrived.


The Raspberry Pi Foundation produced 1000 of these to celebrate the first year of production of the Raspberry Pi.

RS ran a competition on Twitter, the winning prize being a Blue Pi.  To enter the competition, all you had to do was Tweet @RSElectronics with the hashtag #bluepi and a description of what you'd do with a blue Pi.  Here was my entry:

So easy to enter, I didn't even have to enclose a stamped, addressed envelope*.  I only went and won!  I was chuffed to bits.

The observant Geek dad blog fan will observe that I've already made a Scratch controlled Raspberry Pi disco.  The reality is that, following the mantra that "a pessimist is never disappointed",  I didn't think I'd be lucky enough to win a blue Pi. Hence I want out and bought another Pi to use, (my first Pi being busy running my environment monitor). But I did!!

* Showing my age.  

Sunday 7 April 2013

Raspberry Pi Scratch GPIO Disco Lights Game

Eben Uptons's Pycon keynote speech reminded me that the Raspberry Pi project was all about getting kids in to coding.  To date, I'd got the kids interested in the Pi through things like the Pi controlled Lego car and they love Minecraft, but I couldn't say that we'd done some proper coding.

The answer was to do more with Scratch and we tinkered around with it and (my six year old in particular - she calls Scratch "Kitty-Cat") enjoyed this.  We got the sprite to move around, react to keyboard presses and say stuff.  Interesting to them but not super-exciting.

I decided we'd need to do some stuff with the GPIO to make it more interesting.  This brilliant blog posting from SimpleSi explains how to do it.  Full marks to him for doing this, it's excellent.

My first try was to build a Lego disco; it's shown on the picture below.  On the picture you can see:
-The disco lights (bottom middle)
-The motors for the glitter balls
-The funky dance floor



...a view of the wiring and soldering, (so it's simply some jumper wires from the GPIO onto a bread-board, through some resistors and then to the LEDs:


Using Scratch GPIO control we could:
-Set up different light sequences.
-Turn the glitter ball motors on and off.
-Compose some music.

Here's an example of a light sequence and music, (not sure how to export Scratch code other than this!!):


...and two light sequences plus a method to piece multiple sequences together.  The "when 3 key pressed" block is almost like a main procedure that calls two sub-routines.  I like it!


The kids liked this but didn't find it super-exciting.  There's only so many ways you piece together light sequences and making the music is a little laborious.  For some reason, making the LEDs flash left to right then right to left like KITT / a Cylon didn't excite them!

So the next plan was to create a game where the LEDs flashed in a sequence and you win by pressing the space bar when the central LED is lit.  Here's the code, segment by segment.  First, pressing G gets you in to the loop shown below:


So this sets a LoopVar with the value 1 then goes in to a loop, sending out a set of broadcast messages and testing LoopVar to see if the loop and the broadcast messages should continue.

The code segments that receive the broadcast messages are shown below:


So the code receives the broadcasts, sets a variable that can be checked to see if the LED is lit, lights the LED, pauses then turns the LED off.

The final code segment handles detecting when the space bar is pressed:


So when the space bar is pressed:
-LoopVar is set to 0 to come out of the "G" loop, (the "if LoopVar = 1" checks in the "G" loop means we don't light the LEDs for the remainder of the loop)
-We check which LED is lit.
-If the centre LED is lit (GPIO output 13) then you get a you've "won message", the LED flashes and a happy sound is played.
-If other LEDs are lit then you get a "you've lost" message and the Kitty Cat meows.

The kids enjoyed playing the game and tinkering with the code.  We changed timer variables, changed the words that Kitty Cat says, changed the sounds made when winning and losing and changed loop variables, (i.e. play sounds more/less when you win).

Subsequent modifications we made:
-Allow the time the light is on to be modified by changing a single variable, (rather than modifying 5 code blocks).
-Allow the time the light is lit to be gradually decreased, making the game harder.
-Added a "Points" variables that increases as the lights flash faster, (i.e. more points if you win when the lights are flashing fast).

My eldest said it was "Epic and fun at the same time" and my youngest said it was "Awesome".  That will do for me!

(They subsequently said "can we play Minecraft" so there's still some work to do...)