arduino蜂鸣器小星星音乐代码
时间: 2023-09-10 09:04:51 浏览: 319
蜂鸣器的音乐代码
以下是使用 Arduino 蜂鸣器播放 "小星星" 的示例代码:
```c++
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523
int starMelody[] = {
NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4,
NOTE_A4, NOTE_A4, NOTE_G4,
NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4,
NOTE_D4, NOTE_D4, NOTE_C4,
NOTE_G4, NOTE_G4, NOTE_F4, NOTE_F4,
NOTE_E4, NOTE_E4, NOTE_D4,
NOTE_G4, NOTE_G4, NOTE_F4, NOTE_F4,
NOTE_E4, NOTE_E4, NOTE_D4,
NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4,
NOTE_A4, NOTE_A4, NOTE_G4,
NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4,
NOTE_D4, NOTE_D4, NOTE_C4
};
int starNoteDurations[] = {
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 playNote(int note, int duration) {
tone(8, note, duration);
delay(duration+10);
}
void setup() {
pinMode(8, OUTPUT);
}
void loop() {
for (int i = 0; i < sizeof(starMelody) / sizeof(starMelody[0]); i++) {
int noteDuration = 1000 / starNoteDurations[i];
playNote(starMelody[i], noteDuration);
}
}
```
阅读全文