Micro:bit自带一个有趣的功能就是可以生成音乐播放,今天做一个简单实用的案例,用Micro:bit播放生日快乐歌。
算法:
按下按键A,显示生日快乐
播放D
播放D
播放E
播放D
播放G
播放F
休息
为了播放音乐,需要将一个可以3V工作的蜂鸣器或者耳机,播放器连到Micro:bit的GND和Pine0脚之间,如下图
Blocky程序如下
MicroPython程序脚本如下:
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)
下载就可以播放啦。
原文参考
https://mp.weixin.qq.com/s?__biz=MzU0MTg0ODIzNA==&mid=2247484011&idx=1&sn=45a885492aa34f2b6c56a632d0ccb595&chksm=fb22e5aecc556cb84933e0fb3be524da23f1e5641ab05f9e1daf0a62fd5826dd21ccdc1b5bb1#rd
原文地址:https://www.cnblogs.com/Astar-STEAM/p/9770015.html