Go Down

Topic: Need help with frequency code (Read 475 times) previous topic - next topic

robbarriga

I am trying to create a drum tuner with arduino. For this small project i am using a arduino mkrzero and a microphone: MAX9814. I am still fairly new to coding and would like some help. I would like this code to be able to detect and display the peak frequency when i hit the drum.

I tried using the library AudioFrequencyMeter and it does display the frequency but it displaying a bunch of different ones. After the drum is hit, i want it to be able to display the peak frequency.


link for library: https://www.arduino.cc/en/Reference/AudioFrequencyMeter

mugambi

Hi rob..

Would mind sharing the code you are using.  I could take a look.  If you prefer pm..

robbarriga

Hi rob..

Would mind sharing the code you are using.  I could take a look.  If you prefer pm..
Here's the code.

#include <AudioFrequencyMeter.h>

AudioFrequencyMeter meter;

void setup() {
  Serial.begin(115200);
  Serial.println("started");

  meter.setBandwidth(70.00, 250);    // Ignore frequency out of this range
  meter.begin(A0, 45000);             // Intialize A0 at sample rate of 45kHz
}

void loop() {
  float frequency = meter.getFrequency();
  if (frequency > 0)
  {
    Serial.print(frequency);
    Serial.println(" Hz");
  }
}

mugambi

Something like this should work.

Code: [Select]


#include <AudioFrequencyMeter.h>

AudioFrequencyMeter meter;
float currentFreq = 0.0;
float freq = 0.0;
float lastFreq = 0.0;
void setup() {
  Serial.begin(115200);
  Serial.println("started");

  meter.setBandwidth(70.00, 250);    // Ignore frequency out of this range
  meter.begin(A0, 45000);             // Intialize A0 at sample rate of 45kHz
}

void loop() {
  float freq = meter.getFrequency();

  if (freq > 0)
  {

    if(currentFreq>lastFreq)
    {
      lastFreq = currentFreq; //store current maximum freq
      currentFreq=freq;
    }
    if(currentFreq>freq) //maximum has been reached on decline
    {
      Serial.print(frequency);
      Serial.println(" Hz");
      lastFreq=currentFreq=0;
    }
  }
}



mugambi

#4
Jan 22, 2019, 09:33 am Last Edit: Jan 23, 2019, 01:13 pm by mugambi
Corrected code
Code: [Select]


#include <AudioFrequencyMeter.h>

AudioFrequencyMeter meter;
float currentFreq = 0.0;
float freq = 0.0;
float lastFreq = 0.0;
void setup() {
  Serial.begin(115200);
  Serial.println("started");

  meter.setBandwidth(70.00, 250);    // Ignore frequency out of this range
  meter.begin(A0, 45000);             // Intialize A0 at sample rate of 45kHz
}

void loop() {
  float freq = meter.getFrequency();

  if (freq > 0)
  {

if(currentFreq<freq)
{
currentFreq=freq;
}

    if(currentFreq>lastFreq)
    {
      lastFreq = currentFreq; //store current maximum freq
      currentFreq=freq;
    }
    if(currentFreq>freq) //maximum has been reached on decline
    {
      Serial.print(lastFreq);
      Serial.println(" Hz");
      lastFreq=currentFreq=0;
    }
  }
}




robbarriga

I tried the updated code, it didnt seem to work. In the serial monitor it is only displaying 0 hz and it value doesnt change. i generated a sound of 160hz and it did not detect it, it was still showing as 0 hz.

arduarn

You'd probably be better off looking for a FFT library for the mkrzero.

robbarriga

You'd probably be better off looking for a FFT library for the mkrzero.
Do you have a link for the library?

arduarn


mugambi

Corrected code
Code: [Select]


#include <AudioFrequencyMeter.h>

AudioFrequencyMeter meter;
float currentFreq = 0.0;
float freq = 0.0;
float lastFreq = 0.0;
void setup() {
  Serial.begin(115200);
  Serial.println("started");

  meter.setBandwidth(70.00, 250);    // Ignore frequency out of this range
  meter.begin(A0, 45000);             // Intialize A0 at sample rate of 45kHz
}

void loop() {
  float freq = meter.getFrequency();

  if (freq > 0)
  {

if(currentFreq<freq)
{
currentFreq=freq;
}

    if(currentFreq>lastFreq)
    {
      lastFreq = currentFreq; //store current maximum freq
      currentFreq=freq;
    }
    if(currentFreq>freq) //maximum has been reached on decline
    {
      Serial.print(lastFreq);
      Serial.println(" Hz");
      lastFreq=currentFreq=0;
    }
  }
}




Let me know how this code performs. I had missed something pretty obvious..updating current freq

robbarriga

Let me know how this code performs. I had missed something pretty obvious..updating current freq
It is working now, thanks for the update.

mugambi


Go Up