|
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 |
5 | 1 | import time |
6 | 2 | import math |
7 | 3 | from adafruit_circuitplayground import cp |
8 | 4 |
|
9 | 5 | 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] |
16 | 12 |
|
17 | 13 | i = 0 |
| 14 | +cp.pixels.fill((255, 0, 0)) |
| 15 | + |
18 | 16 | sleep = False |
19 | | -decel = 0 |
| 17 | + |
20 | 18 | while True: |
21 | 19 | x, y, z = cp.acceleration |
22 | 20 |
|
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 |
27 | 25 |
|
28 | 26 | # 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 |
32 | 30 |
|
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: |
34 | 34 | 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. |
35 | 38 | 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 |
38 | 40 | start = time.monotonic() |
39 | 41 | sleep = True |
40 | 42 | i += 1 |
41 | 43 |
|
42 | | - # sleep variable is for short circuiting |
| 44 | + # sleep variable is for short circuiting. Not really necessary, but it makes things run faster |
43 | 45 | 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 |
46 | 49 | brightness += 0.05 |
47 | 50 | i = 0 |
48 | 51 | sleep = False |
49 | | - decel = 0 |
50 | 52 |
|
51 | 53 | time.sleep(0.02) |
0 commit comments