PulseIn, but then different?

Hello gentlemen,

a few weeks ago I became the proud owner of both an Arduino Uno AND Mega. And as such I started programming in the "arduino" C-code. Most of which is relatively simple to understand, however, I keep running into a problem I can't seem to solve myself. And the most annoying thing about this problem is that it seems simple to solve... but I could be very wrong about that.

I'm trying to make a program that -in a while() loop- starts a timer with the negative flank of one inputpin and stops/saves value/resets the timer with the negative flank of another inputpin. I need this because I'm trying to measure time between two separate signals (the before mentioned inputpins) that enter a HIGH state a few hundred microseconds after eachother (this time varies between 0.15 ms and 6 ms).

I have taken a particualr interrest in the Interrupts that usually are mentioned when people on this forum want to make something similar, yet these don't seem to be leading me anywhere(?).

Also the StopWatchClass seemed to be perfect for this case but I can't get the stopwatch to start/stop at the right moment and sometimes not at all...

PulseIn worked perfectly for a different part of the program that measures RPM.

I was wondering whether anybody present on this forum might have one of those great "how-about-you-check-this-link" type of suggestions? :slight_smile:

Greetings from Holland. :slight_smile:

What pins are you trying to edge detect with? On an UNO the only ones you can are used by hardware serial which is used by the Serial library.

You could try setting your circuit up so that one event takes a single pin HIGH and the other grounds it back to LOW then use pulseIn to get time, which isn't perfect btw.

I'm not sure what the "negative flank" means but I'll guess it means the falling edge. The problem you described doesn't sound too complicated. You need two ISRs. The first ISR saves the timestamp into a variable (say t0) using micros(). Remember to use a volatile keyword when you declare the variable. The second ISR subtracts t0 from the current time. The result is the time between the signals.

Edit: typos

Thank you both for such a fast reply.

So I guess I'm gonna have to take another look at the Interrupts again then huh?
I'll give it another shot this weekend... :stuck_out_tongue:

Thanx again :slight_smile:

here you starting point link: http://arduino.cc/en/Reference/HomePage

and take a look at AttachInterrupt: http://arduino.cc/en/Reference/AttachInterrupt

using low-level register you can use interrutp on any pin, but "just" the "on change" one. but seems that you don't need this complexity, just in case

Hi,

This post will not give you the exact code you need but it does cover the most common pitfalls of using interrupts for the first time and ends with code for measuring three channels worth of pulse width data which you could easily adapt to take the rising edge from one pin and falling edge from another.

Duane B

rcarduino.blogspot.com

GoForSmoke:
What pins are you trying to edge detect with? On an UNO the only ones you can are used by hardware serial which is used by the Serial library.

No they aren't. Hardware serial uses pins 0 and 1, and the edge detecting pins are 2 and 3.

More information about interrupts:

Ah Ha! Boy, that really frees me up for some ideas! Thanks Nick!

Glad to help. I see the confusion now. attachInterrupt takes an argument of 0 or 1, but that is external interrupt 0, not pin 0.