[SOLVED] Arduino, too slow for precise time measurements ?

Hi,
I'm trying to measure a small time period with my Arduino UNO.
Here is the process :
Both pins 7 and 8 are HIGH.
Pin 7 turns LOW, a small time passes, then pin 8 turns LOW.
And I'm trying to measure this small time. It is usually between a little less than a millisecond and 10ms. So I have to measure it in microseconds.
Here is the code I'm currently using :

unsigned long start;
unsigned long stop;
long time;
while(digitalRead(7)==HIGH) {
    start=micros();
}
while(digitalRead(8)==HIGH) {
    stop=micros();
}
time=stop-start;
Serial.print(time);

But when the delay becomes less than 100ms, the print begin to show what seems to be random values.
So I'm guessing that the digitalRead function takes more than 100ms ? Am I correct ?
Do you see how I can improve my code to work for much lower times ?
Or do I have to buy a faster board ? If so, which one ?

Thank you very much in advance.

There's hardware that can help you here - it's called "Input Compare". It's built in to your chip and is designed for measuring the time between external events.

I would write

uint32_t start, stop;

while (digitalRead(7) == HIGH); 
start = micros();
while (digitalRead(8) == HIGH); 
stop = micros();
time = stop - start;
Serial.print(time);

the constant calls to micros are not needed

Thank you for your answer !
I looked up for "Input Compare" and I've found the bitRead function. So I used it as follow :

do {
    time_start=micros();
} while(bitRead(PINF,5)==1);
do {
    time_stop=micros();
} while(bitRead(PINF,6)==1);

(I now use do...while because sometimes the input is already LOW)
Is this code better ?
After some tests, I don't think it is faster. If I connect both pins to GND (LOW) then run my sketch, time is equal to 1492 microseconds (1.5ms).
When I run the old sketch under the same conditions (with digitalRead functions and do...while), time is equal to 4 microseconds.

Am I doing something wrong ?
Thank you again.

armandkd:
...

The granularity of micros is 4 micro seconds, so whatever you measure it will be a multiple of 4.

The code example keeps on setting start and stop while there is no reason. The call to micros() itself takes a few micros() itself.
So your timing will be less accurate. Please give my code above a try, for speed you may replace the digitalRead() with the bitRead construction.

do {
    time_start=micros();  // takes ~4 micros
} while(bitRead(PINF,5)==1);  // compare takes ~ 1 micro
so every loop takes 5 micros

==>
while(bitRead(PINF,5)==1);  // takes 1 micro per loop
time_start=micros();  // takes 4 micros
while(bitRead(PINF,6)==1);
time_stop=micros();

so the pin is tested 5 times more than the above example

Sorry I didn't see your answer before posting my reply.
And yes you're right ! Calling micros only once is better !
So I tried your last code (with bitRead). It is faster than the previous ones. I think this measurement is accurate enough (only a few microseconds won't do much).
This subject is now solved thanks to you two.
Thank you again for your rapidity and your very good ideas ! You helped me a lot !

Wow, lots of posts ... I'm too slow :wink:
Another variation (uses flag to prevent test interruption and falling conditions to trigger start and stop):

// constants
const byte led = 13;

// variables
byte pin7, pin8, pin7previous, pin8previous, runflag;
unsigned long tstart, tstop, ttime;

void setup() {
  Serial.begin(115200);
  pinMode(led, OUTPUT);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
}

void loop() {
  pin7 = digitalRead(7);
  pin8 = digitalRead(8);

  if ((pin7 == LOW) && (pin7previous == HIGH) && (runflag == 0)) {
    tstart = micros();
    runflag = 1;
    Serial.print("Testing...");
  }
  if ((pin8 == LOW) && (pin8previous == HIGH) && (runflag == 1)) {
    tstop = micros();
    ttime = tstop - tstart;
    runflag = 0;
    Serial.print("done. Elapsed microseconds = ");
    Serial.println(ttime, DEC);
  }
  pin7previous = pin7;
  pin8previous = pin8;
}

@dlloyd
in your code you probably mean to use && (logical and) instead of & (bitwise and)
Although the latter works (by accident)

Ha .. you've mentioned that before somewhere (It takes me a few times to get it right).
Corrected (thanks).