arduino蜂鸣器青花瓷代码
时间: 2023-09-08 12:10:19 浏览: 911
以下是使用Arduino控制蜂鸣器播放《青花瓷》的示例代码:
```
int buzzerPin = 8; //定义蜂鸣器连接的引脚
//定义青花瓷歌曲的音符和时长
int melody[] = { 659, 587, 523, 440, 494, 523, 587, 659, 659, 587, 523, 440, 494, 523, 587, 659, 587, 494, 523, 440, 392, 440, 494, 523, 587, 494, 523, 440, 494, 523, 587, 659, 659, 587, 523, 440, 494, 523, 587, 659, 587, 494, 523, 440, 392, 440, 494, 523, 587, 494, 523, 440, 494, 523, 587, 659, 587, 523, 440, 494, 523, 587, 659, 587, 494, 523, 440, 392, 440, 494, 523, 587, 494, 523, 440, 494, 523, 587, 659, 587, 523, 440, 494, 523, 587, 659};
int noteDurations[] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};
void setup() {
pinMode(buzzerPin, OUTPUT); //将蜂鸣器引脚设置为输出模式
}
void loop() {
//循环播放青花瓷歌曲
for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) {
int duration = 1000 / noteDurations[i];
tone(buzzerPin, melody[i], duration); //播放当前音符
delay(duration * 1.30); //等待一段时间,使得音符之间有间隔
noTone(buzzerPin); //停止播放
}
}
```
将以上代码复制到Arduino IDE中并上传到Arduino板子中,即可听到蜂鸣器播放的《青花瓷》曲目。请注意,该示例代码仅供学习和参考之用,如需商用请遵守相关法律法规。
阅读全文