better names, more documentation

This commit is contained in:
dherrada 2020-06-12 16:28:53 -04:00
parent cc584070b8
commit fe6ce3a9bb
No known key found for this signature in database
GPG key ID: CE2ADBAB8775CE81

View file

@ -3,14 +3,20 @@ import math
from adafruit_circuitplayground import cp from adafruit_circuitplayground import cp
brightness = 0 brightness = 0
l10 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
l50 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
i = 0 # List that holds the last 10 z-axis acceleration values read from the accelerometer.
# Used for the n=10 moving average
last10 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# List that holds the last 50 z-axis acceleration values read from the accelerometer.
# Used for the n=50 moving average
last50 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
consecutive_triggers = 0
cp.pixels.fill((255, 0, 0)) cp.pixels.fill((255, 0, 0))
light_on = False light_on = False
@ -19,27 +25,28 @@ while True:
x, y, z = cp.acceleration x, y, z = cp.acceleration
# moving average n=10, not super smooth, but it substantially lowers the amount of noise # moving average n=10, not super smooth, but it substantially lowers the amount of noise
l10.append(z) last10.append(z)
l10.pop(0) last10.pop(0)
avg10 = sum(l10)/10 avg10 = sum(last10)/10
# moving average n=50, very smooth # moving average n=50, very smooth
l50.append(z) last50.append(z)
l50.pop(0) last50.pop(0)
avg50 = sum(l50)/50 avg50 = sum(last50)/50
# If the difference between the moving average of the last 10 points and the moving average of # If the difference between the moving average of the last 10 points and the moving average of
# the last 50 points is greater than 1 m/s^2, this is true # the last 50 points is greater than 1 m/s^2, this is true
if avg10 - avg50 > 1: if avg10 - avg50 > 1:
if i > 3: if consecutive_triggers > 3: # Is true when avg10-avg50 > 1m/s^2 at least 3 times in a row
# Detects shake. Due to the very low shake threshold, this alone would have very low # Detects shake. Due to the very low shake threshold, this alone would have very low
# specificity. This was mitigated by having it only run when the acceleration # specificity. This was mitigated by having it only run when the acceleration
# difference is greater than 1 m/s^2 at least 3 times in a row. # difference is greater than 1 m/s^2 at least 3 times in a row.
if not cp.shake(shake_threshold=10): if not cp.shake(shake_threshold=10):
# Set brightness to max, timestamp when it was set to max, and set light_on to true
cp.pixels.brightness = 1 cp.pixels.brightness = 1
start = time.monotonic() start = time.monotonic()
light_on = True light_on = True
i += 1 consecutive_triggers += 1 # increase it whether or not the light is turned on
# light_on variable is for short circuiting. Not really necessary, just makes things run faster # light_on variable is for short circuiting. Not really necessary, just makes things run faster
elif not light_on or time.monotonic() - start > 0.4: elif not light_on or time.monotonic() - start > 0.4:
@ -47,7 +54,7 @@ while True:
# Max brightness can be adjusted with the coefficient. # Max brightness can be adjusted with the coefficient.
cp.pixels.brightness = abs(math.sin(brightness)) * 0.5 cp.pixels.brightness = abs(math.sin(brightness)) * 0.5
brightness += 0.05 brightness += 0.05
i = 0 consecutive_triggers = 0
light_on = False light_on = False
time.sleep(0.02) time.sleep(0.02)