Lesson 5 – Buttons and the Distance Sensor

Python Concepts

While loops

Teacher Materials

Get Access

The last two lessons focused on the Finch outputs. Now we will return to the Finch inputs and learn how to use the Finch sensors to make decisions about how the robot should behave. In this lesson, you will use the buttons and the distance sensor.

A picture of the Finch showing the buttons in the micro:bit in the Finch tail and the distance sensor under the beak.

Using the Buttons

As you may recall from Lesson 2, the getButton() method returns a Boolean value that is True when a button is pressed and False otherwise. The getButton() method takes one parameter that tells the program which button you are interested in. 

bird.getButton('A') 
 The getButton() function takes one parameter, which can be either 'A' or 'B'.

In the last lesson, you learned to use an if-else statement to make a decision based on a Boolean condition. Since getButton() returns a Boolean value, you can use it within an if-else statement. For example, the code below turns the Finch beak green if you are pressing button A when you run the program. Otherwise, the beak turns red.

if bird.getButton('A'): # If pressing button A 
bird.setBeak(0,100,0) # Beak green 
else: # Otherwise 
bird.setBeak(100,0,0) # Beak red 
sleep(1) 
bird.stopAll()

Exercise 1

Try out the sample code shown above. Then modify it so that you can press either button A or button B to turn the beak green.

Note: The program above only checks the status of the button at the instant that it reaches the if statement. For the beak to turn green, you should hold the button down while you start the program.

Exercise 2

Write a program that moves the Finch backward if button B is pressed. Otherwise, the Finch should move forward. 

micro:bit V2 Extra Challenge

If your Finch contains a micro:bit V2, you also have a third button! A micro:bit V2 has a gold micro:bit logo, and this logo is a button that senses when you touch it. getButton(“Logo”) will return the value of this button. If you don’t have a micro:bit V2 in your Finch, you will see an error when you use getButton(“Logo”).

Write a program that turns the Finch in a circle to the left if the logo is pressed. Otherwise, the Finch should turn to the right.

Using the Distance Sensor

You can use comparison operators to create Boolean conditions that use other Finch sensors, such as the distance sensor. The getDistance() method returns the distance to the closest object in centimeters. The range of the Finch distance sensors is approximately 2-200 cm. The distance sensor cannot measure an object that is pressed directly against it, so make sure the object is at least 2 cm away.

Exercise 3

What do you think this code will do? Make a hypothesis, and then try it out. In this code, the value of the distance sensor is also printed to the screen to aid in debugging.

if bird.getDistance() < 30: 
bird.setTail("all",0,0,100) 
else: 
bird.setTail("all",100,0,0) 
sleep(2) 
bird.stopAll()

Exercise 4

Write a program that moves the Finch forward if it does not sense an obstacle. If the Finch does see an obstacle, it should move backward.

While Loops

As you have seen, an if-else statement checks a Boolean condition only once. However, sometimes you want to keep checking the condition as long as it is true. To do this you can use a while loop. A while loop repeats the statements inside it as long as a Boolean expression is true. 

As an example, consider the code below. This code will make the Finch beak red for as long as the value of the distance sensor is less than 30 cm. The keyword while is followed by a Boolean expression, bird.getDistance() < 30. If this expression is true, the program runs the statement inside the loop and turns the beak red. Then it checks the Boolean expression again. If it is still true, the program turns the beak red again, so you will see it stay red. The loop continues repeating the statement inside it until it checks the Boolean expression and finds that it is false. Then the program skips the indented statement inside the loop and moves on to the next unindented line of code. In this case, the program turns the beak green for one second and then off.

while bird.getDistance() < 30: # As long as distance less than 30 
bird.setBeak(100,0,0) # Red 
bird.setBeak(0,100,0) # Green 
sleep(1) 
bird.stopAll()

Exercise 5

Try out the sample code above. What will happen if nothing is near the distance sensor when this code runs? Make a hypothesis, and then try it out.

Exercise 6

Write a program to make the Finch move forward until it is close to an obstacle.

Exercise 7

If you run the code below, how long will you have to hold your hand in front of the distance sensor to make it stop blinking? Make a hypothesis, and then try it out. Make sure you can explain your answer. 

while bird.getDistance() > 20: 
bird.setTail("all",0,0,100) 
sleep(3) 
bird.setTail("all",100,0,100) 
sleep(3) 
bird.stopAll()

Exercise 8

Use the Finch as a security system! Place the Finch in front of a door. As long as the door is closed, the beak and tail should blink red. When the door opens (obstacle gone), the Finch should go crazy with light and movements. 

You can also use while loops with the buttons. This while loop will blink two tail LEDs as long as button A is pressed. The user must press the button before the while loop starts.

[print("Hold down button A and wait for the blinking to start") 
sleep(3) 
while bird.getButton('A'): # While button A is pressed 
bird.setTail(1,0,100,0) 
sleep(0.1) 
bird.setTail(3,0,100,0) 
sleep(0.1) 
bird.stopAll() 
sleep(0.1)

Exercise 9

What do you think this code will do? Make a hypothesis, and then try it out. Hint: not is a logical operator like and & or.

while not bird.getButton('A'): # Repeat until button A is pressed 
bird.setTail(1,0,100,0) 
sleep(0.1) 
bird.setTail(3,0,100,0) 
sleep(0.1) 
bird.stopAll() 
sleep(0.1)

Exercise 10

Write a program that moves the Finch back and forth until you press button B.

Exercise 11

Compare this code to the code in Exercise 5. What do you think will happen when the code runs? Make a hypothesis, and then try it out.

bird.setBeak(100,0,0) 
while bird.getDistance() < 30: 
pass 
bird.setBeak(0,100,0) 
sleep(1) 
bird.stopAll()

Nesting

You can also place one control structure (loop or decision statement) inside another; this is called nesting. Nesting can help you to create more complex programs.

For example, the code below has an if-else statement inside a while loop. This means that the if-else is repeated over and over until button A is pressed. The if-else statement sets the color of the tail lights based on the value of the distance sensor. When you run this code, the tail lights will change color as you move an object close to the Finch and away. When you press button A, the program will stop checking the if-else and move to the stopAll() command, turning the lights off.

Exercise 12

Try out the code above. Then modify this code to make your Finch avoid obstacles! If there is an obstacle in front of the Finch, it should back up and turn. When there is no obstacle, the Finch should move forward.

Back to Top