How would I make it count frequency?

Hello Arduino community!
Please, if this is the wrong section to post this let me know, since it's the first time I post here!

So!
I've made this piece of code:

int frek = 9; // Variable
int cnt; // Line Numbers
unsigned long frekvens; // Variable

void setup()
{
  pinMode(frek, INPUT); // Set pin 10 as INPUT
  Serial.begin(9600);        // Connect to Serial Port
  Serial.println("Frequency Counter"); // Headline
}

void loop()
{
frekvens = digitalRead(frek);
  Serial.print(cnt++);
  Serial.print(" Frekvens: ");
  Serial.print(frekvens);
  Serial.print("\n");
  delay(500);
}

And it can count HIGH and LOW.
But I need it to count frequency. The actual numeric value. Not just 1 and 0. I use a Duaminova (spelling?) board. It's a digital input. I am a novice at Arduino so please be as noob-friendly as possible in your guidance!

Thanks in Advance :slight_smile:

No one who van help me? =/

There is a frequency counter I think, in the Playground.

Is it the FreqCounter library?
I've tried to use it and I can't make it work.. :frowning:

You have to use the specified pin (pin 5, I think).
What sort of range of frequencies are you trying to measure?

The circuit is run by the Arduino boards 5v pin.
And I do use pin 5.

It just keeps returning 0

EDIT:
Can you help me make it count how many 1's it gets in per second and then display that as a number as I think that would be what I want it to do. I know it's a math formular that needs to be written:
Something like...for every x number of counts per second do print. Or something like that =/

I don't know if you guys know Lua.
But in Lua you can do stuff like what I try to do in the code below. Everytime the Digitalread recieves a 1 it should store this value in the array Pulses and it should count pulses every 100 ms for 1 second. That should give 10 pulses. It should take this and print it since that would be the frequency.

Can you guys help me with that?
If I run this piece of code it just starts counting random numbers which just goes sky high in no time :frowning:

int frek = 5; // Variable
int cnt; // Line Numbers for readability
unsigned long frekvens; // Variable

void setup()
{
  pinMode(frek, INPUT);
  Serial.begin(9600);        // Connect to Serial Port
  Serial.println("Frequency Counter"); // Headline
}

int Pulses[] = {};

void loop()
{
frekvens = digitalRead(frek);
  for (frekvens = 1; frekvens > 0;) {
    Serial.print(cnt++);
    Serial.print(" Frekvens: ");
    Serial.print(Pulses[0]);    
    Serial.println(frekvens);
    delay(1000);
  }
}

To calculate frequency, you must sample the number of transitions in a fixed period of time, divide by two (if you're sampling both transitions), and divide by the sample period.

I'll ask again - what sort of frequency range are you trying to measure?

(what's Lua?)

I suppose it's about 20 KHz.

http://www.lua.org/about.html
Very popular language for C and C++ applications.

applications.

What are they?

World of Warcraft for example.
Especially various games use a Lua API because it doesn't need to be compiled and a simple restart of the application can apply changes you've done to your lua files rather than compiling the whole thing again.

This is not really relevant to my question =/

If I use digialRead(); to get the 1's and then find out how many of those appears at a given time (say 1 second) then take that number and divide it by 2 etc..how would I write that into the Arduino?

I've rewritten the code again.
Now it counts a frequency. Just slowly. I need it to count for 1 second at a time.

int inPin = 5;
int cnt = 0;
float frequency = 0.0;

void setup()
{
      pinMode(inPin, INPUT);
      Serial.begin(9600);
      Serial.println("Frequency Counter");
}

long unsigned expiredTime = 0;
void loop()
{
      unsigned long inValue = digitalRead(inPin);
      if( inValue == HIGH )
            cnt++;

      if( expiredTime > 0 )
            frequency = (float)cnt / expiredTime;
      else
            frequency = cnt;


//      Serial.print("Expired Time: ");
//      Serial.print(expiredTime);
//      Serial.print(" seconds\n");

//      Serial.print("Number of 1s: ");
//      Serial.print(cnt);
//      Serial.print("\n");

      Serial.print("Frequency: ");
      Serial.println(frequency);
      

      expiredTime += 1;
      delay(1000);
}

Why not just count the low to high transistions in, say 100ms, and multiply the result by ten?

unsigned long inValue = digitalRead(inPin);

Isn't that a bit wasteful?

That's what I am asking for help with :o
And well that was just what I could think of so..

Any help with how it would look like would be appreciated.

What's a World of Warcraft?

Now you are just trolling =/

Let me ask how you would implement a frequency counter in this scripting language.

Set a peg in the ground "now".
Reset counter.
Has 100ms elapsed since "now"?
No: is the line state HIGH?
Yes: was the last state low?
Yes:increment counter.
Yes (100ms has elapsed): calculate frequency.

(I don't know what a World of Warcraft is either!)

It's a game.
Look it up.

What you say I should do I've always known that I should do it that way. I just don't know how to express that in code. That's the problem. I am a bit novice at this.

[uncompiled, untested, incomplete]

unsigned long now = millis (); //Set a peg in the ground "now".
bool lastState = digitalRead (inputPin);
int counter = 0; // Reset counter.
while ((millis () - now > 100)){ // Has 100ms elapsed since "now"?
 bool currentState = digitalRead (inputPin);
 if (currentState) { //No: is the line state HIGH?
    if (!lastState) {  //Yes: was the last state low?
     counter++; // Yes:increment counter.
    }
  }
  lastState = currentState;
} 
//(100ms has elapsed): calculate frequency.

Could you post the Lua (?) solution, please?
I've never heard of it either (I'm a systems programmer, and games rarely impinge on my world), and I'd be interested in the contrast.

In function 'void loop()':
error: 'currentState' was not declared in this scope

int inPin = 5;
int cnt = 0;
float frequency = 0.0;

void setup()
{
  pinMode(inPin, INPUT);
  Serial.begin(9600);
  Serial.println("Frequency Counter");
}

long unsigned expiredTime = 0;
void loop()
{
  unsigned long now = millis (); //Set a peg in the ground "now".
  bool lastState = digitalRead (inPin);
  int counter = 0; // Reset counter.
  while ((millis () - now > 100)) // Has 100ms elapsed since "now"?
    bool currentState = digitalRead(inPin);
  if (currentState) { //No: is the line state HIGH?
    if (!lastState) {  //Yes: was the last state low?
      counter++; // Yes:increment counter.
    }
  }
  lastState = currentState;
}
//(100ms has elapsed): calculate frequency.

When this is over I will gladly try to write a lua approach.