In this exercise, we are going to bring inputs and outputs together to make a musical instrument. Microbit can play music using the music.play function and can get input from buttons or from the touch inputs on the edge connector. Wire a speaker or a pair of headphones between pin 0 and and on the edge of the microbit before running.
from microbit import *
import music
while True:
if pin1.is_touched() and pin2.is_touched():
tune = ["C4:4"]
music.play(tune)
elif pin1.is_touched():
tune = ["G4:4"]
music.play(tune)
elif pin2.is_touched():
tune = ["A4:4"]
music.play(tune)
elif button_a.was_pressed():
tune = ["F4:4"]
music.play(tune)
elif button_b.was_pressed():
tune = ["E4:4"]
music.play(tune)
sleep(100)