Microsoft MakeCode has excellent support for using the accelerometer on the Circuit Playground Express.
If you are new to using MakeCode and Circuit Playground Express, see this guide to learn how to get started.
MakeCode blocks which use the Circuit Playground Express accelerometer are in the INPUT code block group.
set accelerometer range block adjusts the range at which the accelerometer will detect. You can think of it as a sensitivity adjustment. If you think the measurements will be small, 1 g should be fine. But for violent bumps, maybe 2, 4, or 8g may be better. Often this block is placed in an
on start block to set the sensitivity before the main
rotation blocks read the accelerometer and provide a value you can use in calculations or decisions. the acceleration will read either x, y, or z so reading all three will take three
set variable blocks. The value returned is in milli-gs, where one gee is 9.8 m/s2, the pull of the earth. The
acceleration block can also return a
strength value which is the acceleration value from all three directions.
rotation will read either the
roll values. Pitch is up or down, roll is left or right and is in degrees (0 to 360).
on shake is actually a number of readings that can actuate some MakeCode. All the actions you can choose from are:
shake: shake the board
logo up: the logo is facing up
logo down: the logo is facing down
screen up: the screen side is up
screen down: the screen side is down
tilt left: the board is tilted to the left
tilt right: the board is tilted to the right
free fall: the board is falling for a distance
3g: acceleration force of 3 g
6g: acceleration force of 6 g
This is very handy as you don't have to continually read the raw values, compute something, then take action, MakeCode does this for the user if desired.