|
我正在做一個吉他的調音器
我在網路上找到了一個計頻器,說也可以測聲音頻率
所以我用他改寫成一個吉他條音器
我硬體部分 我麥克風模組直接輸入ARDUINO 判斷準度輸出LED
程式如下
int divider[6] = {0, 1, 8, 64, 256, 1024};
int prescaler = 5;
double count = 0;
double middle = 0;
char x = 0;
int a;
int led1G=3;
int led1R=5;
int led1B=6;
int led2G=9;
int led2R=10;
int led2B=11;
int led3G=A0;
int led3R=A1;
int led3B=A2;
ISR(TIMER1_OVF_vect)
{
if (prescaler < 4)
{
prescaler++;
}
}
void interrupt()
{
if (!x)
{
count = TCNT1;
TCNT1 = 0x000;
TCCR1B = prescaler;
attachInterrupt(0, interrupt, FALLING);
}
else
{
middle = TCNT1;
attachInterrupt(0, interrupt, RISING);
}
x = ~x;
}
void setup()
{
Serial.begin(57600);
TIMSK1 = 0x01;
TCCR1A = 0x00;
attachInterrupt(0, interrupt, RISING);
pinMode(led1G,OUTPUT);
pinMode(led1R,OUTPUT);
pinMode(led1B,OUTPUT);
pinMode(led2G,OUTPUT);
pinMode(led2R,OUTPUT);
pinMode(led2B,OUTPUT);
pinMode(led3G,OUTPUT);
pinMode(led3R,OUTPUT);
pinMode(led3B,OUTPUT);
}
void loop()
{
Serial.print("Freq: ");
a=16000000.0 / divider[prescaler] / count;
Serial.print(a);
Serial.print(" Hz\t\tDuty: ");
Serial.print(middle / count * 100);
Serial.print(" %\t\tPeriod: ");
Serial.print(0.0000625 * divider[prescaler]*count);
Serial.print(" ms\t\tH-time: ");
Serial.print(0.0000625 * divider[prescaler]*middle);
Serial.print(" ms\t\tL-time: ");
Serial.print(0.0000625 * divider[prescaler]*(count - middle));
Serial.println(" ms");
if (prescaler > 1)
{
prescaler--;
delay(100);
}
if (81<=a && a<=83) //E音準確
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,LOW);
digitalWrite(led1B,LOW);
digitalWrite(led2G,HIGH);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,LOW);
digitalWrite(led3B,LOW);
delay(1000);
}
if(a>=75 && a<=80) //E音偏低
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,HIGH);
digitalWrite(led1B,LOW);
digitalWrite(led2G,LOW);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,LOW);
digitalWrite(led3B,LOW);
delay(1000);
}
if(a>=84 && a<=90) //E音偏高
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,LOW);
digitalWrite(led1B,LOW);
digitalWrite(led2G,LOW);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,HIGH);
digitalWrite(led3B,LOW);
delay(1000);
}
if (109<=a && a<=111 ) //A音準確
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,LOW);
digitalWrite(led1B,LOW);
digitalWrite(led2G,HIGH);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,LOW);
digitalWrite(led3B,LOW);
delay(1000);
}
if(a>=105 && a<=108) //A音偏低
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,HIGH);
digitalWrite(led1B,LOW);
digitalWrite(led2G,LOW);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,LOW);
digitalWrite(led3B,LOW);
delay(1000);
}
if(a>=112 && a<=116) //A音偏高
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,LOW);
digitalWrite(led1B,LOW);
digitalWrite(led2G,LOW);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,HIGH);
digitalWrite(led3B,LOW);
delay(1000);
}
if(a>=145 && a<=150) //D音準確
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,LOW);
digitalWrite(led1B,LOW);
digitalWrite(led2G,HIGH);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,LOW);
digitalWrite(led3B,LOW);
delay(1000);
}
if(a>=140 && a<=144) //D音偏低
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,HIGH);
digitalWrite(led1B,LOW);
digitalWrite(led2G,LOW);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,LOW);
digitalWrite(led3B,LOW);
delay(1000);
}
if(a>=151 &&a<=155) //D音偏高
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,LOW);
digitalWrite(led1B,LOW);
digitalWrite(led2G,LOW);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,HIGH);
digitalWrite(led3B,LOW);
delay(1000);
}
if(a>=194 && a<=198) //G音準確
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,LOW);
digitalWrite(led1B,LOW);
digitalWrite(led2G,HIGH);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,LOW);
digitalWrite(led3B,LOW);
delay(1000);
if(a>=189 && a<=193)
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,HIGH);
digitalWrite(led1B,LOW);
digitalWrite(led2G,LOW);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,LOW);
digitalWrite(led3B,LOW);
delay(1000);
}
if(a>=198 && a<=202)
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,LOW);
digitalWrite(led1B,LOW);
digitalWrite(led2G,LOW);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,HIGH);
digitalWrite(led3B,LOW);
delay(1000);
}
}
else
{
digitalWrite(led1G,LOW);
digitalWrite(led1R,LOW);
digitalWrite(led1B,LOW);
digitalWrite(led2G,LOW);
digitalWrite(led2R,LOW);
digitalWrite(led2B,LOW);
digitalWrite(led3G,LOW);
digitalWrite(led3R,LOW);
digitalWrite(led3B,LOW);
}
}
測出來頻率卻是這樣,而且要彈很多次才會出現正常一點的頻率,大部分都是很奇怪的數字,每條弦都一樣
請問是哪裡出問題?
順便問 這個程式輸出腳是2腳,要怎麼改成別的輸出點? |
|