Linux Format

Raspberry Pi GPIO................

Les Pounder shows us how to use GPIO Zero, buttons and LEDs to create your own simple 1980s inspired game.

- Les Pounder works with the Raspberry Pi Foundation delivering its Picademy training. He also likes to tinker with electronic­s and toys and blogs at http://bigl.es

Les Pounder explains how to use GPIO Zero to create a simple 1980s-inspired memory game.

The Simon memory game was particular­ly popular when we were growing up. This was an electronic game that used LEDs and big buttons to challenge players to remember and repeat a complex sequence. We’re going to make our own version using a Raspberry Pi.

In this tutorial, we’ll use coloured LEDs (one blue, green, red and yellow) and four momentary buttons/switches which we’ll assign to each LED and we’ll be using Python and GPIO Zero to code the project. You can use any model of Pi, but you’ll need the latest Raspbian Pixel Release. You’ll also need four 220 Ohm resistors (orange-orange-brown-gold), a halfsize breadboard, 10 male-to-female jumper wires and four male-to-male jumper wires. For instructio­ns on how to connect your circuit refer to the diagram. (All of the code for this project and the diagram can be found at https://github. com/lesp/LXFMemoryG­ame/archive/master.zip.)

All of the software necessary for this project is preinstall­ed on Raspbian/Pixel. So to start coding we need to click on the main menu and go to Programmin­g > Python 3 Editor. Once the editor opens we need to click on File > New to open a new blank document and click on File > Save in the blank document and call your file memorygame.py.

Our first block of code involves importing modules that are used to provide extra functions for our project. import time import random from gpiozero import LED, Button

Here we import the time library so that we can control the pace of our project. We import random to introduce random numbers into our project. Last, we import two sections of the GPIO Zero library, which deal with LEDs and buttons. GPIO Zero has a class called LED which is used to control the GPIO pins as outputs. We tell the class which pin is connected to the long leg of each LED. Here we create four objects that store the location of each LED for each colour: red_pin = LED(2) yellow_pin = LED(3) green_pin = LED(4) blue_pin = LED(17)

There is also a class to work with GPIO pins as inputs and it is called Button . Here we use it to create objects for inputs. red_button = Button(14) yellow_button = Button(15) green_button = Button(18) blue_button = Button(23)

To control our LEDs we are going to create a function, called lights that has two additional arguments: def lights(colour,duration):

A function is a block of code that can be executed by calling its name. The arguments for the function are for the LED colour and how long it should be displayed:

Inside of the function, all lines of code are automatica­lly indented four spaces. We create a conditiona­l test that will use the colour argument to turn on the correct coloured LED.

So if we used "red" then the red LED will turn on. if colour == "red": red_pin.on() time.sleep(duration) red_pin.off() time.sleep(duration) elif colour == "yellow": yellow_pin.on() time.sleep(duration) yellow_pin.off() time.sleep(duration) Afterwards, we use the sleep function with the duration to control how long the LED is on for. We then turn the LED off and again pause for the duration. We do this for each LED, here ( above) we see the first two blocks of code that control

"red" and "yellow" , the code for “green” and “blue” is the same as "yellow" . We’ve truncated the code.

Now out of the function we create a list; a data structure that’s used to store multiple pieces of data, each with their own place, called an ‘index’ in the list. Here we call the list leds and store the colours of each LED. leds = ["red”,“yellow”,“green”,“blue"]

To check that our LEDs are working and to signify the game is ready we use a for loop that will iterate over each colour in our leds list and call our lights function. for led in leds: lights(led,0.3) We now enter into an infinite loop that will run our game, all of the code inside of the loop is indented four spaces. while True:

Inside the loop we create a trigger to start the game. This trigger is pressing the button assigned to match our “red” LED. When pressed, it starts a countdown using print : red_button.wait_for_press() print("Standby for light sequence!!!") time.sleep(1) print("3") time.sleep(1) print("2") time.sleep(1) print("1") time.sleep(1) print("GO!!!")

As soon as the countdown is over we immediatel­y shuffle the contents of the leds list we created earlier. This gives us a random list of colours for our game. random.shuffle(leds)

We then play the lights sequence to the player using a for loop that will iterate over each entry in our leds list and will use our lights function to turn on each LED. for led in leds: lights(led,0.2) We then create another list, called answer but this time we leave it empty, we shall fill it later. answer = []

We now enter another while loop, inside the original while True loop. This new while loop will run for as long as the length of our answer list is less than four items long. while len(answer) <4: The goal of this loop is to capture button presses correspond­ing to the colours of the LED sequence: time.sleep(0.3) if red_button.is_pressed:

answer.append(“red”) lights(“red”,0.1) print(answer) elif yellow_button.is_pressed: answer.append(“yellow”) lights(“yellow”,0.1) print(answer)

To capture the sequence, we use a delay of 0.3 seconds to ensure the LEDs have stopped flashing. Next, we use another conditiona­l statement test that checks to see the status of our buttons. Once a button is pressed the correspond­ing colour is appended to our answer list and the correct colour flashes to indicate our selection. We also print the answer to the shell. We now come out of the if conditiona­l test and while len loop, but remain inside the main while True loop.

Our final section of code is another if conditiona­l test, but it compares the randomly chosen sequence of LEDs with the player’s answer. If correct, the player wins and we use a for loop to blink all of the LEDs. if leds == answer: print("YOU WIN!!!") for blink in range(10): lights("red",0.1) lights("yellow",0.1) lights("green",0.1) lights("blue",0.1) But if the player gives an incorrect answer they lose the game and only see a blinking red LED. else: print("YOU LOSE!!!") for blink in range(10): lights("red",0.1)

With that our project is complete. Save your work and click on Run > Run Module to launch the game. To make the game harder, shorten the duration in the lights function.

 ??  ?? The project’s circuit has many components but it’s easy, just trace each line from the Pi to the component.
The project’s circuit has many components but it’s easy, just trace each line from the Pi to the component.
 ??  ??
 ??  ?? Momentary switches/push buttons come in various sizes, but generally if they look like these then they will fit into your breadboard.
Momentary switches/push buttons come in various sizes, but generally if they look like these then they will fit into your breadboard.

Newspapers in English

Newspapers from Australia