A quick PulseIn question

I haven't got my setup, well, set up yet so I can't test this but can I use PulseIn twice in a row to calculate duty cycle?

highPulse = pulseIn(InputPin, HIGH); lowPulse = pulseIn(InputPin, LOW); dutyCycle = highPulse/lowPulse;

The pulses are from a car engine so will not vary wildly between pulses so "missing" one won't cause too much of an issue.

Thanks in advance, Easty.

Yes that should work ok.


Thanks Lefty.

From http://www.arduino.cc/en/Reference/PulseIn

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing.

After your

highPulse = pulseIn(InputPin, HIGH);

the pin will be LOW. When you start your

lowPulse = pulseIn(InputPin, LOW);

the pin will still be LOW. But that second line of code waits for the pin to [u]go[/u] LOW, not to [u]be[/u] LOW. The pin will have to go HIGH again before it goes LOW again, which is when that second line of code will start its timer. Thus you will miss measuring a LOW pulse and the next HIGH pulse before the pin drops to LOW again.

That may be Ok with you, since you say missing one won't cause too much of an issue, but will missing two out of three be a problem for you?

See http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1250557703 for an alternate explanation of what I tried to convey above. That post has some code that may suit your situation better than pulseIn() does.

[edit]I originally predicted you would miss half the pulses. You will actually miss two out of three. I edited the original text above to reflect the actual situation. Text I added or changed is in italics above.[/edit]

Thanks TBAr, that's kind of what I thought from what I read in the Reference section.

Although it's not that important in my project if I miss 2/3, it's definately worth noting for any future projects!

I will definately have a look at the other thread in case I can use that instead.