Compare commits

..

7 commits

Author SHA1 Message Date
Kattni
d16c916429
Merge pull request #65 from dcbriccetti/accel-maps-pixels
Create an acceleration to LEDs example
2019-08-08 16:35:09 -04:00
Dave Briccetti
d0d481ed7e Make Adafruit-requested changes 2019-08-08 13:22:59 -07:00
Dave Briccetti
5f494e00db Create an acceleration to LEDs example 2019-08-07 19:24:24 -07:00
Kattni
d675c99134
Merge pull request #63 from dcbriccetti/simplify-and-improve
Remove unneeded variables and conditions
2019-08-07 15:59:57 -04:00
Dave Briccetti
178406039b Remove unneeded variables and conditions 2019-08-07 12:57:16 -07:00
Scott Shawcroft
965fdd30c3
Merge pull request #64 from hexthat/patch-4
Update to support CP versions 3.0.0 to 5.0.0
2019-08-02 11:53:14 -07:00
hexthat
f8789a496f
Update to support CP versions 3.0.0 to 5.0.0 2019-08-01 07:42:11 -07:00
2 changed files with 49 additions and 7 deletions

View file

@ -0,0 +1,48 @@
"""Maps acceleration (tilt) to Neopixel colors.
x, y, and z acceleration components map to red, green and blue,
respectively.
When the CPX is level, the lights are blue because there is no acceleration
on x or y, but on z, gravity pulls at 9.81 meters per second per second (m/).
When banking, the vertical (z) axis is no longer directly aligned with gravity,
so the blue decreases, and red increases because gravity is now pulling more
along the x axis. Similarly, when changing the pitch from level, we see blue change
to green.
This video walks you through the code: https://youtu.be/eNpPLbYx-iA
"""
import time
from adafruit_circuitplayground.express import cpx
cpx.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 earths 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 01
return round(normalized_accel * 255) # Convert to 0255
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 = cpx.acceleration
rgb_amounts = [color_amount(axis_value) for axis_value in acceleration]
cpx.pixels.fill(rgb_amounts)
log_values()
time.sleep(0.1)

View file

@ -18,10 +18,4 @@ while True:
B = 0 B = 0
x, y, z = cpx.acceleration x, y, z = cpx.acceleration
print((x, y, z)) print((x, y, z))
if x: cpx.pixels.fill(((R + abs(int(x))), (G + abs(int(y))), (B + abs(int(z)))))
R = R + abs(int(x))
if y:
G = G + abs(int(y))
if z:
B = B + abs(int(z))
cpx.pixels.fill((R, G, B))