├── readme.txt ├── tiltmusic2.py ├── otiltmusic.py └── tiltmusic.py /readme.txt: -------------------------------------------------------------------------------- 1 | ====TiltMusic==== 2 | 3 | This is a sample program or two some basic features of the BBC MicroBit. 4 | 5 | attach a speaker, flash one of the scripts then tilt and press buttons for "Music"! 6 | 7 | -------------------------------------------------------------------------------- /tiltmusic2.py: -------------------------------------------------------------------------------- 1 | # TiltMusic by Alex "Chozabu" P-B. September 2016. 2 | # 3 | # Tilt Y to change Pitch 4 | # hold A to play sound 5 | # hold B to play notes 6 | # 7 | # A quick demo can be found at https://youtu.be/vvECQTDiWxQ 8 | # 9 | # This program has been placed into the public domain. 10 | 11 | from microbit import * 12 | import music 13 | 14 | #A selection of sharp notes 15 | notes = [233.08, 277.18, 311.13, 369.99, 415.30, 16 | 466.16, 554.37, 622.25, 739.99, 830.61, 932.33, 17 | 1108.73, 1244.51, 1479.98, 1661.22, 1864.66, 18 | 2217.46, 2489.02, 2959.96, 3322.44, 3729.31, 19 | 4434.92, 4978.03, 5919.91, 6644.88, 7458.62] 20 | 21 | notelen = len(notes) 22 | 23 | while True: 24 | #get accelerometer readings 25 | xreading = abs(accelerometer.get_x()) 26 | yreading = abs(accelerometer.get_y()) 27 | 28 | #get a note based on tilt 29 | note = xreading*.01 30 | pitch = notes[int(note)%notelen] 31 | 32 | #use a to toggle music 33 | if button_a.is_pressed(): 34 | music.pitch(int(pitch), -1) 35 | elif button_a.was_pressed(): 36 | music.pitch(0, 1) 37 | continue 38 | 39 | 40 | if button_b.is_pressed(): 41 | music.pitch(int(pitch), 200) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /otiltmusic.py: -------------------------------------------------------------------------------- 1 | # TiltMusic by Alex "Chozabu" P-B. September 2016. 2 | # 3 | # Tilt Y to change Pitch 4 | # press A to turn sound on or off 5 | # hold B and tilt X to change the note length 6 | # 7 | # A quick demo can be found at https://youtu.be/vvECQTDiWxQ 8 | # 9 | # This program has been placed into the public domain. 10 | 11 | from microbit import * 12 | import music 13 | 14 | #A selection of sharp notes 15 | notes = [233.08, 277.18, 311.13, 369.99, 415.30, 16 | 466.16, 554.37, 622.25, 739.99, 830.61, 932.33, 17 | 1108.73, 1244.51, 1479.98, 1661.22, 1864.66, 18 | 2217.46, 2489.02, 2959.96, 3322.44, 3729.31, 19 | 4434.92, 4978.03, 5919.91, 6644.88, 7458.62] 20 | 21 | #note lengths 22 | note_durations = [ 23 | 50, 100, 200, 400, 800 24 | ] 25 | durationlen = len(note_durations) 26 | notelen = len(notes) 27 | 28 | duration = 100 29 | 30 | play_music = True 31 | 32 | while True: 33 | #get accelerometer readings 34 | xreading = abs(accelerometer.get_x()) 35 | yreading = abs(accelerometer.get_y()) 36 | 37 | #use a to toggle music 38 | if button_a.was_pressed(): 39 | play_music = not play_music 40 | if not play_music: 41 | continue 42 | 43 | #get a note based on tilt 44 | note = xreading*.01 45 | pitch = notes[int(note)%notelen] 46 | 47 | #if b is pressed, alter the length based on tilt 48 | if button_b.is_pressed() == 1: 49 | #pitch *= .5 50 | duration = note_durations[int(yreading*0.01)%durationlen] 51 | 52 | #play our sound! 53 | music.pitch(int(pitch), duration) 54 | 55 | -------------------------------------------------------------------------------- /tiltmusic.py: -------------------------------------------------------------------------------- 1 | # TiltMusic by Alex "Chozabu" P-B. September 2016. 2 | # 3 | # Tilt Y to change Pitch 4 | # press A or B to change note length, and turn sound on or off 5 | # 6 | # A quick demo can be found at https://youtu.be/vvECQTDiWxQ 7 | # 8 | # This program has been placed into the public domain. 9 | 10 | from microbit import * 11 | import music 12 | 13 | #A selection of sharp notes 14 | notes = [233.08, 277.18, 311.13, 369.99, 415.30, 15 | 466.16, 554.37, 622.25, 739.99, 830.61, 932.33, 16 | 1108.73, 1244.51, 1479.98, 1661.22, 1864.66, 17 | 2217.46, 2489.02, 2959.96, 3322.44, 3729.31, 18 | 4434.92, 4978.03, 5919.91, 6644.88, 7458.62] 19 | 20 | #note lengths 21 | note_durations = [ 22 | 0, 50, 100, 200, 400, 800 23 | ] 24 | durationlen = len(note_durations) 25 | notelen = len(notes) 26 | 27 | duration_index = 2 28 | 29 | duration = 100 30 | 31 | play_music = True 32 | 33 | while True: 34 | #get accelerometer readings 35 | xreading = abs(accelerometer.get_x()) 36 | yreading = abs(accelerometer.get_y()) 37 | 38 | #use a to toggle music 39 | if button_a.was_pressed(): 40 | duration_index +=1 41 | if button_b.was_pressed() == 1: 42 | duration_index -=1 43 | duration_index = duration_index%durationlen 44 | duration = note_durations[duration_index] 45 | if not duration: 46 | continue 47 | 48 | #get a note based on tilt 49 | note = xreading*.01 50 | pitch = notes[int(note)%notelen] 51 | 52 | #if b is pressed, alter the length based on tilt 53 | 54 | #play our sound! 55 | music.pitch(int(pitch), duration) 56 | 57 | --------------------------------------------------------------------------------