Pages: [1]   Go Down
Author Topic: PulseIn, but then different?  (Read 541 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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? smiley

Greetings from Holland. smiley
« Last Edit: June 29, 2012, 04:58:32 am by Bob_Wannabe » Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4028
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Examples can be found in your IDE.

Finland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: June 29, 2012, 06:31:51 am by pekkaa » Logged

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

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... smiley-razz

Thanx again smiley
Logged

0
Online Online
Shannon Member
****
Karma: 117
Posts: 10112
:(){:|:&};: TOX id: fcb8e918bef08581e23f6ddf9d4dba77697c25b217bf372736ed959a95fde36df5b8c5b90fbb
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

my Arduino code: https://github.com/lestofante/arduinoSketch
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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





Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4028
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Examples can be found in your IDE.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: