Go Down

Topic: counting pulses per second (Read 4347 times) previous topic - next topic

Leviticus6432

what do you mean by saying "It blocks..."?  and also what is "polling the pin"?   sorry, just not very familliar with coding lingo lol

PaulS

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.

Leviticus6432

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 

PaulS

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.

Leviticus6432

#19
Nov 24, 2011, 04:38 pm Last Edit: Nov 24, 2011, 05:29 pm by AWOL Reason: 1
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);
}

Leviticus6432

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

Grumpy_Mike

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 //


Grumpy_Mike

Quote
got it

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

Leviticus6432

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.

Leviticus6432


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?

Leviticus6432

Code: [Select]
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);
}

PaulS

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: [Select]
  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).

Leviticus6432

#28
Nov 24, 2011, 08:25 pm Last Edit: Nov 24, 2011, 08:44 pm by Leviticus6432 Reason: 1
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?

Leviticus6432

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.

Go Up