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.
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;
}
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.
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.