-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmusic.py
42 lines (32 loc) · 1.13 KB
/
music.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
from microbit import *
import music
TOLERANCE = 3000
def get_accelerometer_total():
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
return x + y + z
def wait_for_shake():
shaken = False
last = get_accelerometer_total()
while not shaken:
this = get_accelerometer_total()
diff = last - this
if diff < 0:
diff = diff * -1
if diff > TOLERANCE:
shaken = True
last = this
sleep(50)
twinkle_twinkle = ['c4:4', 'c', 'g', 'g', 'a', 'a', 'g:8',
'f:4', 'f', 'e', 'e', 'd', 'd', 'c:8',
'g:4', 'g', 'f', 'f', 'e', 'e', 'd:8',
'g:4', 'g', 'f', 'f', 'e', 'e', 'd:8',
'c:4', 'c', 'g', 'g', 'a', 'a', 'g:8',
'f:4', 'f', 'e', 'e', 'd', 'd', 'c:8']
list_of_songs = [twinkle_twinkle, music.FUNERAL, music.DADADADUM, music.BIRTHDAY, music.ENTERTAINER, music.ODE,
music.PRELUDE, music.BLUES, music.PYTHON, music.NYAN]
while True:
wait_for_shake()
music.stop()
music.play(list_of_songs[random(len(list_of_songs))], wait=False)