How to measure the time between interrupts? (MPU6050)

Hello,

I would like to measure the time between the interrupts from my MPU6050 which is connected to an Arduino UNO R3.
I placed

deltaT = pulseIn(2,LOW);

after line 229 of the MPU6050 sample code: https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/Examples/MPU6050_DMP6/MPU6050_DMP6.ino

The ouput is beween 49675 and 49729 microseconds.
I would be very grateful if you could confirm or improve my solution.

Kind regards

I would be very grateful if you could confirm or improve my solution.

What is connected to pin 2? The pulseIn() function does not measure the time between interrupts. Placing a call to micros() in the interrupt function would allow you to record when an interrupt occurred. If you copied last time before setting this time, then you could determine the time between the two events.

The int Pin from the MPU6050 is connected to pin2 of my UNO.
I have tried to achieve your suggestion and this is my result:

unsigned long deltaT=0;
unsigned long now=0;
unsigned long lastTime=0;

// ================================================================
// ===               INTERRUPT DETECTION ROUTINE                ===
// ================================================================

volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
    now=micros();
    deltaT=now-lastTime;
    lastTime=now;
    mpuInterrupt = true;
}

Is this code correct?

Is this code correct?

If you don't intend to access deltaT, now, or lastTime from outside the ISR, yes. Otherwise, they need to be volatile, too.

Edit: No, wait. lastTime needs to be set to now BEFORE now is reset.

rookiebalboa:
Is this code correct?

Almost. Variable deltat at least needs to be volatile, because you will be reading it outside the ISR. Variable 'now' is better declared as a local variable. Like this:

volatile unsigned long deltaT=0;
unsigned long lastTime=0;

// ================================================================
// ===               INTERRUPT DETECTION ROUTINE                ===
// ================================================================

volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
    unsigned long now=micros();
    deltaT=now-lastTime;
    lastTime=now;
    mpuInterrupt = true;
}

Does micros() increment during an ISR ?
If not, the intermediate variable now is not needed because two calls to micros will return the same value

void dmpDataReady() 
{
  deltaT = micros() - lastTime;
  lastTime = micros();
  mpuInterrupt = true;
}

UKHeliBob:
Does micros() increment during an ISR ?
If not, the intermediate variable now is not needed because two calls to micros will return the same value

Yes it does increment during an ISR. Also, ISRs should be as fast as possible, and it is faster to call micros() only once.

Yes it does increment during an ISR

Speed aside, that blows the idea out of the water anyway.

Is it quicker to declare a local variable in the ISR than to use a global variable that has already been declared ?

UKHeliBob:
Is it quicker to declare a local variable in the ISR than to use a global variable that has already been declared ?

I don't believe it's ever slower, and it's very often faster and leads to more compact code. The value of a local variable may be kept in registers for some or all of its lifetime, so that it rarely (if ever) needs to be written to RAM.