Skip to content

Instantly share code, notes, and snippets.

@lamegaton
Created August 31, 2018 05:44
Show Gist options
  • Save lamegaton/aaad3d8917834bdbfd8e459a4661ddfa to your computer and use it in GitHub Desktop.
Save lamegaton/aaad3d8917834bdbfd8e459a4661ddfa to your computer and use it in GitHub Desktop.
From my blog in 2013
#include "pitches.h"
// Khai báo dãy nốt nhạc:
int melody[] = {NOTE_C7, NOTE_A7,NOTE_G7, NOTE_C7, NOTE_G7,0, NOTE_B7, NOTE_C7};
// Cao độ của nốt: 4 = nốt đen, 8 = nốt móc, etc.:
int noteDurations[] = {4, 8, 8, 3,4,6,10,10 };
void setup() {
// Chơi tất cả các nốt:
for (int thisNote = 0; thisNote < 8; thisNote++) {
/* Để tính cao độ, ta lấy 1 giây chia cho nốt VD: nốt đen = 1000 / 4, nốt móc = 1000/8,...*/
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// Để phân biệt các nốt, ta chèn một khoảng thời gian ngắn giữa chúng.
// Ở đây coder chọn 30%:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:noTone(8);
}
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment