[arduino] - 아두이노/ESP32 - 피에조 부저, 악보를 보고 동요 멜로디 코딩하기(동요 겨울밤, 다람쥐, 옹달샘, 작은별)
[arduino] - 아두이노 - 피에조 부저 멜로디 BTS DNA
[arduino] - 아두이노 - 피에조 부저 멜로디 Hwasa Maria
[arduino] - 아두이노 - 피에조 부저 멜로디 BTS Dynamite
// 코드 설명 https://postpop.tistory.com/98
// 12화음 이름 정의
typedef enum {
NOTE_C, NOTE_Cs, NOTE_D, NOTE_Eb, NOTE_E, NOTE_F, NOTE_Fs, NOTE_G, NOTE_Gs,
NOTE_A, NOTE_Bb, NOTE_B, NOTE_MAX
} note_t;
// 화음별 피에조 부저 진동수 정의 및 옥타브에 따른 값 변경 함수
double ledcWriteNote(note_t note, uint8_t octave){
const uint16_t noteFrequencyBase[12] = {
// 1 2 3 4 5 6 7 8 9 10 11 12
// C C# D Eb E F F# G G# A Bb B
4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902
};
if(octave > 8 || note >= NOTE_MAX){
return 0;
}
double noteFreq = (double)noteFrequencyBase[note] / (double)(1 << (8-octave));
return noteFreq;
}
// ---------------------------------------------------------------------- 동요: 사과같은 내얼굴
uint8_t Tempo = 120;
float tempoRatio = float(Tempo)/60.0;
uint8_t KeySign = 0;
uint8_t octaveTemp = 4;
float beatTime = 3/4;
const note_t Mnote[] PROGMEM= {
NOTE_C,NOTE_D,NOTE_E,NOTE_E,NOTE_F,NOTE_E,NOTE_D,NOTE_D,NOTE_E,NOTE_F,NOTE_F,NOTE_G,NOTE_F,NOTE_E,
NOTE_E,NOTE_F,NOTE_G,NOTE_G,NOTE_C,NOTE_A,NOTE_G,NOTE_G,NOTE_C,NOTE_D,NOTE_E,NOTE_E,NOTE_D,NOTE_C,
};
const uint8_t Moct[] PROGMEM= {
4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,5,4,4,4,4,4,4,4,4,4,
0,
};
const uint8_t localKey[] PROGMEM= {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,
};
const uint8_t Mdur[] PROGMEM= {
16,
2,2,4,4,2,2,8,2,2,4,4,2,2,8,
2,2,4,4,2,2,4,4,2,2,4,4,4,8,
};
// ----------------------------------------------------------------------
uint8_t noteNum = 0;
note_t keySignature(note_t note) {
if (pgm_read_byte(&localKey[noteNum]) == 0) {
if (KeySign == 1) {
if (note == NOTE_B) note = NOTE_Bb;
} else if (KeySign == 2) {
if (note == NOTE_B) note = NOTE_Bb;
else if (note == NOTE_E) note = NOTE_Eb;
} else if (KeySign == 3) {
if (note == NOTE_B) note = NOTE_Bb;
else if (note == NOTE_E) note = NOTE_Eb;
else if (note == NOTE_A) note = NOTE_Gs;
} else if (KeySign == 4) {
if (note == NOTE_B) note = NOTE_Bb;
else if (note == NOTE_E) note = NOTE_Eb;
else if (note == NOTE_A) note = NOTE_Gs;
else if (note == NOTE_D) note = NOTE_Cs;
} else if (KeySign == 5) {
if (note == NOTE_B) note = NOTE_Bb;
else if (note == NOTE_E) note = NOTE_Eb;
else if (note == NOTE_A) note = NOTE_Gs;
else if (note == NOTE_D) note = NOTE_Cs;
else if (note == NOTE_G) note = NOTE_Fs;
} else if (KeySign == 6) {
if (note == NOTE_B) note = NOTE_Bb;
else if (note == NOTE_E) note = NOTE_Eb;
else if (note == NOTE_A) note = NOTE_Gs;
else if (note == NOTE_D) note = NOTE_Cs;
else if (note == NOTE_G) note = NOTE_Fs;
else if (note == NOTE_C) { note = NOTE_B; octaveTemp -= 1; }
} else if (KeySign == 7) {
if (note == NOTE_B) note = NOTE_Bb;
else if (note == NOTE_E) note = NOTE_Eb;
else if (note == NOTE_A) note = NOTE_Gs;
else if (note == NOTE_D) note = NOTE_Cs;
else if (note == NOTE_G) note = NOTE_Fs;
else if (note == NOTE_C) { note = NOTE_B; octaveTemp -= 1; }
else if (note == NOTE_F) note = NOTE_E;
} else if (KeySign == 8) {
if (note == NOTE_F) note = NOTE_Fs;
} else if (KeySign == 9) {
if (note == NOTE_F) note = NOTE_Fs;
else if (note == NOTE_C) note = NOTE_Cs;
} else if (KeySign == 10) {
if (note == NOTE_F) note = NOTE_Fs;
else if (note == NOTE_C) note = NOTE_Cs;
else if (note == NOTE_G) note = NOTE_Gs;
} else if (KeySign == 11) {
if (note == NOTE_F) note = NOTE_Fs;
else if (note == NOTE_C) note = NOTE_Cs;
else if (note == NOTE_G) note = NOTE_Gs;
else if (note == NOTE_D) note = NOTE_Eb;
} else if (KeySign == 12) {
if (note == NOTE_F) note = NOTE_Fs;
else if (note == NOTE_C) note = NOTE_Cs;
else if (note == NOTE_G) note = NOTE_Gs;
else if (note == NOTE_D) note = NOTE_Eb;
else if (note == NOTE_A) note = NOTE_Bb;
} else if (KeySign == 13) {
if (note == NOTE_F) note = NOTE_Fs;
else if (note == NOTE_C) note = NOTE_Cs;
else if (note == NOTE_G) note = NOTE_Gs;
else if (note == NOTE_D) note = NOTE_Eb;
else if (note == NOTE_A) note = NOTE_Bb;
else if (note == NOTE_E) note = NOTE_F;
} else if (KeySign == 14) {
if (note == NOTE_F) note = NOTE_Fs;
else if (note == NOTE_C) note = NOTE_Cs;
else if (note == NOTE_G) note = NOTE_Gs;
else if (note == NOTE_D) note = NOTE_Eb;
else if (note == NOTE_A) note = NOTE_Bb;
else if (note == NOTE_E) note = NOTE_F;
else if (note == NOTE_B) { note = NOTE_C; octaveTemp += 1; }
}
}
else { // 1. natural
if (pgm_read_byte(&localKey[noteNum]) == 2) { // 2. b
if (note == NOTE_C) { note = NOTE_B; octaveTemp -= 1; }
else if (note == NOTE_D) note = NOTE_Cs;
else if (note == NOTE_E) note = NOTE_Eb;
else if (note == NOTE_F) note = NOTE_E;
else if (note == NOTE_G) note = NOTE_Fs;
else if (note == NOTE_A) note = NOTE_Gs;
else if (note == NOTE_B) note = NOTE_Bb;
}
else if (pgm_read_byte(&localKey[noteNum]) == 3) { // 2. #
if (note == NOTE_C) note = NOTE_Cs;
else if (note == NOTE_D) note = NOTE_Eb;
else if (note == NOTE_E) note = NOTE_F;
else if (note == NOTE_F) note = NOTE_Fs;
else if (note == NOTE_G) note = NOTE_Gs;
else if (note == NOTE_A) note = NOTE_Bb;
else if (note == NOTE_B) { note = NOTE_C; octaveTemp += 1; }
}
else if (pgm_read_byte(&localKey[noteNum]) == 4) { // 4. bb
if (note == NOTE_C) { note = NOTE_Bb; octaveTemp -= 1; }
else if (note == NOTE_D) note = NOTE_C;
else if (note == NOTE_E) note = NOTE_D;
else if (note == NOTE_F) note = NOTE_Eb;
else if (note == NOTE_G) note = NOTE_F;
else if (note == NOTE_A) note = NOTE_G;
else if (note == NOTE_B) note = NOTE_A;
}
else if (pgm_read_byte(&localKey[noteNum]) == 5) { // 5. ##
if (note == NOTE_C) note = NOTE_D;
else if (note == NOTE_D) note = NOTE_E;
else if (note == NOTE_E) note = NOTE_Fs;
else if (note == NOTE_F) note = NOTE_G;
else if (note == NOTE_G) note = NOTE_A;
else if (note == NOTE_A) note = NOTE_B;
else if (note == NOTE_B) { note = NOTE_Cs; octaveTemp += 1; }
}
}
return note;
}
// ----------------------------------------------------------------------
int beepPin = 10;
void setup() {
Serial.begin(9600);
}
bool beepMelody = false;
unsigned long int beepTime = 0;
// 시리얼 모니터에서 0을 입력하면 멜로디 재생
// 0을 한번더 입력하면 멜로디 정지
void loop() {
if(Serial.available() > 0){
String temp = Serial.readStringUntil('\n');
Serial.println(temp);
if (temp == "0") {
beepMelody = !beepMelody;
noteNum = 0; beepTime = 0;
if (!beepMelody) noTone(beepPin);
}
}
if (beepMelody) {
if (millis() - beepTime >= pgm_read_byte(&Mdur[noteNum])*(1000/(4*tempoRatio))) {
beepTime = millis();
noTone(beepPin);
octaveTemp = pgm_read_byte(&Moct[noteNum]);
note_t KeyNote = keySignature(pgm_read_word(&Mnote[noteNum]));
tone(beepPin, ledcWriteNote(KeyNote, octaveTemp)); // ledcWriteNote(uint8_t channel, note_t note, uint8_t octaveTemp);
noteNum++;
if (noteNum == sizeof(Mdur)) { // 초기화
noTone(beepPin);
noteNum = 0;
beepMelody = false;
}
}
}
}
반응형
'Arduino > Piezo Buzzer' 카테고리의 다른 글
피에조 부저 멜로디 - 아빠와 크레파스 (0) | 2020.10.26 |
---|---|
피에조 부저 멜로디 - 아기공룡 둘리 (0) | 2020.10.26 |
피에조 부저 멜로디 - 섬집 아기 (0) | 2020.10.26 |
피에조 부저 멜로디 - 동요: 산토끼 (0) | 2020.10.26 |
피에조 부저 멜로디 - 동요: 똑같아요 (0) | 2020.10.26 |
피에조 부저 멜로디 - 동물농장 (0) | 2020.10.26 |
피에조 부저 멜로디 - 동요: 눈 (0) | 2020.10.26 |
피에조 부저 멜로디 - 동요: 꼬부랑 할머니 (0) | 2020.10.26 |