-
Notifications
You must be signed in to change notification settings - Fork 776
/
Copy pathcode.py
66 lines (54 loc) · 1.71 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# SPDX-FileCopyrightText: 2019 Dano Wall for Adafruit Industries
# SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# Music Box code in CircuitPython - Dano Wall and Anne Barela
# Revised by Ladyada 2019-01-16
from adafruit_crickit import crickit
from analogio import AnalogIn
from rainbowio import colorwheel
import neopixel
import audioio
import audiocore
import board
AUDIO_FILENAME = 'fur-elise.wav'
# Audio output
cpx_audio = audioio.AudioOut(board.A0)
audio = audiocore.WaveFile(open(AUDIO_FILENAME, "rb"))
# Rotating dancer
dancer = crickit.servo_2
dancer.angle = 0
MAX_SERVO_ANGLE = 160
move_direction = 1
# neopixels!
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1)
pixels.fill((0, 0, 0))
# light sensor
light = AnalogIn(board.LIGHT)
def rainbow(value):
for i in range(10):
pixels[i] = colorwheel((value * i) & 255)
while True:
# turn off LEDs so we can tell if its dark out!
pixels.brightness = 0
# read light level
light_level = light.value
# turn LEDs back on
pixels.brightness = 1
# Turn things off if light level < value, its dark
if light_level < 2000:
pixels.fill((0, 0, 0))
cpx_audio.stop()
else:
if not cpx_audio.playing:
# Start playing the song again
cpx_audio.play(audio)
# calculate servo rotation
if dancer.angle <= 0:
move_direction = 1
if dancer.angle > MAX_SERVO_ANGLE:
move_direction = -1
# Move servo one degree forward or backward.
rainbow(int(dancer.angle * 255/MAX_SERVO_ANGLE))
dancer.angle += move_direction