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

65 lines
1.6 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Compass Heading
2016-11-02 01:44:37 +01:00
Find which direction on a compass the @boardname@ is facing.
2016-06-10 00:09:48 +02:00
2016-11-02 01:44:37 +01:00
The @boardname@ measures the **compass heading** from `0` to `360`
2016-06-10 00:09:48 +02:00
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
2016-11-02 01:44:37 +01:00
that means whether the @boardname@ is facing north (N), south (S),
2016-06-10 00:09:48 +02:00
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
2016-11-02 01:44:37 +01:00
turned the @boardname@ on), the @boardname@ will start to **calibrate**
2016-06-10 00:09:48 +02:00
(adjust itself). It will ask you to draw a circle by tilting the
2016-11-02 01:44:37 +01:00
@boardname@.
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
2016-11-02 01:44:37 +01:00
confuse the @boardname@.
2016-03-26 00:47:20 +01:00
### See also
2016-04-13 17:27:45 +02:00
[acceleration](/reference/input/acceleration)