Pages: 1 [2] 3   Go Down
Author Topic: counting pulses per second  (Read 3974 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 do you mean by saying "It blocks..."?  and also what is "polling the pin"?   sorry, just not very familliar with coding lingo lol
Logged

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

There are blocking functions and non-blocking functions. Serial.available() is a non-blocking function. It gets a value and returns immediately. delay() is a blocking function. It does not return until the time has expired.

pulseIn is a blocking function. It does not return until something has happened.

Doing something in loop(), where there are no delays in loop(), is called polling. If, on every pass through loop(), you ask the pin if it is HIGH (or LOW, as appropriate), that is polling. If you do it often enough, you won't miss any signals.
Logged

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

well with that idea would it be able to handle the number of pulses at 100mph... I re-did my math and its 1.37 pulses per second per mph...  So 100mph would be 137 pulses in a second 
Logged

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

137 pulses per second is an eternity between pulses. However, I question your setup, if there is only one pulses per 1.37 miles per hour. That is nowhere near enough pulses to give any kind of accuracy.
Logged

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

well its off of a toyota pickup instrument cluster. There is a built in speed sensor on the back of the speedometer (everything else is mechanical)  The speed sensor is for the cruise control which I believe doesn't work until over 30mph in factory applicaions. That would be like 512 pulses per second. I am not using it for cruise control though. I need it to at least start being accurate by 5mph which is almost 7 pulses per second. Assuming the amount of time inbetween pusles is the same as the width of the pulse, can't you just count the width of a pulse and... idk do something with that. (i tried dividing by 1000 and then dividing by 2)  I wrote a code but i think it sucks in more than one aspect. I'm sure you'll all agree.


Quote
volatile int state = LOW; //what is this? do i need it?"
long pulseStart = 0;
long pulseLength = 0;
long currentMillis = 0;
int HZ = 0;
int PPS = 0;
byte freqpin = 2;

void setup()
{
  attachInterrupt(1, down, FALLING); //can I use 2 interrupts like this?
  attachInterrupt(1, up, RISING);
  Serial.begin(9600);
  Serial.println ("HZ");
  pinMode(freqpin,INPUT);
  digitalWrite (freqpin,HIGH);
}

void loop()
{
  unsigned long currentMillis = millis(); //should this be placed in setup?
}

void down()  //can I just name it down or does it have to be a reconized term
{
pulseStart = currentMillis;
Serial.println ("DOWN");
}

void up()
{
  Serial.println ("UP");
pulseLength = (currentMillis - pulseStart);
HZ = (1000 / pulseLength);
PPS = (HZ / 2);            //sounded good in theory to me but it doesn't ever write anything other then HZ of course
Serial.println(PPS);
}
« Last Edit: November 24, 2011, 11:29:30 am by AWOL » Logged

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

Paul, I was going to attempt your polling idea, but the code is actually quite long and I was worried it wouldn't catch all the pulses. Now you say that I'm actually not working with very many pulses in a second so I might give it a shot. thanks
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34519
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please modify that code and delete the quite brackets and replace it with the # (code) ones. Then we will have code we can compile. Also this might change the comments from " to //
Logged

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

got it
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34519
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
got it
No try again. Select all the code and hit the # icon (the one next to the quote)
Logged

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

Oh, and the pulses are a high to low pulse. Note that the pull up resistor is activated.  Also I'm sure I'm delivering pulses to the Arduino as I am using an oscilloscope.
Logged

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

Quote
got it
No try again. Select all the code and hit the # icon (the one next to the quote)

so confused I thought I just hit copy for forum in arduino?
Logged

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

Code:
volatile int state = LOW; //what is this? do i need it?"
long pulseStart = 0;
long pulseLength = 0;
long currentMillis = 0;
int HZ = 0;
int PPS = 0;
byte freqpin = 2;

void setup()
{
  attachInterrupt(1, down, FALLING); //can I use 2 interrupts like this?
  attachInterrupt(1, up, RISING);
  Serial.begin(9600);
  Serial.println ("HZ");
  pinMode(freqpin,INPUT);
  digitalWrite (freqpin,HIGH);
}

void loop()
{
  unsigned long currentMillis = millis(); //should this be placed in setup?
}

void down()  //can I just name it down or does it have to be a reconized term
{
pulseStart = currentMillis;
Serial.println ("DOWN");
}

void up()
{
  Serial.println ("UP");
pulseLength = (currentMillis - pulseStart);
HZ = (1000 / pulseLength);
PPS = (HZ / 2);            //sounded good in theory to me but it doesn't ever write anything other then HZ of course
Serial.println(PPS);
}
Logged

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

Quote
Assuming the amount of time inbetween pusles is the same as the width of the pulse, can't you just count the width of a pulse and... idk do something with that.
I would not think that this is a valid assumption.

Code:
  attachInterrupt(1, down, FALLING); //can I use 2 interrupts like this?
  attachInterrupt(1, up, RISING);
Only the up function will be called. When the pin changes state, THE interrupt function is called if the change is the one desired.

You would need to have the same signal going in to two pins (2 and 3), and have the rising edge trigger the interrupt handler on one pin and the falling edge trigger the interrupt handler on the other pin.

Or use the CHANGE option, and detect, in the handler, whether the edge was rising (pin is now HIGH) or falling (pin is now LOW).
Logged

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

well I suppose the interrupts are fixable... but it would be worthless anyway if my assumption about the width of the pause is invalid, wouldn't it be pointless anyway?
« Last Edit: November 24, 2011, 02:44:25 pm by Leviticus6432 » Logged

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

Paul, I reread your post and noted that you said only the up would work? You realize it is normally high 5v and drops to 0v for a pulse? Wouldn't only the down function work. Not trying to pick at you or anything, Im just really having a hard time grasping the interrupt concept and that confused me. If I am wrong in thinking the interrupts work on a sort of first come, first serve basis please explain. Also If i need to flip the pulse thats fine, the way I have it set up now just looks like a cleaner wave in my oscilloscope.
Logged

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