The idea of the slouch detector is simple. If the current angle sensed by the Circuit Playground (current_angle) exceeds a preset value (SLOUCH_ANGLE), then we are slouching and should sound an alarm. Here's the code.

# Circuit Playground Slouch Detector v1
#
# Compute current angle using accelerometer and compare
# to preset slouch angle. Sound alarm if slouching.
#
# Author: Carter Nelson
# MIT License (https://opensource.org/licenses/MIT)
import time
import math
from adafruit_circuitplayground.express import cpx

SLOUCH_ANGLE    = 10.0
SLOUCH_TIME     = 3
GRAVITY         = 9.80665

# Loop forever
while True:
    # Compute current angle
    current_angle = math.asin(-cpx.acceleration[2] / GRAVITY)
    current_angle = math.degrees(current_angle)
    
    # Check if slouching
    if current_angle > SLOUCH_ANGLE:
        cpx.play_tone(800, 0.5)

This guide was first published on Dec 21, 2016. It was last updated on Dec 21, 2016.

This page (Slouch Detector v1) was last updated on Nov 27, 2021.

Text editor powered by tinymce.