Inspired by Les Pounder's
hacking, I spent a quid on some LED pumpkin lights and thought I'd make something fun for the trick-or-treaters this year. I wanted the lights to come on and a spooky sound effect to play when a button is pressed on my little box of horrors.
As Les suggested I removed the battery box from the LEDs, and then extended the wires with some hook up wire. I also found and edited some nice spooky sfx, which I saved onto the Zero.
The Raspberry Pi Zero has no audio output, so I added a
pHAT DAC from Pimoroni; remembering to use extended headers so I could later add a
ProtoZero board to tidily solder the wires onto.
For the first prototype I connected the LEDs to a GPIO pin and the ground pin and started by writing some code that just made them come on and then go off again after a few seconds. Next I added a button which switched the lights on. Finally I used Pygame mixer to play the audio file at the same time as the lights come on.
The code looks like this:
| # Import Python libraries |
| import RPi.GPIO as GPIO |
| import time |
| import pygame |
|
|
| # Set the GPIO caming convention |
| GPIO.setmode(GPIO.BCM) |
| GPIO.setwarnings(False) |
|
|
| # Set the GPIO pins for button input and LED output |
| GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) |
| GPIO.setup(24, GPIO.OUT) |
|
|
| pygame.mixer.init() |
| pygame.mixer.load("Laugh.wav") |
|
|
| while True: |
| if(GPIO.input(3) ==0): |
| GPIO.output(24, GPIO.HIGH) |
| pygame.mixer.music.play() |
| time.sleep (10) |
| GPIO.output(24, GPIO.LOW) |
| |
| GPIO.cleanup |
I went back to my favourite Instructable on
launching Python scripts at startup and then set about cramming it all - including USB powered speakers and a USB power bank - into a Celebrations box that I'd kept because it looked useful. I drilled a small hole in the side to pass the LED's wires through, and a big hole in the lid for an arcade button. It's a bit of a squeeze to get to the lid on, but it all just about fits in.
There are plenty of things I could do to make this better. For instance...
- At the moment it plays the same sound every time the button is pressed, but it'd be nice to play a random selection from a playlist of sounds
- The lights could flicker and flash while the sound plays instead of being constantly on
- If the sounds are of varying lengths the lights should only be on for as long as each sound plays
- The lights and sounds could be swapped for Christmas or other gaudily celebrated occasions
- Spray paint and decorate the box to be a bit less chocolate-boxy
- Re-write it in gpiozero