【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
|