Compass

The microbit has a compass inside it. The compass shows your direction headings. These are the numbers in the picture. On the microbit, compass headings go from 0, which is North, all the way round to 359. This is 1° away from getting back to North again.

In this project we write code to show the letters N, E, W or S. The letters are short for North, East, South or West. The microbit is using the magnetometer to measure the earth's magnetic field. This means we are measuring magnetic compass bearings not the true directions shown on a map.

The microbit has to set up its compass. This is why we need the 'calibrate compass' block. The screen fills up with dots while it is calibrating. When all the LEDs are lit up, the program runs.

We work out which direction the microbit is facing by dividing the heading by 90. Then we round it to the nearest number. Use the 'help' to read about what the 'round' block does. The result of this is a number from 0 to 4.

When we start up the program we put the letters N E S W N in an array. Now the number we have worked out can be used to get the right letter from the array.

Can you figure out why we put N in the array twice?

Hints:
The first item in an array is at position = 0.
The 'round' block rounds up as well as down.
We have worked out the numbers 0 to 4, which is 5 numbers.
So we need 5 letters in our array.

This table shows how the letters fit in our array:

01234
NESWN

Here is the code


Did you wonder why the first picture is called a compass rose? This is because they are often drawn like this picture. When it is drawn like this it looks like a rose!


A More Realistic Compass

We can make a more realistic compass. As we turn the microbit round we could have an arrow that always points North. This is what magnetic compasses do.

To do this we could use the 'show arrow' block. There are 8 arrows available. The 8 arrows are numbered from 0 to 7. This is because they are kept in a kind of list called an array and arrays are always counted starting from 0. The arrows point in the same directions as the points of the compass.

0 1 2 3 4 5 6 7
North North-East East South-East South South-West West North-West

The sums we have to do to decide which arrow to use are quite complicated. Here is an explanation of them:

  1. The compass headings are the numbers 0 to 359
  2. There are 360 numbers in this range
  3. Imagine that we start off by facing North
  4. Then we turn round through part of a circle
  5. To get back to facing North again we have to turn round the rest of the circle.

So our first instruction will be a maths block containing the sum (360 - compass heading)

  1. We only have 8 arrows so we need to reduce our 360° to just 8 numbers
  2. 8 divides into 360 exactly 45 times
  3. So we use another maths block to divide (360 - compass heading) by 45
  4. Now use the 'round' block: it changes the result of our sums to the nearest whole number
  5. We have worked out a number from 0 to 8
  6. But the arrows are numbered from 0 to 7
  7. So we use the remainder block to get the remainder after we divide by 8
  8. Now our numbers go from 0 to 7
  9. This is perfect to select the arrow we need.

So finally, we put all our sums in the 'show arrow' block. Here is the code:

Just for fun, here is the same code in JavaScript:


                input.calibrateCompass()
                basic.forever(function () {
                    basic.showArrow( Math.round( (360 - input.compassHeading() ) / 45 ) % 8 )
                })

            

In JavaScript we use brackets to show which sums to do first. The innermost brackets are worked out first, then the next brackets out, and so on until all the sums are done. So if you look closely you will see how this does all the sums in the right order. Then, finally, the code uses showArrow.

Notice the use of the % sign. This replaces the 'remainder' block in MakeCode. Quite a lot of coding languages use the % sign to do exactly the same thing.

Another Idea

There is another way to show the compass points N E S W. This way doesn't have any sums in the code at all. What we do instead is to fill up a big array with 360 letters in it. Then we just use 'compass heading' to get the right letter from the array.

There is a detail we need to get right. We want to show N for North when the heading is either between 0 and 45 or between 315 and 360. So we put N into the first 45 places in the array, then we put the letter E in 90 times, then the letter S in 90 times, then the letter W in 90 times and then N in another 45 times.

You can see why we need to do this by looking carefully at the compass rose.