We will use the accelerometer on the Circuit Playground to measure the slouch angle. A more in depth overview of the accelerometer can be found in the How Tall Is It? Guide. If you look at the Circuit Playground sideways, the coordinate system of the accelerometer is defined as shown in the figure below.
In the figure below, the Circuit Playground is tilted away from the vertical by an amount we are calling currentAngle. The big green arrow is the acceleration due to gravity. It is always present and always points vertically. (see How Tall Is It? Guide for more info)
A portion of the green arrow will be sensed by the X axis and returned by
motionX(). Similarly, a portion will be sensed by the Z axis as returned by
motionZ(). All together, these values form a right triangle as shown in the figure below. This let's us use the basic right angle functions shown to compute the value for currentAngle.
Note that the portion of gravity sensed by the Z axis is in the negative direction. Therefore, a - sign shows up in the equations.
Two ways are shown above for computing currentAngle. One uses both the
motionZ() values in the arctangent function,
atan(). The second one only uses the
motionZ() value in the arcsine function,
asin(). GRAVITY is a constant which, on Earth at sea level, is about 9.80665 m/s2.
So which equation should we use? We'll let's take a look at how they behave.