From fe6ce3a9bbcbf9057dc5c794afcbae2a1d10e640 Mon Sep 17 00:00:00 2001 From: dherrada Date: Fri, 12 Jun 2020 16:28:53 -0400 Subject: [PATCH] better names, more documentation --- Circuit_Playground_Brake_Light/code.py | 39 +++++++++++++++----------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/Circuit_Playground_Brake_Light/code.py b/Circuit_Playground_Brake_Light/code.py index 288133e62..841604eef 100644 --- a/Circuit_Playground_Brake_Light/code.py +++ b/Circuit_Playground_Brake_Light/code.py @@ -3,14 +3,20 @@ import math from adafruit_circuitplayground import cp 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)) light_on = False @@ -19,27 +25,28 @@ while True: x, y, z = cp.acceleration # moving average n=10, not super smooth, but it substantially lowers the amount of noise - l10.append(z) - l10.pop(0) - avg10 = sum(l10)/10 + last10.append(z) + last10.pop(0) + avg10 = sum(last10)/10 # moving average n=50, very smooth - l50.append(z) - l50.pop(0) - avg50 = sum(l50)/50 + last50.append(z) + last50.pop(0) + avg50 = sum(last50)/50 # 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 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 # 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. 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 start = time.monotonic() 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 elif not light_on or time.monotonic() - start > 0.4: @@ -47,7 +54,7 @@ while True: # Max brightness can be adjusted with the coefficient. cp.pixels.brightness = abs(math.sin(brightness)) * 0.5 brightness += 0.05 - i = 0 + consecutive_triggers = 0 light_on = False time.sleep(0.02)