Reflection: Selection in physical computing
Just like last week, in this step you are going to reflect on what you achieved in the project you just completed. You are going to think about buttons and selection in physical computing.
Buttons and switches are a class of input component. They allow a user to have some control over a circuit or to send signals to a computer.
What type of data do we get from our button? A button can either be pressed or not; it can have one of two values, so a button returns Boolean data. How can programs react to this?
Programming is governed by three universal principles: sequence, selection, and iteration. In this course you will use all three of them in your projects, but for now I would like to focus on selection.
Selection refers to a process in your code that changes which instructions are executed; you don’t necessarily want every piece of code to run each time your program does. You want to select a specific part of your code to run, based on conditions. The most recognisable form of selection is the ubiquitous
if temperature < 10: print("It is cold! Wear a jacket") elif temperature >= 10 and temperature < 25: print("Maybe a jumper would be appropriate") elif temperature >= 25 and temperature < 40: print("It's t-shirt weather") else: print("Maybe stay inside and make sure you drink a lot of water")
These constructs use a condition, set by the programmer, to control whether or not the pieces of code contained in them execute.
- If the condition is
True, the code executes, and if it is
False, those lines of code are skipped
- You can also use an
elifstructure to check another condition after an
ifstatement; the second condition will only be checked if those preceding it are
elsestatement will run the instructions contained within when the
elif(if you have them) statements proceeding it return
Making use of these structures allows our programs to behave differently under different circumstances, depending on the input.
Using buttons for selection
The GPIO Zero documentation for buttons can be found here. I will be walking you through some of the other methods and properties of buttons in this step, and I will link to the relevant points in the documentation.
In the previous step, you used an
if statement to check the state of the button and to turn the LED on when the button was pressed. You did this with the
is_pressed property of the button, accessing the raw Boolean data.
In the previous example, your code ran only when the button was pressed; now I would like you to switch it around.
Create another new Python file and copy your original light switch program into it.
Can you edit the program so that the light is on by default, and only turns off when the button is pressed?
Events in computing are detectable occurrences that we can react to in our programs. The button you have attached has two states, and an event is triggered when those states change. GPIO Zero allows you to use two events for the button. The
pressed event is triggered when the switch inside the button is closed, and the
released event is triggered when the switch opens again.
Create a new file in Mu.
Add the following to your program:
from gpiozero import Button, LED btn = Button(4) led = LED(17) btn.when_pressed = led.on
Save and run your code. Press the button.
You should see the LED turn on. You just used event-driven programming to control an LED!
Note: You may have noticed that when I set
led.on, I did not add brackets to the
onmethod. This is important. When setting a reaction to the
when_pressedevent, I am providing a reference to the method, not calling it. The event only needs the name of a function to call; when it is triggered, it will find the method and call it. If you include brackets with the
onmethod it is called immediately.
when_releasedbutton event to turn the LED off again.
Waiting for events
Another way you can use events is to have your program halt until an event is triggered.
while True: btn.wait_for_press() led.on() btn.wait_for_release() led.off()
Buttons make things happen
You have now learnt how buttons can be used with GPIO Zero to instruct your programs to do things.
Use the comments section to share some of your ideas for things that you could make your Raspberry Pi do when a button is pushed or released.