利用蜂鸣器写一个能在arduino上面执行的小情歌编程
时间: 2024-03-16 10:44:42 浏览: 30
好的,以下是一个简单的小情歌程序,使用Arduino的蜂鸣器播放:
```c
int speakerPin = 8;
int length = 15; // 歌曲长度
// 歌曲的音符和持续时间(毫秒)
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4, NOTE_C4, NOTE_C4, NOTE_C5
};
int noteDurations[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8
};
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
int duration = noteDurations[i];
tone(speakerPin, melody[i], duration * 10);
delay(duration * 10 * 1.3);
}
}
```
这个程序使用了Arduino的tone()函数,它需要两个参数:蜂鸣器引脚和要播放的频率。在这个例子中,我们使用了一个数组来存储音符和持续时间,循环播放这个数组中的每个音符。
相关问题
利用蜂鸣器buzzer写一个能在arduino上面执行的小情歌编程
好的,以下是一个简单的小情歌程序,使用Arduino的蜂鸣器播放:
```c
int buzzerPin = 8;
int length = 15; // 歌曲长度
// 歌曲的音符和持续时间(毫秒)
int melody[] = {
262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349, 262, 262, 523
};
int noteDurations[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8
};
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
int duration = noteDurations[i];
tone(buzzerPin, melody[i], duration * 10);
delay(duration * 10 * 1.3);
}
}
```
这个程序使用了Arduino的tone()函数,它需要两个参数:蜂鸣器引脚和要播放的频率。在这个例子中,我们使用了一个数组来存储音符和持续时间,循环播放这个数组中的每个音符。
利用蜂鸣器buzzer写一个能在arduino上面执行的小星星编程
好的,以下是一个简单的小星星程序,使用Arduino的蜂鸣器播放:
```c
int buzzerPin = 8;
int length = 18; // 歌曲长度
// 歌曲的音符和持续时间(毫秒)
int melody[] = {
262, 262, 392, 392, 440, 440, 392,
349, 349, 330, 330, 294, 294, 262,
392, 392, 349, 349, 330, 330, 294,
392, 392, 349, 349, 330, 330, 294,
262, 262, 392, 392, 440, 440, 392,
349, 349, 330, 330, 294, 294, 262
};
int noteDurations[] = {
4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4, 2
};
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
int duration = noteDurations[i];
tone(buzzerPin, melody[i], duration * 10);
delay(duration * 10 * 1.3);
}
}
```
这个程序使用了Arduino的tone()函数,它需要两个参数:蜂鸣器引脚和要播放的频率。在这个例子中,我们使用了一个数组来存储音符和持续时间,循环播放这个数组中的每个音符。整个程序就是播放了一遍《小星星》的旋律。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)