In the UK, plus a few other countries here and there, no Christmas table is complete without the traditional Christmas cracker next to every plate.
The Christmas cracker is a cardboard tube, tied shut at both ends. When two people pull it apart, an inner ‘snapper’ gives off a bang while the cracker’s contents fall into your mashed potatoes and gravy. There’s usually a paper hat that tears the moment you try to fit it on your head (or falls away with the after-dinner meat sweats), a gift that tends to be something like a magic trick, a miniature sewing kit or a golf tee, along with a joke, like this one:
What’s orange and sounds like a parrot?
Cracker jokes are notorious for being awful.
Because of this, I have created the Crackerjoke-a-tron. It’s the ultimate joke response unit that allows you to pronounce judgement upon the jokes at this year’s Christmas table.
To make your own Crackerjoke-a-tron, you’ll need:
- a Raspberry Pi (any model will work)
- 2 x tactile push buttons
- a speaker with a standard 3.5 mm jack
If you don’t fancy soldering, you’ll also need:
- a breadboard
- 2 x male-to-male jumper leads
- 5 x female-to-male jumper leads
To add lights, you’ll need:
- a red LED
- a green LED
- 2 x 330 ohm resistors
You can download the .wav files you will need directly to your Pi.
Create a new folder on your Pi called ‘crackerjoke’ by entering the following into a terminal window:
You can then enter this folder using this command:
To download the .wav files to your Pi, use this:
wget http://rpf.io/goodjoke -O goodjoke.wav
And then this:
wget http://rpf.io/badjoke -O badjoke.wav
To make sure the files play, try typing the following (make sure to plug in your speaker or some headphones):
If this works, you’re ready to get your code written and your buttons and lights set up.
First, we’ll put the components in place. Here’s a picture of what to expect:
The GPIO pins we are using are as follows:
- Good joke button = pin 21
- Bad joke button = pin 24
- Red LED = pin 8
- Green LED = pin 7
If you have a breadboard, ground everything as standard. If you don’t, make sure you ground all your LEDs and GPIO pins.
Now it’s time for the code. Open Python 3, create a new file within the crackerjoke folder called ‘crackerjoke.py’ and type the following:
import pygame.mixer from pygame.mixer import Sound from gpiozero import Button, LED from signal import pause pygame.mixer.init() good = Sound("/home/pi/crackerjoke/goodjoke.wav") bad = Sound("/home/pi/crackerjoke/badjoke.wav") good_button = Button(21) bad_button = Button(24) red = LED(8, initial_value=True) green = LED(7, initial_value=True) good_button.when_pressed = good.play bad_button.when_pressed = bad.play pause()
Save your code, press F5, and you should be good to go.
If you’d like the code to run on reboot, allowing you to detach yourself from the monitor, keyboard, and mouse, open a terminal window and type:
At the bottom of the file, add:
Save and reboot.
If you make the Crackerjoke-a-tron, don’t forget to share a picture or a video with us via social media, using the hashtag #BerryXmas.