Skip to content

Commit 7a8df7c

Browse files
committed
personal menorah project
1 parent c09aff5 commit 7a8df7c

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

WS2801_LED_Menorah/code.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""Jack-o'-Lantern flame example Adafruit Circuit Playground Express"""
5+
import time
6+
import math
7+
import random
8+
import board
9+
import adafruit_ws2801
10+
from digitalio import DigitalInOut, Direction, Pull
11+
12+
# button setup
13+
button = DigitalInOut(board.A2)
14+
button.direction = Direction.INPUT
15+
button.pull = Pull.UP
16+
17+
# pixel setup
18+
pixel_num = 9
19+
pixel_offset = 2
20+
pixels = adafruit_ws2801.WS2801(board.SDA1, board.SCL1, pixel_num+pixel_offset, brightness=1, auto_write=False)
21+
22+
pixel_prev = [128] * len(pixels)
23+
24+
lit_candles = None
25+
night = 1
26+
27+
def split(first, second, offset):
28+
"""
29+
Subdivide a brightness range, introducing a random offset in middle,
30+
then call recursively with smaller offsets along the way.
31+
@param1 first: Initial brightness value.
32+
@param1 second: Ending brightness value.
33+
@param1 offset: Midpoint offset range is +/- this amount max.
34+
"""
35+
if offset != 0:
36+
mid = ((first + second + 1) / 2 + random.randint(-offset, offset))
37+
offset = int(offset / 2)
38+
split(first, mid, offset)
39+
split(mid, second, offset)
40+
else:
41+
level = math.pow(first / 255.0, 2.7) * 255.0 + 0.5
42+
return level
43+
44+
while True:
45+
if not button.value:
46+
while not button.value:
47+
time.sleep(0.01) # debounce
48+
night += 1 # next night
49+
if night == 9: # wrap around
50+
night = 1
51+
lit_candles = None # reset the lights
52+
if not lit_candles:
53+
print("Current night: ", night)
54+
lit_candles = [True] * night + [False] * (8-night)
55+
lit_candles.insert(4, True) # shamash always on
56+
print(lit_candles)
57+
58+
# animate candles
59+
for p in range(len(pixels)-pixel_offset):
60+
if not lit_candles[p]:
61+
pixels[p+pixel_offset] = 0
62+
continue
63+
if p == 4:
64+
level = random.randint(128, 255)
65+
else:
66+
level = random.randint(64, 191)
67+
68+
color = split(pixel_prev[p], level, 32)
69+
pixels[p+pixel_offset] = ((int(level), int(level / 8), int(level / 48)))
70+
pixel_prev[p] = level
71+
pixels.show()

0 commit comments

Comments
 (0)