The Finch contains two line sensors on the bottom of the Finch on either side of the on/off button. They are infrared sensors that emit infrared radiation and measure the amount reflected by the surface below the Finch. You can use these sensors to detect whether each sensor is over a white surface or a black surface.
Lesson 9 – Line Tracking
Using the Line Sensors
To use the line sensors in a Boolean expression, you need a threshold. Write a program to find this threshold. Your program should do the following:
- Prompt the user to place the Finch over a white surface.
- Record the mean of the left and right line sensors in a variable named white.
- Prompt the user to place the Finch over a black surface.
- Record the mean of the left and right line sensors in a variable named black.
- Compute a threshold for the line sensors that is the mean of white and black.
- Reports the threshold to the user.
Write a program to make that robot drive forward until it detects a black line.
Try tracking a line with the code above. Then try modifying the setMotors() commands. How fast can you track the line before you lose it?
Note: As you run a Java program, the BlueBird Connector relays commands over Bluetooth to the Finch. There is some delay in this process, and that limits how fast you can track the line.
Write a program that tracks a line as long as there is no obstacle in the Finch’s path. If there is an obstacle, the Finch should stop, but it should start tracking again when the obstacle is removed. The Finch should continue tracking and stopping for obstacles until button A is pressed.
micro:bit V2 Extra Challenge
If your Finch contains a micro:bit V2, you can also use a sound sensor in the micro:bit to measure the volume of sound around the Finch from 0 – 100. The getSound() method returns the int value of this sensor. If you don’t have a micro:bit V2 in your Finch, you will see an error when you use this method.
Write a program that uses the sound sensor to start line tracking when you clap. Another clap should then stop the line tracking.
Turning left or right based on a single line sensor is the simplest version of a line tracking algorithm, but there are many variations. Write a program to implement the alternate line tracking algorithm described below. Which works better for your path?
- If the left line sensor is over white and the right is over black, turn right.
- If the right line sensor is over white and the left is over black, turn left.
- If both line sensors are over the same color, move forward in a straight line.
By using both line sensors, the Finch can also detect places where a path branches. When the Finch reaches the branch point in a path like the one below, both of the line sensors will be over black. Use black electrical tape to create a path on a large sheet of white paper. The path should contain a branching point where the Finch will need to decide between two different paths.
In this exercise, you will modify your code from Exercise 5 so that when the Finch reaches the branching point, it decides which branch to take. You will place an obstacle on one branch, and the robot should take the branch without an obstacle. Your robot must do the following:
- Stop when it reaches the branch point (both line sensors detect black).
- Turn to face one path, pause, and check for an obstacle. If there is no obstacle, the robot should move forward slightly (to get past the branch point) and then begin to track this path.
- If there is an obstacle, the robot should turn toward the other path, move forward slightly (to get past the branch point), and begin to track this path.