Skip to content

Commit 203ecce

Browse files
committed
Added auto brake light code.py
1 parent b5114e5 commit 203ecce

File tree

1 file changed

+51
-0
lines changed
  • Circuit_Playground_Brake_Light

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import time
6+
import math
7+
from adafruit_circuitplayground import cp
8+
9+
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]
16+
17+
i = 0
18+
sleep = False
19+
decel = 0
20+
while True:
21+
x, y, z = cp.acceleration
22+
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
27+
28+
# moving average n=50, very smooth
29+
l1.append(z)
30+
l1.pop(0)
31+
avg1 = sum(l1)/50
32+
33+
if avg - avg1 > 1:
34+
if i > 3:
35+
if not cp.shake(shake_threshold=10):
36+
cp.pixels.fill((0, 0, 255))
37+
cp.pixels.brightness = 0.25
38+
start = time.monotonic()
39+
sleep = True
40+
i +=1
41+
42+
# sleep variable is for short circuiting
43+
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+
brightness += 0.05
47+
i = 0
48+
sleep = False
49+
decel = 0
50+
51+
time.sleep(0.02)

0 commit comments

Comments
 (0)