반응형

[arduino] - 아두이노/ESP32 - 피에조 부저, 악보를 보고 동요 멜로디 코딩하기(동요 겨울밤, 다람쥐, 옹달샘, 작은별)

[arduino] - 아두이노 - 피에조 부저 멜로디 BTS DNA

[arduino] - 아두이노 - 피에조 부저 멜로디 Hwasa Maria

[arduino] - 아두이노 - 피에조 부저 멜로디 BTS Dynamite

 

 

piezo_Uno_melody11.ino
0.01MB
piezo_nodeMcu_melody11.ino
0.01MB

// 코드 설명 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;
float beatTime = 4/4;

const note_t Mnote[] PROGMEM= {
NOTE_A,NOTE_G,NOTE_F,NOTE_MAX,NOTE_D,NOTE_F,NOTE_C,NOTE_A,NOTE_C,NOTE_F,NOTE_G,NOTE_A,NOTE_MAX,
NOTE_C,NOTE_D,NOTE_C,NOTE_A,NOTE_C,NOTE_A,NOTE_G,NOTE_F,NOTE_D,NOTE_F,NOTE_C,NOTE_MAX,
NOTE_F,NOTE_A,NOTE_C,NOTE_A,NOTE_C,NOTE_D,NOTE_C,NOTE_A,NOTE_G,NOTE_C,NOTE_F,NOTE_MAX,
};

const uint8_t Moct[] PROGMEM= {
4,4,4,4,4,4,4,4,5,4,4,4,4,
5,5,5,4,5,4,4,4,4,4,4,4,
4,4,5,4,5,5,5,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,
};

const uint8_t Mdur[] PROGMEM= {
16,
4,4,4,4,6,2,8,4,4,4,4,12,4,
6,2,8,4,4,8,6,2,4,4,12,4,
4,4,8,4,4,8,6,2,4,4,12,4,
};
// ----------------------------------------------------------------------

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;
      }
    }
  } 
}

+ Recent posts