2018年11月10日

bpi-bit buzzer music

參考midi網址:https://en.m.wikipedia.org/wiki/Scientific_pitch_notation#Table_of_note_frequencies


 #define Buzzer 25   //GPIO 25

// 0 NULL 0.0001
// 5    LG 196.00
// 5.5  #LG 207.65
// 6    LA 220.00
// 6.5  #LA 233.08
// 7    LB 246.94
// 11    C 261.63
// 11.5 #C 277.18
// 12    D 293.66
// 12.5 #D 311.13
// 13    E 329.63
// 14    F 349.23
// 14.5 #F 369.99
// 15    G 392.00
// 15.5  #G 415.30
// 21    A 440.00
// 21.5  #A 466.16
// 22    B 493.88

#define TIME 250       // 1拍時間
#define LEDC_CHANNEL 0    
#define LEDC_TIMER_BIT 13
#define LEDC_BASE_FREQ 5000
double music_star[] = {11,11,15,15,16,16,15,0,14,14,13,13,12,12,11,0,15,15,14,14,13,13,12,0,15,15,14,14,13,13,12,0,11,11,15,15,16,16,15,0,14,14,13,13,12,12,11,0 };
double music_happy[] = {5,5,6,5,11,7,0,5,5,6,5,12,11,0,5,5,15,13,11,7,6,0,0,0,14,13,12,11,0 };
double music_ode[] = {7,7,11,12,12,11,7,6,5,5,6,7,0,6,6,0,7,7,11,12,12,11,7,6,5,5,6,7,6,0,5,5,0};

void setup(){
  ledcSetup(LEDC_CHANNEL, LEDC_BASE_FREQ, LEDC_TIMER_BIT);
  ledcAttachPin(Buzzer, LEDC_CHANNEL);
}

double to_num(double m_n){
double r_n = 0.00;
if (m_n ==5){
  r_n = 196.00;
}else if( m_n ==5.5){
  r_n = 207.65;
}else if ( m_n == 6){
  r_n = 220.00;
}else if ( m_n == 6.5){
  r_n = 233.08;
}else if ( m_n == 7){
  r_n = 246.94;
}else if ( m_n == 11){
  r_n = 261.63;
}else if ( m_n == 11.5){
  r_n = 277.18;
}else if ( m_n == 12){
  r_n = 293.66;
}else if( m_n == 12.5){
  r_n = 311.13;
}else if( m_n == 13){
  r_n = 329.63;
}else if( m_n == 14){
  r_n = 349.23;
}else if( m_n == 14.5){
  r_n = 369.99;
}else if( m_n == 15){
  r_n = 392.00;
}else if( m_n ==15.5){
  r_n = 415.30;
}else if( m_n == 16){
  r_n = 440.00;
}else if( m_n ==16.5){
  r_n = 466.16;
}else if( m_n == 17){
  r_n =493.88;
}else if( m_n ==21){
  r_n =523.25;
}
   return r_n ;
}


void loop(){
  for (int i=0;i<sizeof(music_happy)/8;i++)
  {
  
    if (to_num(music_happy[i]) == 0){
          delay(TIME);
      }else{
        ledcWriteTone(LEDC_CHANNEL, to_num(music_happy[i]));
        delay(TIME);
        }
    }
  
  ledcWriteTone(LEDC_CHANNEL, 0);
  delay(1000);
}

沒有留言: