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:

  1. The unconnected sides of each button should sit on different connect strips

  2. One side of each button should be wired to ground

  3. 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 GPIO12 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:

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.