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
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
The @boardname@ measures the **compass heading** from `0` to `359`
|
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();
|
|
|
|
```
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Returns
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
* a [number](/types/number) from `0` to `359` degrees, which means the compass heading. If the compass isn't ready, it returns `-1003`.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Example
|
2016-03-26 00:47:20 +01:00
|
|
|
|
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()
|
|
|
|
```
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## ~hint
|
2016-03-26 00:47:20 +01:00
|
|
|
|
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
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## ~
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Example: compass
|
2016-03-26 00:47:20 +01:00
|
|
|
|
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
|
2019-12-02 05:58:26 +01:00
|
|
|
let degrees = 0
|
2016-03-26 00:47:20 +01:00
|
|
|
basic.forever(() => {
|
2019-12-02 05:58:26 +01:00
|
|
|
degrees = input.compassHeading()
|
|
|
|
if (degrees < 45) {
|
|
|
|
basic.showArrow(ArrowNames.North)
|
|
|
|
} else if (degrees < 135) {
|
|
|
|
basic.showArrow(ArrowNames.East)
|
|
|
|
} else if (degrees < 225) {
|
|
|
|
basic.showArrow(ArrowNames.South)
|
|
|
|
} else if (degrees < 315) {
|
|
|
|
basic.showArrow(ArrowNames.West)
|
|
|
|
} else {
|
|
|
|
basic.showArrow(ArrowNames.North)
|
|
|
|
}
|
2016-03-26 00:47:20 +01:00
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Calibration
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2016-06-10 00:09:48 +02:00
|
|
|
Every time you start to use the compass (for example, if you have just
|
2019-12-02 05:58:26 +01:00
|
|
|
turned the @boardname@ on), the @boardname@ will start to [calibrateCompass](/reference/input/calibrate-compass)
|
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
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## ~ hint
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
Keep the calibration handy by running it when the user pressed **A+B**.
|
|
|
|
|
|
|
|
```block
|
|
|
|
input.onButtonPressed(Button.AB, () => {
|
|
|
|
input.calibrateCompass();
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
## ~
|
|
|
|
|
|
|
|
## See also
|
|
|
|
|
|
|
|
[acceleration](/reference/input/acceleration), [calibrateCompass](/reference/input/calibrate-compass)
|