In our previous projects, and , we introduced the concept of controlling and monitoring the state of a component (LEDs and buttons) by using GPIOs as inputs/outputs. In the gaming world, this concept gets more useful — and interesting — as we add more components. Unfortunately, it can also get tricky to manage inputs from, and outputs to, lots of different components. Which is why it's important to learn how to use lists to manage sets of similar components. We will also often use loops along with those lists to efficiently handle the information flowing to and from those components.
In this project, we're going to wire four buttons to four different GPIOs. Based on the projects we've already done, it shouldn't be too difficult to create the circuit. But, then we're going to handle the input from those buttons using a list, instead of trying to manage input from each button individually. While this project will focus on buttons, the concept of monitoring inputs and controlling outputs using lists and loops can be applied to pretty much any component.
Wiring four buttons to four different GPIOs is no different than wiring a single button to a GPIO and then replicating that circuit four times. Give it a try!
Here are three things to remember as you're wiring your buttons:
The unconnected sides of each button should sit on different connect strips
One side of each button should be wired to ground
One side of each button should be wired to your chosen GPIO
Here is what your breadboard should look like at this point (we've chosen to use GPIO14, GPIO15, GPIO25 and GPIO7 for our buttons):
For this project, we're simply going to create a list of buttons and then
use a for
loop to cycle through the list and determine which
buttons are current being pressed.
Here is what that code will look like:
Let's look at this code in more detail:
On Line 1 and 2, we import the modules and functions we need
On Line 4, we create a list called buttons
that contains the
initialization for each of the four buttons we've created. For each button
in the list, we call the Button() function, which returns a "button object"
for each item in the my_buttons
variable. We talked about that
a bit in , but the important thing to understand is
that we can use these objects to access Button()
functions and
attributes
On Line 6, we start a loop (this is where we'll spend all of our time once we run the program)
On Line 7, we create a for
loop that we use to cycle
through the items in the my_buttons
list
On Line 8, we check to see if the next button
in our list
is currently being pressed
On Line 9, if the button
we observed on Line 8 was being
pressed, we send that information to the Program Output window
Lines 7-9 will loop for each of the buttons in the list
my_buttons
On Line 10, we pause briefly (so we don't hog the processor) and then
return to the top of the while
loop to check our buttons
again
By using lists and loops, we've reduced the amount of code required to create and test four buttons from about 20 lines of code down to about 6 lines of code!
While that might not seem significant, imagine if you were creating something with dozens or hundreds of buttons (like a piano keyboard that has 88 keys)? The code to monitor the 88 keys on a piano keyboard would be no longer than the code you see above. When you run this code, you'll see that every time you press a button, that information is sent to the Program Output window.
Write a little game to see how fast you can push a single button ten times. You'll need to keep a count of how many times the loop runs, and a count of how many times the button was pressed. Once the button is pressed 10 times, break out of the loop, and print the count.
Write a little game to see how fast you can push all of the buttons, one at a time. You'll need to keep a count of how many times the loop runs, and then keep track of whether each button is pressed. Once they are all pressed, break out of the loop, and print the count.
Try replacing the four buttons above with four LEDs (make sure to wire them correctly as GPIO outputs), and then see if you can change the code to flash each of the LEDs one at a time using a list structure.
With four LEDs added, each one next to a button, light just one specific LED. Then, if a button next to that LED is pressed within half a second, the user wins. Have another player try to hit the correct button when you press start. This is the beginnings of Whac-A-Mole style amusement park game.