使用ArduinoIDE,帮我生成一段使用蜂鸣器演奏スーパーマリオブラザーズ的代码
时间: 2024-06-09 13:07:42 浏览: 81
好的,下面是生成的代码,请将其复制到ArduinoIDE中,然后将蜂鸣器连接到Arduino板子的数字引脚8上,上传代码,即可听到演奏效果。
```
int speakerPin = 8;
// notes of the moledy followed by the duration.
int melody[] = {
// スーパーマリオブラザーズ
NOTE_E7, 8, NOTE_E7, 8, 0, 8, NOTE_C7, 8, NOTE_E7, 8, 0, 8, NOTE_G7, 8, 0, 8,
0, 8, NOTE_G6, 8, 0, 8, 0, 8, 0, 8,
// 上がっていく部分
NOTE_C7, 8, 0, 8, NOTE_G6, 8, 0, 8, NOTE_E6, 8, 0, 8, NOTE_A6, 8, 0, 8,
NOTE_B6, 8, 0, 8, NOTE_AS6, 8, NOTE_A6, 8, 0, 8, NOTE_G6, 8, NOTE_E7, 8, 0, 8,
NOTE_G7, 4, NOTE_A7, 4, NOTE_F7, 4, NOTE_G7, 4, 0, 4, NOTE_E7, 4, 0, 4,
NOTE_C7, 4, NOTE_D7, 4, NOTE_B6, 4, NOTE_C7, 4, 0, 4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4,
8, 8, 8, 8, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8
};
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < sizeof(melody) / sizeof(int) / 2; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(speakerPin, melody[thisNote * 2], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(speakerPin);
}
}
```
注意:本代码参考了https://www.arduino.cc/en/Tutorial/ToneMelody,如需详细了解请前往该网站。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)