簡譜_生日快樂

 /*

  Arduino Happy Birthday
  數字簡譜版
  蜂鳴器接 D8
*/
// 音階頻率
const int freq[] = {
  0,    // 0 = 休止符
  262,  // 1 = Do
  294,  // 2 = Re
  330,  // 3 = Mi
  349,  // 4 = Fa
  392,  // 5 = Sol
  440,  // 6 = La
  466,  // 7 = Si(b)
  523   // 8 = 高音Do
};
// Happy Birthday 簡譜
int melody[] = {
  1, 1, 2, 1, 4, 3,
  1, 1, 2, 1, 5, 4,
  1, 1, 8, 6, 4, 3, 2,
  7, 7, 6, 4, 5, 4
};
// 對應拍子(ms)
int noteDurations[] = {
  400, 400, 800, 800, 800, 1600,
  400, 400, 800, 800, 800, 1600,
  400, 400, 800, 800, 800, 800, 1600,
  400, 400, 800, 800, 800, 1600
};
void setup() {
  pinMode(8, OUTPUT);
}
void loop() {
  int notes = sizeof(melody) / sizeof(melody[0]);
  for (int i = 0; i < notes; i++) {
    int note = melody[i];
    if (note > 0) {
      tone(8, freq[note], noteDurations[i]);
    }
    delay(noteDurations[i] * 1.3);
    noTone(8);
  }
  // 播放完等待4秒
  delay(4000);
}

留言