Using the hardware setup from Many Buttons, we can turn our RaspberrySTEM into a very basic sound machine. We will start with four buttons each wired to a GPIO as input, and write some code that will generate corresponding note when each button is pressed. We'll keep it simple in this project, and just play each note for one second.
Step #1: Build the circuit.
Your breadboard should still have the circuit we wired in the MANY BUTTONS project. As a reminder, here is what it should look like:
Step #2: Write the code.
The code for this project is straightforward:
First, we import the functions we'll be using.
Next, we initialize the buttons with their corresponding GPIOs and initialize the notes from our sound library.
Then, we create a while: loop that will continually test each button for a press, and if a press if received, will play the corresponding note.
Here is what the code should look like: