The micro:bit has a part called the accelerometer that can check how the micro:bit is moving. Watch this video to learn how the accelerometer works:
Rotation
Find how much the micro:bit is tilted in different directions.
input.rotation(Rotation.Roll);
Parameters
kind
means which direction you are checking:Rotation.Pitch
(up and down) orRotation.Roll
(left and right)
Returns
- a number that means how much the micro:bit is tilted in the direction you ask for. This is a value in degrees between
-180
to180
in either theRotation.Pitch
or theRotation.Roll
direction of rotation.
Example: micro:bit leveler
This program helps you move the micro:bit until it is level. When it is level, the micro:bit shows a smiley.
If you are running this program in a browser, you can tilt the micro:bit with your mouse.
let pitch = 0;
basic.forever(() => {
pitch = input.rotation(Rotation.Pitch);
let roll = input.rotation(Rotation.Roll);
if (Math.abs(pitch) < 10 && Math.abs(roll) < 10) {
basic.showLeds(`
. # . # .
. . . . .
. . . . .
# . . . #
. # # # .
`);
} else {
basic.showLeds(`
# . . . #
. # . # .
. . # . .
. # . # .
# . . . #
`);
}
});