Friday 22 August 2014

Home Schooling ICT, part 8: The timey wimey circuits are working!

TARDIS build is coming along, but am finding the learning curve for this coding stuff is actually quite hard. blog.mindrocketnow.com

Our summer holidays project to make a cardboard Tardis paused whilst other summer activities took precedence. Suddenly, we’ve got two weeks of holiday to go, and we’ve not progressed much further. Quick – start building!

The bulk of the construction was already done, just the decorating and the telephone to install. We abandoned the collage idea, and went with the quicker writing out by hand. The box is going to house the emergency telephone, and now needs to be attached to the TARDIS with a duct tape hinge.



The writing might be a little wonky 

The next part of the build is going to be a little tricky. We only have a small blue LED, so to make it visible we can either put behind a lens in a lantern. Or we can cover the lantern with blue acetate and use a bunch of white LEDs. I think we should try the former, because we have the parts already, and it might actually be brighter than the latter.


Having seen the limited appetite of the DDs to learn coding by googling for answers, I decided to write the next version of the TARDIS program myself, to present to them afterwards. However, I underestimated my own recollection of coding from university days. I now realise that not only do I not know the python programming language, but I’ve also forgotten how to read and understand API documentation.

My late night surfing for code examples for newbies showed me that the barrier to learning is quite significant; there’s an expectation that you know the basics before questions are answered. I know the DDs won’t have the patience to work it out with me, so I’m going to have to go through it so that I can explain back to them.

Case in point was playing the sound file of the TARDIS de/rematerialising. I’d already installed VLC, which has a nice Xwindows interface. The DDs were able to play the sound just by right clicking on the file. But how to invoke VLC in a python script? Even though there are some well-documented python bindings for VLC, I couldn’t understand them. So I was unsurprised when my simple code didn’t work. (To understand the following code snippet: pin 23 is connected to the switch, and pin 24 is connected to the blue LED. The idea is, when you push the switch, the LED flashes in time with the sound.)

import RPi as GPIO
GPIO.setmode(GPIO.BCM)
import time, vlc

GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.OUT)
player = vlc.MediaPlayer(‘Tardis.mp3’)

while True:
if GPIO.input(23):
GPIO.output(24, True)
player.play()
time.sleep(1)
GPIO.output(24, False)
time.sleep(1)
else:
GPIO.output(24, False)
time.sleep(0.1)

I couldn’t get the sound to repeat. Whilst I kept my finger on the button, the LEDs flashed, but the sound played only once. I also couldn’t figure out why I needed that first time.sleep(1) command (in the if section); surely the LED would only stay lit for as long as the file was being played, thereby being synchronous?

Searching for why it wasn’t working just sent me to the VLC python bindings documentation, which seems to me both comprehensive and unintelligible. I think I need a better understanding of how API documentation is written in general before I can understand the VLC one. So I gave up, and tried to find another way to achieve the same end.

Whilst searching for how to make a doorbell using a RPi (press button – make sound), I came across this great article from Adafruit (incidentally, a source of a number of interesting project ideas). The answer, it seemed, was to use a different player, mpg321, and to invoke from the operating system command line, not via a python binding. The code then became:

import RPi as GPIO
GPIO.setmode(GPIO.BCM)
import time, os

GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.OUT)

while True:
if GPIO.input(23):
GPIO.output(24, True)
os.system(‘mpg321 Tardis.mp3’)
GPIO.output(24, False)
time.sleep(1)
else:
GPIO.output(24, False)
time.sleep(0.1)

And it worked! So DDs and I amended their code from last time with my newly-worked out code, and we made the LED flash in time with the dematerialisation sound. 


Can you see the LED (next to DD2’s hand) lit?

TARDIS Project Plan - Update
a) Planning
* Draw picture of what we want to build (done)
* Draw cause-effect diagram of how we want the program to work (done)
* Create a shopping list for the stuff (done)

b) The TARDIS box
* Paint the box blue (done)
* Install in doorway (done)
* Paint the “Police Public Call Box” sign (done)
* Decorate and attach box for telephone handset (half done)
* Find a light lantern, and attach lens, for the blue LED
* Make mount for TARDIS ignition key switch

c) Programming the Raspberry Pi
* Learn how to make an LED flash (done)
* Learn how to play a sound file (done)
* Learn how to play a sound file through an external speaker
* Write a program that flashes an LED and makes a whooshing sound, in sync (done)

d) Connecting the Pi to the TARDIS
* Buy the components and check that they connect together (a bit done)
* Build any bespoke connectors
* Prototype the light and sound rig on breadboard (done)
* Build final rig, and test that it all works

More things learnt
* Painting and decorating is more interesting to electronics and coding, at least to my DDs. Therefore do more painting, and make the coding less demanding.
* Before attempting to teach coding, work through the code yourself first. Things that you assume will work first time, won’t.
* Sometimes knowledge is hard-fought yet won. Other times, it remains elusive. Teaching is to make it worth the challenge.


More in this series: part 7.

No comments:

Post a Comment

It's always great to hear what you think. Please leave a comment, and start a conversation!