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, miniature sewing kit, or golf tee, and a joke, like this one:
What’s orange and sounds like a parrot?
A carrot.
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.
Crackerjoke-a-tron
Pronounce judgement upon the jokes at this year’s Christmas table. Full information, including build ingredients and code, can be found at the Raspberry Pi blog:
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 2.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:
mkdir crackerjoke
You can then enter this folder using this command:
cd crackerjoke
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):
aplay goodjoke.wav
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 from time import sleep pygame.mixer.init() good = Sound("/home/pi/crackerjoke/goodjoke.wav") bad = Sound("/home/pi/crackerjoke/badjoke.wav") goodbutton = Button(21) badbutton = Button(24) red = LED(8) green = LED(7) while True: red.on() green.on() goodbutton.when_pressed = good.play badbutton.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:
nano ~/.config/lxsession/LXDE-pi/autostart
At the bottom of the file, add:
@python /home/pi/crackerjoke/crackerjoke.py
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.
The post The Crackerjoke-a-tron appeared first on Raspberry Pi.
Source: Raspberry Pi – The Crackerjoke-a-tron