112 views|0 replies

650

Posts

8

Resources
The OP
 

【Follow me Season 2 Episode 1】Making a tumbler - showing different lighting effects during the movement of the tumbler [Copy link]

 This post was last edited by 29447945 on 2024-9-1 17:45

In the previous section, we used ultrasound to do proximity detection. In this section, we use motion sensors to create a tumbler effect.

The sensor used in this section is the LIS3DH; an ultra-small, high-performance, low-power 3-axis accelerometer produced by STMicroelectronics. It can detect and measure acceleration in the x, y, and z axes, that is, the movement and orientation changes of the device in these three directions.

Here the motion sensor obtains the xyz axis data, and then controls different LED colors according to the data;

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT



import time
from adafruit_circuitplayground import cp

cp.pixels.brightness = 0.2  # Adjust overall brightness as desired, between 0 and 1


def color_amount(accel_component):
    """Convert acceleration component (x, y, or z) to color amount (r, g, or b)"""
    standard_gravity = 9.81  # Acceleration (m/s) due to gravity at the earth’s surface
    accel_magnitude = abs(accel_component)  # Ignore the direction
    constrained_accel = min(accel_magnitude, standard_gravity)  # Constrain values
    normalized_accel = constrained_accel / standard_gravity  # Convert to 0–1
    return round(normalized_accel * 255)  # Convert to 0–255


def format_acceleration():
    return ", ".join(("{:>6.2f}".format(axis_value) for axis_value in acceleration))


def format_rgb():
    return ", ".join(("{:>3d}".format(rgb_amount) for rgb_amount in rgb_amounts))


def log_values():
    print("({}) ==> ({})".format(format_acceleration(), format_rgb()))


while True:
    acceleration = cp.acceleration
    rgb_amounts = [color_amount(axis_value) for axis_value in acceleration]
    cp.pixels.fill(rgb_amounts)
    log_values()
    time.sleep(0.1)

The data printed here is as follows:

The actual effect is as follows:

cd551fafb404987ddbbde287ce770953

This post is from DigiKey Technology Zone
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list