Go Down

Topic: PulseIn, but then different? (Read 648 times) previous topic - next topic

Bob_Wannabe

Jun 29, 2012, 11:54 am Last Edit: Jun 29, 2012, 11:58 am by Bob_Wannabe Reason: 1
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? :)

Greetings from Holland. :)

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.

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.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

pekkaa

#2
Jun 29, 2012, 12:48 pm Last Edit: Jun 29, 2012, 01:31 pm by pekkaa Reason: 1
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

Bob_Wannabe

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... :P

Thanx again :)

lesto

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
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

DuaneB

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.

http://rcarduino.blogspot.com/2012/04/how-to-read-multiple-rc-channels-draft.html

Duane B

rcarduino.blogspot.com





Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

Nick Gammon


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:

http://www.gammon.com.au/interrupts
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

GoForSmoke

Ah Ha! Boy, that really frees me up for some ideas! Thanks Nick!
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up