Lesson 5 – Buttons and the Distance Sensor

Java 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 String parameter that tells the program which button you are interested in. 

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.

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) {
} else {

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 statements inside the loop curly braces and moves on to the next line of code. In this case, the program turns the beak green for one second and then off.

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.

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.

Exercise 9

What do you think this code will do? Make a hypothesis, and then try it out.

Hint: ! is a logical operator like && and ||.

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.


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.