# 蜂鸣器演奏音乐的两个要素就是声音频率、声音持续时间,分别对应乐谱中的音调、节拍。
from machine import Pin,PWM
import time
beeper = PWM(Pin(22)) #定义PWM波端口为GPIO22 # 通过Pin对象来创建PWM对象
tone = [247,262,294,330,349,392,440,494,523,587,659,698,784,1000] # 设置蜂鸣器的声音大小,由ADC值来控制,蜂鸣器声音范围0-1023
# 音调
music=[
5,5,6,8,7,6,5,6,13,13,
5,5,6,8,7,6,5,3,13,13,
2,2,3,5,3,5,6,3,2,1,
6,6,5,6,5,3,6,5,13,13,
5,5,6,8,7,6,5,6,13,13,
5,5,6,8,7,6,5,3,13,13,
2,2,3,5,3,5,6,3,2,1,
6,6,5,6,5,3,6,1,
13,8,9,10,10,9,8,10,9,8,6,
13,6,8,9,9,8,6,9,8,6,5,
13,2,3,5,5,3,5,5,6,8,7,6,
6,10,9,9,8,6,5,6,8
]
# 节拍
beat = [
2,4,2,2,2,2,2,8,4,4,
2,4,2,2,2,2,2,8,4,4,
2,4,2,4,2,2,4,2,2,8,
2,4,2,2,2,2,2,8,4,4,
2,4,2,2,2,2,2,8,4,4,
2,4,2,2,2,2,2,8,4,4,
2,4,2,4,2,2,4,2,2,8,
2,4,2,2,2,2,2,8,
4,2,2,2,4,2,2,2,2,2,8,
4,2,2,2,4,2,2,2,2,2,8,
4,2,2,2,4,2,2,5,2,6,2,4,
2,2,2,4,2,4,2,2,12
]
'''
# 方法一:
i = 0
while True:
speed = beat[i] # 设置播放速度,speed值越小,等待时间越短,播放速度越快
voice = tone[music[i]] # 音调大小
beeper.init(freq=voice) # PWM初始化并设置PWM频率
time.sleep(speed) # 等待时间
print(voice) # 打印出ADC值
i = i + 1 # i自增加1
if i > len(music) - 1: # 如果i的值大于music列表的长度
beeper.deinit() # 关闭PWM
break # 跳出循环
'''
# 方法二:
for i,j in zip(music,beat):
if(music == []): # 判断列表是否为空
beeper.duty(0) # 设置PWM波的占空比为0
else:
beeper.duty(512) # 设置PWM波的占空比为512,即50%
beeper.freq(tone[i-1])
time.sleep(0.12*j) # 控制节拍速度
beeper.duty(0)
time.sleep(0.15)