Skip to content

Commit 825354f

Browse files
authored
Add Music Box code in CircuitPython
1 parent 2205e1e commit 825354f

File tree

1 file changed

+66
-0
lines changed
  • Crickits/Music_Box_with_Crickit

1 file changed

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

0 commit comments

Comments
 (0)