In this lesson, you will learn to use the Finch compass. The compass tells you the direction of your micro:bit relative to magnetic north.
Lesson 13 – Compass
Calibrating the Compass
Using the Compass
Once you have successfully calibrated, you can use the getCompass() method to read the value of the compass. This method requires no parameters and returns the value of the compass from 0° and 359°. 0° corresponds to the direction of magnetic north. The angle increases as the robot turns clockwise, so 90° is east, 180° is south, and 270° is west. To use the compass, the Finch must be level to the ground. Otherwise, the compass will not provide useful measurements.
Write a function that returns a string that indicates the direction that the Finch is facing. The function should return the following directions: N, NE, E, SE, S, SW, W, NW. Any compass heading within ±15° of a given direction should return that direction; other headings should return an empty string. For example, angles between 75° and 105° should return ‘E’, while angles between 120° and 150° should return ‘SE’. Test your function by printing the direction on the micro:bit screen as you slowly turn the Finch. Remember to include a pause so that your robot has time to print two letters to the micro:bit screen.
Write a program that makes the Finch always drive north. This problem is similar to line tracking. When the robot is facing west of north, it should turn right. When it is facing east of north, it should turn left.
Modify your code from Exercise 2 so that the Finch can also avoid obstacles. If there is no obstacle, the Finch should head north. If there is an obstacle, the Finch should back up and turn right.
Suppose that you want the Finch to play a different note for each direction. One way to do this would be to use an if-elif-else statement and include a case for each direction. However, you can make your code more efficient using a dictionary. A dictionary is similar to a list, except that every element consists of a key and a value. Instead of accessing a value using its index (as you did with lists), you access a value by placing its key inside square brackets. For example, this code defines a dictionary sampleDictionary with three entries. Each entry has a string key that is related to a note value. The playNote() method accesses note 64 using the key ‘e’.
Create a dictionary that contains eight notes. The key for each note should be one of the directions returned by your function from Exercise 1. Write a program that uses the dictionary to make the Finch play notes as you turn it in different directions. Remember that your direction function sometimes returns the empty string. The program should not play a note when it receives the empty string.
Create a dictionary named directionColor that contains eight lists with three elements. Each list should represent a color. The key for each color should be one of the directions returned by your function from Exercise 1. Use this dictionary to modify your code from Exercise 3 so that the beak and tail of the Finch change to a different color for each note.
Hint: You can access the elements of each list using a second set of square brackets. For example, this code accesses the first element of the list that corresponds to key.