Program your whoopee cushion
![]() |
Dibujo extraído de raspberrypi.org |
- Open Python 3 (IDLE) from the Programming menu and click on File and New Window. This will open a blank file.
- Click on File and Save As and name the file
whoopee.py
-
Type the following code into your file:
import os import random from time import sleep from gpiozero import Button
This part of the code pulls in all the libraries that you are going to use to write your program.
-
Then, you'll need to use the Button class in your code. You'll have
to tell it that the button is on pin 2. To do this, write the following
code in your new file:
button = Button(2)
-
Now create a list of all your sound effects and store then inside a variable that you can call later on in your code:
trumps = ['ben-fart.wav', 'ca-fart.wav', 'marc-fart.wav']
In Python, square brackets are used to create a list. Each item in the list is separated by a comma.
- Once all the setup needed in the code is complete, you can move on
to writing the part of the program that will make something happen when
the button is pressed. Begin by creating a loop using
while True:
. - Then, add
button.wait_for_press()
inside the loop by indenting by four spaces. Each time around the loop, the computer waits for the button to be pressed. - On the next line, use the
random.choice
function to select a sound at random from the list you created earlier. That selected sound needs to be stored inside another variable which you can call parp! Typeparp = random.choice(trumps)
. - The next line will play the sound selected at random using
aplay
, which you used earlier to test your sounds. Typeos.system("aplay {0}".format(parp))
. - Finally, add
sleep(2)
to pause the program before it starts the loop again. -
Your code should look like this:
while True: button.wait_for_press() parp = random.choice(trumps) os.system("aplay {0}".format(parp)) sleep(2)
-
Save the file by clicking on File and Save.
- Test that your code works by clicking on Run and Run Module. Use your hand to push the top plate of your Whoopi cushion down to make a connection between the tinfoil sheets and you should hear a fun sound. If it does not work first time, do not worry. Check your code through. Have you typed your code out exactly as you see it here?
Program your whoopee cushion(Curso)
Let’s begin with simply playing the fart sound using Python. You’ll need to have some speakers or headphones plugged into your Raspberry Pi for this, as the computer has no inbuilt output devices for sound.
Test the sound
That’s the hardware complete. Now for the software! We are going to use Python. Don’t worry if you haven’t used it before: just follow the instructions and you will pick it up. You will be using the command line to enter commands. To do this you will need to open a terminal window by clicking on the terminal icon: it looks like a computer screen, and is found three icons along from the menu button on your desktop.- Connect the speaker to the Raspberry Pi using the sound jack port.
-
Create a new folder called
whoopee
by typing the following command in the terminal and pressing enter on the keyboard:
mkdir whoopee
-
Next, use the following command to enter the folder you have just created:
cd whoopee
-
Download a burp sample using the following command:
wget http://rpf.io/burp -O burp.wav
-
Now test that you can play the sound file using
aplay
by typing:
aplay burp.wav
To switch audio to the headphone jack, return to the terminal window and type the following command:
amixer cset numid=3 1
- If your Raspberry Pi is connected to the internet you could search for some suitable trumping sounds. They need to be in ‘wav’ format to work. Alternatively, you can download our example sounds here.
Write a program in Python
- Open Python 3 (IDLE) from the Programming menu and click on File and New Window. This will open a blank file.
- Click on File and Save As and name the file
whoopee.py
-
Type the following code into your file:
import os import random from time import sleep from gpiozero import Button
-
Then, you’ll need to use the Button class in
your code. You’ll have to tell it that the button is on pin 2. To do
this, write the following code in your new file:
button = Button(2)
-
Now create a list of all your sound effects and store then inside a variable that you can call later on in your code:
trumps = ['ben-fart.wav', 'ca-fart.wav', 'marc-fart.wav']
- Once all the setup needed in the code is
complete, you can move on to writing the part of the program that will
make something happen when the button is pressed. Begin by creating a
loop using
while True:
. - Then, add
button.wait_for_press()
inside the loop by indenting by four spaces. Each time around the loop, the computer waits for the button to be pressed. - On the next line, use the
random.choice
function to select a sound at random from the list you created earlier. That selected sound needs to be stored inside another variable which you can call parp! Typeparp = random.choice(trumps)
. - The next line will play the sound selected at random using
aplay
, which you used earlier to test your sounds. Typeos.system("aplay {0}".format(parp))
. - Finally, add
sleep(2)
to pause the program before it starts the loop again. -
Your code should look like this:
while True: button.wait_for_press() parp = random.choice(trumps) os.system("aplay {0}".format(parp)) sleep(2)
-
Save the file by clicking on File and Save.
- Test that your code works by clicking on Run and Run Module. Use your hand to push the top plate of your Whoopi cushion down to make a connection between the tinfoil sheets and you should hear a fun sound. If it does not work first time, do not worry. Check your code through. Have you typed your code out exactly as you see it here?
Setting it up
- Carefully place your whoopee cushion in a spot where your victim will sit on it (obviously!), but not under a really heavy cushion where it will be squashed straight away.
- The tricky bit is setting up the Pi so that it can’t be seen: remember, you’ll need a plug socket to connect it to power, unless you are using a battery pack.
- Run the program, and wait. Here’s a hint: whistle tunelessly and look around at the ceiling. This will make you seem innocent, and will help to attract potential victims.
Fuentes:
- Raspberry Pi Oficial. https://www.raspberrypi.org/learning/whoopi-cushion/worksheet/
- Curso de básico Raspberrypi. https://www.futurelearn.com/courses/physical-computing-raspberry-pi-python/1/steps/139157
Comentarios
Publicar un comentario