Lesson 13 – Compass

Java Concepts

Dictionaries

Teacher Materials

Get Access

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.

This picture shows that the compass is in the micro:bit in the tail of the Finch.

Calibrating the Compass

Before you use the compass, you need to calibrate it in the BlueBird Connector.  To do this, click on the purple compass button next to the name of your device. Follow along with the video to move your Finch around in different directions to calibrate it. You should see a green check 

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.

As an example, suppose you want to display ‘E’ on the micro:bit screen when the angle of the Finch is between 75° and 105° (i.e., pointing within 15° of 90°). 

Exercise 1

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.

Exercise 2

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.

Exercise 3

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.

Dictionaries

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 array, except that every element consists of a key and a value. Instead of accessing a value using its index (as you did with arrays), you access a value using its key. For example, this code defines a dictionary sampleDictionary and then adds three entries to it. Each entry has a String key that is related to a note value. Notice that the dictionary has to use type Integer instead of type int; for our purposes, these are the same. After the dictionary is defined, the playNote() method accesses note 64 using the get() method with the key “e”.

When using a dictionary, you often want to check if a given key is in the dictionary. You can do this using the .containsKey() method. For example, this code will not play a note because the key “a” is not in the dictionary.

Exercise 4

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.

Dictionaries can also contain values that are arrays. For example, each key in this dictionary corresponds to an array of three elements.

Exercise 5

Create a dictionary named directionColor that contains eight arrays with three elements. Each array 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 still can access the elements of each array using a set of square brackets. For example, this code accesses the first element of the array that corresponds to key.