pxt-calliope/docs/reference/input/compass-heading.md

69 lines
1.6 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Compass Heading
2016-06-10 00:09:48 +02:00
Find which direction on a compass the micro:bit is facing.
The micro:bit measures the **compass heading** from `0` to `360`
degrees with its **magnetometer** chip. Different numbers mean north,
east, south, and west.
2016-03-26 00:47:20 +01:00
```sig
input.compassHeading();
```
### Returns
2016-06-10 00:09:48 +02:00
* a [number](/reference/types/number) from `0` to `360` degrees, which means the compass heading. If the compass isn't ready, it returns `-1003`.
2016-03-26 00:47:20 +01:00
### Example
2016-06-10 00:09:48 +02:00
This program finds the compass heading and stores it in the
`degrees` variable.
2016-03-26 00:47:20 +01:00
```blocks
let degrees = input.compassHeading()
```
### ~hint
2016-06-10 00:09:48 +02:00
When you run a program that uses this function in a browser, click and drag
the compass needle on the screen to change the compass heading.
2016-03-26 00:47:20 +01:00
### ~
### Example: compass
2016-06-10 00:09:48 +02:00
This program finds the compass heading and then shows a letter
that means whether the micro:bit is facing north (N), south (S),
east (E), or west (W).
2016-03-26 00:47:20 +01:00
```blocks
basic.forever(() => {
let degrees = input.compassHeading()
if (degrees < 45)
basic.showString("N")
else if (degrees < 135)
basic.showString("E")
else if (degrees < 225)
basic.showString("S")
else basic.showString("W")
})
```
### Calibration
2016-06-10 00:09:48 +02:00
Every time you start to use the compass (for example, if you have just
turned the micro:bit on), the micro:bit will start to **calibrate**
(adjust itself). It will ask you to draw a circle by tilting the
micro:bit.
2016-03-26 00:47:20 +01:00
2016-06-10 00:09:48 +02:00
If you are calibrating or using the compass near metal, it might
confuse the micro:bit.
2016-03-26 00:47:20 +01:00
### Lessons
2016-04-13 17:27:45 +02:00
[compass](/lessons/compass)
2016-03-26 00:47:20 +01:00
### See also
2016-04-13 17:27:45 +02:00
[acceleration](/reference/input/acceleration)