Skip to content

Commit fe6ce3a

Browse files
committed
better names, more documentation
1 parent cc58407 commit fe6ce3a

File tree

1 file changed

+24
-17
lines changed
  • Circuit_Playground_Brake_Light

1 file changed

+24
-17
lines changed

Circuit_Playground_Brake_Light/code.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
from adafruit_circuitplayground import cp
44

55
brightness = 0
6-
l10 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
7-
l50 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
12-
13-
i = 0
6+
7+
# List that holds the last 10 z-axis acceleration values read from the accelerometer.
8+
# Used for the n=10 moving average
9+
last10 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
10+
11+
# List that holds the last 50 z-axis acceleration values read from the accelerometer.
12+
# Used for the n=50 moving average
13+
last50 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
18+
19+
consecutive_triggers = 0
1420
cp.pixels.fill((255, 0, 0))
1521

1622
light_on = False
@@ -19,35 +25,36 @@
1925
x, y, z = cp.acceleration
2026

2127
# moving average n=10, not super smooth, but it substantially lowers the amount of noise
22-
l10.append(z)
23-
l10.pop(0)
24-
avg10 = sum(l10)/10
28+
last10.append(z)
29+
last10.pop(0)
30+
avg10 = sum(last10)/10
2531

2632
# moving average n=50, very smooth
27-
l50.append(z)
28-
l50.pop(0)
29-
avg50 = sum(l50)/50
33+
last50.append(z)
34+
last50.pop(0)
35+
avg50 = sum(last50)/50
3036

3137
# If the difference between the moving average of the last 10 points and the moving average of
3238
# the last 50 points is greater than 1 m/s^2, this is true
3339
if avg10 - avg50 > 1:
34-
if i > 3:
40+
if consecutive_triggers > 3: # Is true when avg10-avg50 > 1m/s^2 at least 3 times in a row
3541
# Detects shake. Due to the very low shake threshold, this alone would have very low
3642
# specificity. This was mitigated by having it only run when the acceleration
3743
# difference is greater than 1 m/s^2 at least 3 times in a row.
3844
if not cp.shake(shake_threshold=10):
45+
# Set brightness to max, timestamp when it was set to max, and set light_on to true
3946
cp.pixels.brightness = 1
4047
start = time.monotonic()
4148
light_on = True
42-
i += 1
49+
consecutive_triggers += 1 # increase it whether or not the light is turned on
4350

4451
# light_on variable is for short circuiting. Not really necessary, just makes things run faster
4552
elif not light_on or time.monotonic() - start > 0.4:
4653
# Sine wave used for the color breathing effect.
4754
# Max brightness can be adjusted with the coefficient.
4855
cp.pixels.brightness = abs(math.sin(brightness)) * 0.5
4956
brightness += 0.05
50-
i = 0
57+
consecutive_triggers = 0
5158
light_on = False
5259

5360
time.sleep(0.02)

0 commit comments

Comments
 (0)