Skip to content

Commit 2f429c5

Browse files
committed
Cleaned up, documented
1 parent aff12ae commit 2f429c5

File tree

1 file changed

+27
-25
lines changed
  • Circuit_Playground_Brake_Light

1 file changed

+27
-25
lines changed
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
1-
# pylint:disable=invalid-name,import-error,missing-module-docstring
2-
3-
# Great pdf on moving average filters:
4-
# https://www.analog.com/media/en/technical-documentation/dsp-book/dsp_book_Ch15.pdf
51
import time
62
import math
73
from adafruit_circuitplayground import cp
84

95
brightness = 0
10-
l = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
11-
l1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13-
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]
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]
1612

1713
i = 0
14+
cp.pixels.fill((255, 0, 0))
15+
1816
sleep = False
19-
decel = 0
17+
2018
while True:
2119
x, y, z = cp.acceleration
2220

23-
# moving average n=10, not super smooth, but not terrible either
24-
l.append(z)
25-
l.pop(0)
26-
avg = sum(l)/10
21+
# 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
2725

2826
# moving average n=50, very smooth
29-
l1.append(z)
30-
l1.pop(0)
31-
avg1 = sum(l1)/50
27+
l50.append(z)
28+
l50.pop(0)
29+
avg50 = sum(l50)/50
3230

33-
if avg - avg1 > 1:
31+
# If the difference between the moving average of the last 10 points and the moving average of
32+
# the last 50 points is greater than 1 m/s^2, this is true
33+
if avg10 - avg50 > 1:
3434
if i > 3:
35+
# Detects shake. Due to the very low shake threshold, this alone would have very low
36+
# specificity. This was mitigated by having it only run when the acceleration
37+
# difference is greater than 1 m/s^2 at least 3 times in a row.
3538
if not cp.shake(shake_threshold=10):
36-
cp.pixels.fill((0, 0, 255))
37-
cp.pixels.brightness = 0.25
39+
cp.pixels.brightness = 1
3840
start = time.monotonic()
3941
sleep = True
4042
i += 1
4143

42-
# sleep variable is for short circuiting
44+
# sleep variable is for short circuiting. Not really necessary, but it makes things run faster
4345
elif not sleep or time.monotonic() - start > 0.4:
44-
cp.pixels.fill((255, 0, 0))
45-
cp.pixels.brightness = abs(math.sin(brightness)) * 0.25
46+
# Sine wave used for the color breathing effect.
47+
# Max brightness can be adjusted with the coefficient.
48+
cp.pixels.brightness = abs(math.sin(brightness)) * 0.5
4649
brightness += 0.05
4750
i = 0
4851
sleep = False
49-
decel = 0
5052

5153
time.sleep(0.02)

0 commit comments

Comments
 (0)