Need help with frequency code

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

Hi rob..

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

mugambi:
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");
}
}

Something like this should work.

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

Corrected code

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

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.

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

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

Do you have a link for the library?

robbarriga: Do you have a link for the library?

'fraid not.

mugambi:
Corrected code

#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

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

It is working now, thanks for the update.

You are welcome.