|
4 | 4 | values to light up the NeoPixels based on those acceleration values."""
|
5 | 5 | from adafruit_circuitplayground.express import cpx
|
6 | 6 |
|
| 7 | +cpx.pixels.brightness = 0.2 # Adjust as desired, between 0 and 1 |
| 8 | + |
7 | 9 | # Main loop gets x, y and z axis acceleration, prints the values, and turns on
|
8 | 10 | # red, green and blue, at levels related to the x, y and z values.
|
9 | 11 | while True:
|
|
13 | 15 | cpx.pixels.fill((0, 0, 0))
|
14 | 16 | continue
|
15 | 17 | else:
|
16 |
| - x, y, z = cpx.acceleration |
17 |
| - print((x, y, z)) |
18 |
| - cpx.pixels.fill((abs(int(x)), abs(int(y)), abs(int(z)))) |
| 18 | + def color_amount(accel_component): |
| 19 | + """Convert acceleration component (x, y, or z) to color amount (r, g, or b)""" |
| 20 | + standard_gravity = 9.81 # Acceleration (m/s²) due to gravity at the earth’s surface |
| 21 | + accel_magnitude = abs(accel_component) # Ignore the direction |
| 22 | + constrained_accel = min(accel_magnitude, standard_gravity) # Constrain values |
| 23 | + normalized_accel = constrained_accel / standard_gravity # Convert to 0–1 |
| 24 | + return round(normalized_accel * 255) # Convert to 0–255 |
| 25 | + |
| 26 | + # Get the acceleration, reducing the precision to match our needs and for display |
| 27 | + acceleration = [round(a, 2) for a in cpx.acceleration] |
| 28 | + # Call color_amount for each acceleration part |
| 29 | + rgb_amounts = tuple(map(color_amount, acceleration)) |
| 30 | + print(acceleration, '->', rgb_amounts) |
| 31 | + cpx.pixels.fill(rgb_amounts) |
0 commit comments