Micro:bit comes with an interesting feature is the ability to generate music playback, today do a simple and practical case, with Micro:bit play Happy Birthday song.
algorithm:
Press the button A to show Happy birthday
play D
play D
play E
play D
play G
play F
rest
In order to play music, a buzzer or headset that works 3V can be connected to the Micro:bit Gnd and Pine0 feet, such as
blocky program is as follows
micropython program script is as follows:
from microbit Import *
import music
tune = [" E4:4 "," E4:4 "," E4:8 "," E4:4 "," E4:4 "," E4:8 "," E4:4 "," G4:4 "," C4:4 "," D4:4 "," E4:8 "," F4:4 "," F4:4 "," F4:4 "," F4:4 "," F4:4 ",
" E4:4 "," E4:4 "," E4:2 "," E4:2 "," E4:4 "," D4:4 "," D4:4 "," E4:4 "," D4:8 "," G4:8 "," E4:4 "," E4:4 ", "E4:8", "E4:4", "E4:4", "E4:8",
" E4:4 "," G4:4 "," C4:4 "," D4:4 "," E4:8 "," F4:4 "," F4:4 "," F4:4 "," F4:4 "," F4:4 "," E4:4 "," E4:4 ", "E4:2", "E4:2", "G4:4", "G4:4",
" F4:4 "," D4:4 "," C4:8 "]
while True:
if button_a.is_pressed ():
music.play (tune)
download to play.
original reference
https:// mp.weixin.qq.com/s?__biz=mzu0mtg0odizna==&mid=2247484011&idx=1&sn=45a885492aa34f2b6c56a632d0ccb595 &chksm=fb22e5aecc556cb84933e0fb3be524da23f1e5641ab05f9e1daf0a62fd5826dd21ccdc1b5bb1#rd
Play a Happy Birthday song with Micro:bit