pxt-calliope/docs/reference/input/rotation.md

62 lines
1.4 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Rotation
Find how much the micro:bit is tilted in different directions.
2016-03-26 00:47:20 +01:00
```sig
input.rotation(Rotation.Roll);
```
## ~hint
The BBC micro:bit has a part called the **accelerometer** that can
check how the micro:bit is moving.
## ~
2016-03-26 00:47:20 +01:00
### Parameters
* which direction you are checking: `Rotation.Pitch` (up and down) or `Rotation.Roll` (left and right)
2016-03-26 00:47:20 +01:00
### Returns
* a [number](/reference/types/number) that means how much the microbit is tilted in the direction you say, from `0` to `360` degrees
2016-03-26 00:47:20 +01:00
### Example: micro:bit leveler
2016-03-26 00:47:20 +01:00
This program helps you move the BBC micro:bit until it is level. When
it is level, the micro:bit shows a smiley.
2016-03-26 00:47:20 +01:00
If you are running this program in a browser, you can tilt the
micro:bit with your mouse.
```blocks
let pitch = 0;
2016-03-26 00:47:20 +01:00
basic.forever(() => {
pitch = input.rotation(Rotation.Pitch);
let roll = input.rotation(Rotation.Roll);
2016-03-26 00:47:20 +01:00
if (Math.abs(pitch) < 10 && Math.abs(roll) < 10) {
basic.showLeds(`
. # . # .
. . . . .
. . . . .
# . . . #
. # # # .
`);
2016-03-26 00:47:20 +01:00
} else {
basic.showLeds(`
# . . . #
. # . # .
. . # . .
. # . # .
# . . . #
`);
}
});
2016-03-26 00:47:20 +01:00
```
### See also
2016-04-13 17:27:45 +02:00
[acceleration](/reference/input/acceleration), [compass-heading](/reference/input/compass-heading)
2016-03-26 00:47:20 +01:00