File: tiltmusic.py

package info (click to toggle)
firmware-microbit-micropython 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,372 kB
  • sloc: ansic: 83,496; cpp: 27,665; python: 2,475; asm: 274; makefile: 227; sh: 25
file content (54 lines) | stat: -rw-r--r-- 1,342 bytes parent folder | download | duplicates (3)
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
# TiltMusic by Alex "Chozabu" P-B. September 2016.
#
# Tilt Y to change Pitch
# press A to turn sound on or off
# hold B and tilt X to change the note length
#
# A quick demo can be found at https://youtu.be/vvECQTDiWxQ
#
# This program has been placed into the public domain.

from microbit import *
import music

#A selection of sharp notes
notes = [233.08, 277.18, 311.13, 369.99, 415.30,
466.16, 554.37, 622.25, 739.99, 830.61, 932.33,
1108.73, 1244.51, 1479.98, 1661.22, 1864.66,
2217.46, 2489.02, 2959.96, 3322.44, 3729.31,
4434.92, 4978.03, 5919.91, 6644.88, 7458.62]

#note lengths
note_durations = [
 50, 100, 200, 400, 800
]
durationlen = len(note_durations)
notelen = len(notes)

duration = 100

play_music = True

while True:
    #get accelerometer readings
    xreading = abs(accelerometer.get_x())
    yreading = abs(accelerometer.get_y())
    
    #use a to toggle music
    if button_a.was_pressed():
        play_music = not play_music
    if not play_music:
        continue
    
    #get a note based on tilt
    note = xreading*.01
    pitch = notes[int(note)%notelen]
    
    #if b is pressed, alter the length based on tilt
    if button_b.is_pressed() == 1:
        #pitch *= .5
        duration = note_durations[int(yreading*0.01)%durationlen]
    
    #play our sound!
    music.pitch(int(pitch), duration)