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.
Lesson 5 – Buttons and the Distance Sensor
Using the Buttons
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.
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.
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.
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.
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.
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.
Write a program to make the Finch move forward until it is close to an obstacle.
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.
Write a program that moves the Finch back and forth until you press button B.
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.
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.