22.12.2016 Views

CODING

MagPi53

MagPi53

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

MUSIC BOX<br />

Tutorial<br />

ch4listing1.py<br />

import pygame.mixer<br />

from pygame.mixer import Sound<br />

Language<br />

>PYTHON 3<br />

DOWNLOAD:<br />

magpi.cc/2bhwqlH<br />

pygame.mixer.init()<br />

drum = Sound("samples/DrumBuzz.wav")<br />

while True:<br />

drum.play()<br />

ch4listing2.py<br />

Below Each time you press a button, the assigned sound sample<br />

will play through a connected speaker<br />

>STEP-04<br />

Add a second button<br />

We’ll now add a second button, as in the diagram.<br />

Wire it to GPIO 3 and the ground rail. Open a new file<br />

in Python 3 IDLE, enter the code from ch4listing3.py,<br />

and save it in musicbox. Note that rather than<br />

assigning the Button objects and sounds to the pins<br />

individually, we’re using a dictionary structure to<br />

assign their numbers to sound samples:<br />

sound_pins = {<br />

2: Sound("samples/DrumBizz.wav"),<br />

3: Sound("samples/CymbalCrash.wav"),<br />

}<br />

We then create a list of buttons on all the pin<br />

numbers in the sound_pins dictionary: buttons =<br />

[Button(pin) for pin in sound_pins]<br />

Finally, we create a for loop that looks up<br />

each button in the dictionary and plays the<br />

appropriate sound:<br />

for button in buttons:<br />

sound = sound_pins[button.pin.number]<br />

button.when_pressed = sound.play<br />

Run the program and press each button to hear a<br />

different sound.<br />

>STEP-05<br />

Add more buttons<br />

The way we have structured the program makes it<br />

easy to add extra buttons and assign them to sound<br />

samples. Just connect each button to a GPIO number<br />

pin (not any other type) and the ground rail, as before.<br />

Then add the GPIO pin numbers and sounds to the<br />

dictionary, as in the following example:<br />

from gpiozero import Button<br />

import pygame.mixer<br />

from pygame.mixer import Sound<br />

from signal import pause<br />

pygame.mixer.init()<br />

button = Button(2)<br />

drum = Sound("samples/DrumBuzz.wav")<br />

button.when_pressed = drum.play<br />

pause()<br />

ch4listing3.py<br />

from gpiozero import Button<br />

import pygame.mixer<br />

from pygame.mixer import Sound<br />

from signal import pause<br />

pygame.mixer.init()<br />

sound_pins = {<br />

2: Sound("samples/DrumBuzz.wav"),<br />

3: Sound("samples/CymbalCrash.wav"),<br />

}<br />

buttons = [Button(pin) for pin in sound_pins]<br />

for button in buttons:<br />

sound = sound_pins[button.pin.number]<br />

button.when_pressed = sound.play<br />

pause()<br />

sound_pins = {<br />

2: Sound("samples/DrumBizz.wav"),<br />

3: Sound("samples/CymbalCrash.wav"),<br />

4: Sound("samples/Gong.wav"),<br />

14: Sound("samples/HandClap.wav"),<br />

}<br />

raspberrypi.org/magpi January 2017<br />

47

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!