|
3 | 3 | from adafruit_circuitplayground import cp
|
4 | 4 |
|
5 | 5 | 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 |
14 | 20 | cp.pixels.fill((255, 0, 0))
|
15 | 21 |
|
16 | 22 | light_on = False
|
|
19 | 25 | x, y, z = cp.acceleration
|
20 | 26 |
|
21 | 27 | # 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 |
25 | 31 |
|
26 | 32 | # 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 |
30 | 36 |
|
31 | 37 | # If the difference between the moving average of the last 10 points and the moving average of
|
32 | 38 | # the last 50 points is greater than 1 m/s^2, this is true
|
33 | 39 | 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 |
35 | 41 | # Detects shake. Due to the very low shake threshold, this alone would have very low
|
36 | 42 | # specificity. This was mitigated by having it only run when the acceleration
|
37 | 43 | # difference is greater than 1 m/s^2 at least 3 times in a row.
|
38 | 44 | 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 |
39 | 46 | cp.pixels.brightness = 1
|
40 | 47 | start = time.monotonic()
|
41 | 48 | light_on = True
|
42 |
| - i += 1 |
| 49 | + consecutive_triggers += 1 # increase it whether or not the light is turned on |
43 | 50 |
|
44 | 51 | # light_on variable is for short circuiting. Not really necessary, just makes things run faster
|
45 | 52 | elif not light_on or time.monotonic() - start > 0.4:
|
46 | 53 | # Sine wave used for the color breathing effect.
|
47 | 54 | # Max brightness can be adjusted with the coefficient.
|
48 | 55 | cp.pixels.brightness = abs(math.sin(brightness)) * 0.5
|
49 | 56 | brightness += 0.05
|
50 |
| - i = 0 |
| 57 | + consecutive_triggers = 0 |
51 | 58 | light_on = False
|
52 | 59 |
|
53 | 60 | time.sleep(0.02)
|
0 commit comments