Pages: 1 [2] 3 4 5   Go Down
Author Topic: Read a freqency from an analog pin  (Read 2942 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Try this:

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

You are welcome.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 512
Posts: 26202
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Isn't this simple enough?
Code:
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;
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for answers.  smiley

I write the code below:
Code:
#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?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25623
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The only case where "frequency" is less than one is when it is zero.
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now ,it is correct?
Code:
#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);
     }             
  }
 
}


 

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25623
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: November 14, 2012, 01:20:35 pm by AWOL » Logged

"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.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17253
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Lefty
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Now ,it is correct?
Code:
#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.
« Last Edit: November 14, 2012, 01:30:35 pm by majenko » Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25623
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oops, missed the superfluous function prototype.  smiley-kiss
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am very new at programming.   I want to understand what is wrong, but i can't. smiley-cry
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?  smiley-cry

ps: sorry for bad english.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am very new at programming.   I want to understand what is wrong, but i can't. smiley-cry
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?  smiley-cry

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.
« Last Edit: November 14, 2012, 01:43:21 pm by majenko » Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can make for me a correct program with this library?
Logged

Pages: 1 [2] 3 4 5   Go Up
Jump to: