Read a freqency from an analog pin

void loop()
{
   long getFrequency(A0);
   { 
     #define SAMPLES 4096 
     long freq = 0; 
     for(unsigned int j=0; j<SAMPLES; j++)
     {
        freq+= 500000/pulseIn(A0, HIGH, 250000);
     } 
     frequency = freq/SAMPLES;
     if (frequency <1)
     {
       digitalWrite(5, HIGH);
       digitalWrite(11,HIGH);
     }
     if (frequency >=1)
     {
       digitalWrite(12, HIGH);
       digitalWrite(15,HIGH);
     }              
  }
}

Without your atrocious indenting, and with curly braces every where, this is your loop function. So, what exactly is the function prototype in the loop() function for? You don't call the function anywhere, nor do you implement it, so get rid of it. Then, the { and } that follow it are useless.

You really don't understand what frequency means, do you?

Now ,it is correct?

#include <FreqCounter.h>

double frequency;


void setup() {
  pinMode(5,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(15,OUTPUT);
  pinMode(16,OUTPUT);
  pinMode(17,OUTPUT);

}



void loop() {
  long getFrequency(A0);
   { 
     #define SAMPLES 4096 
     long freq = 0; 
     for(unsigned int j=0; j<SAMPLES; j++)
     {
        freq+= 500000/pulseIn(A0, HIGH, 250000);
     } 
     frequency = freq/SAMPLES;
     if (frequency <5000)
     {
       digitalWrite(5, HIGH);
       digitalWrite(11,HIGH);
     }
     if (frequency >5000)
     {
       digitalWrite(12, HIGH);
       digitalWrite(15,HIGH);
     }              
  }
  
}

What about turning the LEDs off?
What if the average is 5000?

Now ,it is correct?

You're the one with the hardware, you tell us.

This is a very strange posting thread, maybe well on it's way of becoming a classic. :wink:

Lefty

florin:
Now ,it is correct?

#include <FreqCounter.h>

double frequency;

void setup() {
  pinMode(5,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(15,OUTPUT);
  pinMode(16,OUTPUT);
  pinMode(17,OUTPUT);

}

void loop() {
  long getFrequency(A0);
  {
    #define SAMPLES 4096
    long freq = 0;
    for(unsigned int j=0; j<SAMPLES; j++)
    {
        freq+= 500000/pulseIn(A0, HIGH, 250000);
    }
    frequency = freq/SAMPLES;
    if (frequency <5000)
    {
      digitalWrite(5, HIGH);
      digitalWrite(11,HIGH);
    }
    if (frequency >5000)
    {
      digitalWrite(12, HIGH);
      digitalWrite(15,HIGH);
    }             
  }

}

Did you actually make any changes...?

For your reference:

Wikipedia:
Frequency is the number of occurrences of a repeating event per unit time. It is also referred to as temporal frequency. The period is the duration of one cycle in a repeating event, so the period is the reciprocal of the frequency. For example, if a newborn baby's heart beats at a frequency of 120 times a minute, its period (the interval between beats) is half a second.

... and ...

Wikipedia:
In mathematics, a multiplicative inverse or reciprocal for a number x, denoted by 1/x or x−1, is a number which when multiplied by x yields the multiplicative identity, 1. The multiplicative inverse of a fraction a/b is b/a. For the multiplicative inverse of a real number, divide 1 by the number. For example, the reciprocal of 5 is one fifth (1/5 or 0.2), and the reciprocal of 0.25 is 1 divided by 0.25, or 4. The reciprocal function, the function f(x) that maps x to 1/x, is one of the simplest examples of a function which is self-inverse.

Oops, missed the superfluous function prototype. :*

I am very new at programming. I want to understand what is wrong, but i can't. =(
That's the only mistake? As I turned off the LED-s? The circuit does not light at all, I put any frequency. Should not light up LEDs? =(

ps: sorry for bad english.

florin:
I am very new at programming. I want to understand what is wrong, but i can't. =(
That's the only mistake? As I turned off the LED-s? The circuit does not light at all, I put any frequency. Should not light up LEDs? =(

ps: sorry for bad english.

There is one more mistake. The program is wrong. Just plain wrong. First steps is to read up on the basic syntax of C so that you can at least have a clue what you're doing.

Then, you need to properly define what it is you want to achieve.

From that you can break it down into a number of actual steps to achieve your goal.

You can then convert those steps into C code in the form of a program.

This is called Top Down Programming.

Wikipedia:
Top-down approaches emphasize planning and a complete understanding of the system. It is inherent that no coding can begin until a sufficient level of detail has been reached in the design of at least some part of the system.

Can make for me a correct program with this library?

florin:
Can make for me a correct program with this library?

Yes, if you pay me. £100/hour. Will it do what you want? Who knows? We still don't really know what you want it to do. Do you really know what you want it to do?

I want to read frequency. It's simple. :astonished:

What do you want to read frequency using? What is the form of the signal? What is the range of frequencies you are interested in? What do you want to do with those frequencies when you have read them?

A microphone read frequences. If sound is below, blink a led. If sound is sharpen , blink another led. That is all.

What amplification have you got between the microphone and the Arduino?

It is not that simple. A microphone reads all frequencies. If you know there is only one frequency, you can amplify the signal to clip and become a square wave and use the pulse measurement technique mentioned earlier. If the signal is buried in noise, you might have to use an FFT to resolve it.

florin:
A microphone read frequences. If sound is below, blink a led. If sound is sharpen , blink another led. That is all.

You still haven't answered our questions.

What are you getting the signal with?

  • Please provide schematic

What is the format of the signal

  • Oscilloscope traces would be best, or describe the signal in detail

What frequencies are you interested in?

  • Is the signal a single frequency (sine wave), and what is the minimum / maximum frequency?
  • Is the signal a composite signal, and you are interested in a "peak" frequency within the background noise? If so, what is the minimum / maximum frequency?

Until you provide this basic information we cannot help you.

I don't need the full code. I want only a line , wich read frequency...
Please help.

Try this
http://interface.khm.de/index.php/lab/experiments/frequency-measurement-library/
http://www.arduinoos.com/wordpress/?p=1022 8)

The microphone is connected to Analog 0. Max frequency is 20 Mhz. I separe in 2: "<1Mhz" and ">1Mhz" . Then i have 2 LED's. If first conditional is true, blink first led. If second condisitonal is true, blink the second led. XD

Thanks in advance.

The microphone is connected to Analog 0. Max frequency is 20 Mhz. I separe in 2: "<1Mhz" and ">1Mhz"

Please, get real.

Everything will go in the <1MHz category.

florin:
The microphone is connected to Analog 0. Max frequency is 20 Mhz. I separe in 2: "<1Mhz" and ">1Mhz" . Then i have 2 LED's. If first conditional is true, blink first led. If second condisitonal is true, blink the second led. XD

You have a microphone that works up to 20mHz? How much did it cost you?