 # Determine timing difference between two interrupts ATMega1284p

I setup two RFM69 on INT0 and INT1. The code works, because I am getting RSSI.

What I needed to do next is to read PD1 and PD3 and count their difference in reception time. But the code below does not work. Can anyone help?

``````   unsigned long difference = 0, timeout = 0, distance = 0;
boolean finished = false;
boolean oneFire = false;
boolean twoFire = false;
long setPoint1 = 0;
long setPoint2 = 0;

while ((oneFire == 0) || (twoFire == 0)) {
if (((PIND & (1<<PD3)) == 0) && !oneFire) {
oneFire = true;
setPoint1 = timeout;
}
if (((PIND & (1<<PD1)) == 0) && !twoFire) {
twoFire = true;
setPoint2 = timeout;
}
delayMicroseconds(1);
timeout++;
if (timeout > 19000)
break;
}
``````

Much oblgied,
Donald

Read this before posting a programming question

How to use this forum

http://snippets-r-us.com/

donaldhwong:
I setup two RFM69 on INT0 and INT1. The code works, because I am getting RSSI.

What I needed to do next is to read PD1 and PD3 and count their difference in reception time. But the code below does not work. Can anyone help?

``````   unsigned long difference = 0, timeout = 0, distance = 0;
``````

boolean finished = false;
boolean oneFire = false;
boolean twoFire = false;
long setPoint1 = 0;
long setPoint2 = 0;

while ((oneFire == 0) || (twoFire == 0)) {
if (((PIND & (1<<PD3)) == 0) && !oneFire) {
oneFire = true;
setPoint1 = timeout;
}
if (((PIND & (1<<PD1)) == 0) && !twoFire) {
twoFire = true;
setPoint2 = timeout;
}
delayMicroseconds(1);
timeout++;
if (timeout > 19000)
break;
}

``````

Much oblgied,
Donald
``````

That code won’t even compile much less work. I wonder what the REST of the code does?

We have an easy way to tell time differences down to 4 microseconds. Closer takes more code. The basic method follows:

Arduino has functions millis() and micros(). They return unsigned long msecs and usecs (rounded to 4) since start up. I will show with millis() but micros() works too.

unsigned long timeStart, timeDifference;

At some time during execution

timeStart = millis();

At a later time

timeDifference = millis() - timeStart;

Because we use unsigned variables this will ALWAYS work up to a difference of 49.71… days.
If using micros(), up to 71.58… minutes (4294967296 usecs), rounded to 4.

The usual use is for timing something to happen but you want time difference.

if ( millis() - timeStart >= desiredWait )
{
// time to make the donuts
}

I tested out the code with two ultrasonic sensors, it works fine.

But when I switch to monitor the two interrupts coming from two RFM69, it no longer works.

I strongly suspect that I initiated its action either too early or too late.

At the present time, I initiate it right after I get a RFM signal available on the interrupt.

Perhaps I should not use the interrupt pin. If so, then which pin?