[arduino] - 아두이노/ESP32 - 피에조 부저, 악보를 보고 동요 멜로디 코딩하기(동요 겨울밤, 다람쥐, 옹달샘, 작은별)
[arduino] - arduino - Simple Melody 이용 피에조 부저 멜로디 코딩하기, Esp01, EEPROM
[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 = 1;
uint8_t octaveTemp = 4;
const note_t Mnote[] PROGMEM= {
NOTE_F, NOTE_F, NOTE_G, NOTE_A, NOTE_F, NOTE_G, NOTE_G, NOTE_E, NOTE_E, NOTE_F,
NOTE_A, NOTE_A, NOTE_A, NOTE_B, NOTE_C, NOTE_C, NOTE_A, NOTE_B, NOTE_B, NOTE_G, NOTE_G, NOTE_A,
NOTE_C, NOTE_C, NOTE_C, NOTE_C, NOTE_C, NOTE_D, NOTE_C, NOTE_B, NOTE_B, NOTE_C, NOTE_B, NOTE_A,
NOTE_A, NOTE_A, NOTE_A, NOTE_A, NOTE_A, NOTE_B, NOTE_A, NOTE_G, NOTE_G, NOTE_A, NOTE_G, NOTE_F,
NOTE_F, NOTE_F, NOTE_F, NOTE_F, NOTE_F, NOTE_F, NOTE_F, NOTE_F, NOTE_C, NOTE_C, NOTE_C, NOTE_C, NOTE_F,
};
const uint8_t Moct[] PROGMEM= {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 5, 5, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 4, 4, 5, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 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, 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, 0,
0,
};
const uint8_t Mdur[] PROGMEM= {
16,
4, 3, 1, 4, 4, 3, 1, 3, 1, 8,
3, 1, 3, 1, 3, 1, 4, 3, 1, 3, 1, 8,
3, 1, 3, 1, 4, 3, 1, 3, 1, 3, 1, 8,
3, 1, 4, 3, 1, 3, 1, 3, 1, 3, 1, 8,
3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 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.25 |
---|---|
피에조 부저 멜로디 - 산꼴짝에 다람쥐 (0) | 2020.10.24 |
피에조 부저 멜로디 - 바둑이 방울 (0) | 2020.10.24 |
피에조 부저 멜로디 - 둥근해가 떴습니다 (0) | 2020.10.24 |
피에조 부저 멜로디 - 동요 귀여운 꼬마 (0) | 2020.10.24 |
피에조 부저 멜로디 - 동요 겨울밤 (0) | 2020.10.24 |
아두이노 - 피에조 부저 멜로디 BTS Dynamite (0) | 2020.10.23 |
아두이노 - 피에조 부저 멜로디 Hwasa Maria (0) | 2020.10.23 |