Pages: [1] 2 3   Go Down
Author Topic: counting pulses per second  (Read 3789 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the most simple code for counting pulses per second? thanks
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 619
Posts: 33989
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use the pulse in function to measure the high and low periods. Add them together and take the reciprocal.
Logged

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

like this? PPS = 1/(pulseIn(1,HIGH) + pusleIn(1,LOW))
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 619
Posts: 33989
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes give it a try.
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pulseIn() returns a value in microseconds. Since using unsigned longs means no fractions, the value of that equation will always be zero.
Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Leviticus6432

I did a small code to count pulses. Check-it out at http://arduino.cc/forum/index.php/topic,75571.0.html one of my posts.

Just trying to help.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 619
Posts: 33989
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
pulseIn() returns a value in microseconds. Since using unsigned longs means no fractions, the value of that equation will always be zero.
So cast them as floats.
Logged

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

What would the code be to count pulses per second? thanks
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could have some logic to increment a pulse counter each time a pulse occurs, and some logic to determine when a second had elapsed and save the counter somewhere where you are holding 'current pulses per second', then reset the counter to zero.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

well it is for a speed sensor in a car and I really need it to be a little more accuate than checking the speed every second...
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 480
Posts: 18732
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How accurate?

The suggested solution doesn't "check the speed every second". It counts pulses, during a second. Which is what you asked for in the subject line and the first post.

You could count pulses, and check every half second how many you got (and double the number to estimate the number per second).

You could check every quarter of a second.

But if you recall your calculus you will hit a limit. You can't check over a zero period of time. And the shorter the period the less accurate the result will be.

For example, if you counted pulses over a microsecond, you might get one, or no pulses. This is hardly going to give you an accurate result.
Logged


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

You could, each time a pulse occurs, record the time, and compare this pulse's time with the previous pulse's time, to get an instantaneous speed reading (for the second and subsequent pulses).

There a number of ways to skin this particular cat. Pick one, and try to write some code to implement it. Post what you have, and describe why you chose that method, and what issues you are having, if you have issues.

This isn't the "do my homework for me" hotline. We've all done our own homework in the (distant) past. Your turn to do the same.
Logged

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

Quote
byte freqpin = 2;

unsigned long timePeriod = 0;
int frequency  = 0;
unsigned long ton = 0;
unsigned long toff = 0;
int lastin = 0;
int in = 0;
int count = 0;

void setup()

  pinMode(freqpin,INPUT);
  Serial.begin(9600);
  digitalWrite (freqpin,HIGH);
}

void loop()
{
  in = digitalRead(freqpin);
  ton = pulseIn(freqpin,HIGH);
  toff = pulseIn(freqpin,LOW);
  timePeriod = ton + toff; 
  if (in != lastin)
  {
    if (in == LOW)
    {
      count = (count + 1);
    }
  }
 
  if (count > 2)
  {
    frequency =(1000*count/timePeriod);
    timePeriod = 0;
    count = 0;
    Serial.println (frequency);
  }
lastin = in;


}





I was thinking something like that... It checks the hz every time the sample of 2 pulses has been recoded... at really low speeds it is not that accurate but should in theory work well for any speed over say 3 miles per hour.. But it doesn't work at all? what am I doing wrong   PS: the speed sensor pulses something like 1.7 times per mph   Thanks
Logged

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

By the way a very small amount was from Techone, I was reading through his post and started trying to make it work for me... My code is basically completely different than his now tho, only two or three lines and a few varible names remain the same. Just trying to give credit where credit is due lol
Logged

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

Code:
  ton = pulseIn(freqpin,HIGH);
  toff = pulseIn(freqpin,LOW);
This is not how to detect pulses from the sensor. It blocks until the signal transitions from HIGH to LOW and back HIGH, then records the time that LOW to HIGH took. Then, it waits for the signal to go from LOW to HIGH and then back to LOW. Polling the pin, and detecting when a transition occurs yourself, and, of course, noting the time, will produce more accurate results.

Logged

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