Band/HIgh/Low pass filter for audio

Hello
I am trying to make my robot do a certain action when it hears a tone between 2kHz and 3kHz. Is it possible to have a software implemented band(or combination of high and low) pass filter?
I tried using the filters library from the Arduino playground but couldn't find any explanations on how to properly use the library. what I tried doing was:

void loop()
{
  int micValue;
  float lowPassFrequency = 3000;
  float highPassFrequency = 2000;

  FilterOnePole lowpassFilter(LOWPASS, lowPassFrequency);
  FilterOnePole highpassFilter(HIGHPASS, highPassFrequency);

  lowpassFilter.input(analogRead(A0));
  highpassFilter.input(lowpassFilter.output());
  

  micValue = highpassFilter.output();
  
  
  delay(0.2);
}

if anyone has any insights I would be gratified

  delay(0.2);

If you can find the Playground, I'm sure you can find the Reference page, and learn about the proper use of delay(), with particular attention to what type of argument it takes.

So after you've bandpass filtered the signal, how do you plan to process the micValue signal to determine if the frequency is present or not?

What you want to compute isn't a filter, but a Fourier transform.

And even if delay() worked like that, you can't sample a 3kHz bandwidth signal with at 5kHz, that's below the Nyquist limit. Do you have external filtering applied to your signal? If you don't you'll get aliasing from high frequency content.

Jiggy-Ninja:
What you want to compute isn't a filter, but a Fourier transform.

If he were trying to do some sophisticated analysis, you might be right. But to simply detect the presence or absence of a few discrete frequencies, filtering will do the job just fine with FAR less overhead than an FFT - Simply apply a threshold to the filter outputs to determine whether the frequency is present or not.

Regards,
Ray L.

PaulS:

  delay(0.2);

If you can find the Playground, I'm sure you can find the Reference page, and learn about the proper use of delay(), with particular attention to what type of argument it takes.

You're right - I have to admit I'm fairly new to Arduino. So I could use delayMicroseconds() or using timer1(interrupt) or the micros() function. thank you

Jiggy-Ninja:
So after you've bandpass filtered the signal, how do you plan to process the micValue signal to determine if the frequency is present or not?

I don't really know - as I said I couldn't understand how to properly use the filters library and that is pretty much what I am asking for help.

Jiggy-Ninja:
And even if delay() worked like that, you can't sample a 3kHz bandwidth signal with at 5kHz, that's below the Nyquist limit. Do you have external filtering applied to your signal? If you don't you'll get aliasing from high frequency content.

As for that part, after my original post I researched some more and found the Nyquist limit and therefore I increased the sample rate to 9000 Hz
I don't have any external filtering, but the tone that I am looking for will be pretty loud in a rather quiet environment (with occasional other strong noises so I can't simply check for volume) - so I don't think aliasing will be a problem

RayLivingston:
If he were trying to do some sophisticated analysis, you might be right. But to simply detect the presence or absence of a few discrete frequencies, filtering will do the job just fine with FAR less overhead than an FFT - Simply apply a threshold to the filter outputs to determine whether the frequency is present or not.

Regards,
Ray L.

Could you explain further? how would I apply a threshold?

Google "The Scientist's and Engineer's Guide to Digital Signal Processing", and prepare to go down the rabbit hole. In the end, what you do is you keep the last N samples in an array and you do some magic on them involving complex numbers. I haven't managed to cut any code, yet. But that book (the chapters are available online for free, or you can buy a print edition) describes exactly the math behind digital filters.

Why not use a hardware filter that generates a pulse?

Assuming you're using the same microcontroller to control the robot, don't waste time doing floating point math.