Go Down

Topic: Read a freqency from an analog pin (Read 4077 times) previous topic - next topic

dhenry

[quite] I want only a line , wich read frequency...[/quote]

Try this:

Code: [Select]
  freq=freq_read(pin); //read frequency on pin

You are welcome.

PaulS

Code: [Select]
1.long getFrequency(int pin) {
2.#define SAMPLES 4096
3.long freq = 0;
4.for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, HIGH, 250000);
5.return freq / SAMPLES;
6.}

Is right there on the page you linked to. Surely you can figure out how to call this function.

CrossRoads

Isn't this simple enough?
Code: [Select]

long getFrequency(int pin) {
#define SAMPLES 4096
long freq = 0;
for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, HIGH, 250000);
return freq / SAMPLES;
}
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

florin

Thanks for answers.  :)

I write the code below:
Code: [Select]

#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 <1) { digitalWrite(5, HIGH);
                   digitalWrite(11,HIGH);}
if (frequency >=1) { digitalWrite(12, HIGH);
                   digitalWrite(15,HIGH);}
                   
}


 
}



No errors, but circuit doesen't work. I have mistakes in the code?

majenko

Quote

Code: [Select]

if (frequency <1) { digitalWrite(5, HIGH);
                   digitalWrite(11,HIGH);}
if (frequency >=1) { digitalWrite(12, HIGH);
                   digitalWrite(15,HIGH);}


So how do those LEDs ever turn off then?  You are turning them on, but never off.

AWOL

The only case where "frequency" is less than one is when it is zero.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Code: [Select]
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?

florin

Now ,it is correct?
Code: [Select]

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


 


AWOL

#23
Nov 14, 2012, 07:16 pm Last Edit: Nov 14, 2012, 07:20 pm by AWOL Reason: 1
What about turning the LEDs off?
What if the average is 5000?

Quote
Now ,it is correct?

You're the one with the hardware, you tell us.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

retrolefty

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

Lefty

majenko

#25
Nov 14, 2012, 07:24 pm Last Edit: Nov 14, 2012, 07:30 pm by majenko Reason: 1

Now ,it is correct?
Code: [Select]

#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:

Quote from: 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 ...

Quote from: 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.

AWOL

Oops, missed the superfluous function prototype.  :*
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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.

majenko

#28
Nov 14, 2012, 07:39 pm Last Edit: Nov 14, 2012, 07:43 pm by majenko Reason: 1

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.

Quote from: 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.

florin

Can make for me a correct program with this library?

Go Up