안녕하세요!
조덕배 '꿈에' 피에조부저 멜로디 코드입니다.
// 코드 설명 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 = 70;
float tempoRatio = float(Tempo)/60.0;
uint8_t KeySign = 9;
uint8_t octaveTemp = 4;
float beatTime = 4/4;
const note_t Mnote[] PROGMEM= {
NOTE_D,NOTE_F,NOTE_A,NOTE_E,NOTE_MAX,NOTE_F,NOTE_A,NOTE_D,NOTE_F,NOTE_MAX,NOTE_D,NOTE_F,NOTE_A,NOTE_E,NOTE_MAX,NOTE_A,NOTE_D,NOTE_F,NOTE_A,NOTE_MAX,NOTE_MAX,NOTE_F,
NOTE_A,NOTE_MAX,NOTE_A,NOTE_B,NOTE_C,NOTE_MAX,NOTE_C,NOTE_C,NOTE_B,NOTE_B,NOTE_A,NOTE_A,NOTE_G,NOTE_G,NOTE_F,
NOTE_G,NOTE_A,NOTE_G,NOTE_F,NOTE_G,NOTE_MAX,NOTE_MAX,NOTE_E,NOTE_F,NOTE_G,NOTE_MAX,NOTE_E,NOTE_D,NOTE_C,NOTE_D,
NOTE_A,NOTE_G,NOTE_F,NOTE_MAX,NOTE_MAX,NOTE_F,NOTE_G,NOTE_A,NOTE_B,NOT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2,1,1,8,3,1,1,1,1,1,
8,3,1,2,1,1,2,1,1,4,3,1,1,1,1,1,
8,3,1,2,1,1,3,1,6,1,1,1,1,1,1,
8,1,2,1,2,2,1,1,1,1,6,1,1,2,1,1,
8,3,1,2,1,1,3,1,6,1,1,1,1,1,1,
8,1,2,1,1,2,1,1,1,1,1,4,3,1,1,1,1,1,
8,3,1,2,1,1,2,1,1,6,1,1,1,1,1,1,
8,1,2,1,2,2,1,1,1,1,6,1,1,2,1,1,
8,3,1,2,2,3,1,12,
12,1,2,1,1,1,2,8,2,1,1,
8,2,2,1,1,2,1,1,1,1,2,6,1,2,1,
1,1,8,4,1,1,2,1,1,12,
2,1,1,1,1,1,1,1,3,1,1,1,1,1,3,8,2,2,
8,4,2,1,1,1,3,1,2,1,2,1,1,2,1,1,
8,3,1,1,1,1,1,8,3,1,2,1,1,
2,1,1,4,3,1,1,1,1,1,8,3,1,2,1,1,
3,1,6,1,1,1,1,1,1,8,1,2,1,2,2,
1,1,1,1,6,1,1,2,1,1,8,3,1,2,1,1,
3,1,6,1,1,1,1,1,1,8,1,2,1,1,2,1,
1,1,1,1,4,3,1,1,1,1,1,8,3,1,2,1,1,
2,1,1,6,1,1,1,1,1,1,8,1,2,1,2,2,
1,1,1,1,6,1,1,2,1,1,8,3,1,2,2,
3,1,12,16,
};
// ----------------------------------------------------------------------
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;
}
}
}
}
*참조악보*
https://youtu.be/Lt8VeR-FjxI?feature=shared
사진 - 픽사베이
반응형
'Arduino > Piezo Buzzer' 카테고리의 다른 글
아두이노 - 피에조 부저 멜로디 - 로제_브루노마스 아파트 (0) | 2024.11.29 |
---|---|
아두이노 - 피에조 부저 멜로디 - 아틀란티스 소녀 (0) | 2024.09.29 |
아두이노 - 피에조 부저 멜로디 아기상어 (0) | 2024.09.04 |
아두이노 - 피에조 부저 멜로디 BTS (방탄소년단) - Butter (0) | 2022.12.23 |
피에조 부저 멜로디 - 동요: 햇볕은 쨍쨍 (0) | 2020.10.26 |
피에조 부저 멜로디 - 동요: 학교종 (0) | 2020.10.26 |
피에조 부저 멜로디 - 동요: 주먹 쥐고 손을 펴서 (0) | 2020.10.26 |
피에조 부저 멜로디 - 동요: 자전거 (0) | 2020.10.26 |