pxt-calliope/docs/reference/input/compass-heading.md
Juri Wolf a93febb5b7
Map and clean deprecated functions (#175)
* add image and deprecated arrow functions

* update locales

* map basic.showArrow

* map arrow blocks

* map & remove arrow images

* remove arrow blocks

* update locales

* remove & patch:
rgbw -> rgb
button/pin pressed -> button/pin event
loudness -> soundLevel

* update ts mappings for arrows

* add wip ts patch rules

* update .blocks files

* use Click instead of Down as default in Documentation and tests

* patch test.blocks

* fix lowercase name tag

* update test.blocks

* update blocks test files

* update blocks test files

* format block files

* pass blocks file tests

* fix ts mapping

* fix color.defl value

closes https://github.com/microsoft/pxt-calliope/issues/136

* fix ts mappings

- add optional spacing at the end of rgbw()
- map up to v4.0.19

* add suggested changes

* replace innerText by textContent

Co-authored-by: JW <gitkraken@juriwolf.de>
Co-authored-by: Juri <info@juriwolf.de>
2022-04-26 10:28:42 -07:00

2.1 KiB

Compass Heading

Find which direction on a compass the @boardname@ is facing.

The @boardname@ measures the compass heading from 0 to 359 degrees with its magnetometer chip. Different numbers mean north, east, south, and west.

input.compassHeading();

Returns

  • a number from 0 to 359 degrees, which means the compass heading. If the compass isn't ready, it returns -1003.

Example

This program finds the compass heading and stores it in the degrees variable.

let degrees = input.compassHeading()

~hint

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.

~

Example: compass

This program finds the compass heading and then shows a letter that means whether the @boardname@ is facing north (N), south (S), east (E), or west (W).

let degrees = 0
basic.forever(() => {
    degrees = input.compassHeading()
    if (degrees < 45) {
        basic.showIcon(IconNames.ArrowNorth)
    } else if (degrees < 135) {
        basic.showIcon(IconNames.ArrowEast)
    } else if (degrees < 225) {
        basic.showIcon(IconNames.ArrowSouth)
    } else if (degrees < 315) {
        basic.showIcon(IconNames.ArrowWest)
    } else {
        basic.showIcon(IconNames.ArrowNorth)
    }
})

Calibration

Every time you start to use the compass (for example, if you have just turned the @boardname@ on), the @boardname@ will start a calibrate compass (adjust itself). The calibration step will ask you to draw a fill pattern on the screen by tilting the @boardname@.

If you are calibrating or using the compass near metal, it might confuse the @boardname@.

~ hint

Keep the calibration handy by running it when the user pressed A+B.

input.onButtonEvent(Button.AB, ButtonEvent.Click, () => {
    input.calibrateCompass();
})

~

See also

acceleration, calibrate compass