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;
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 ?
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.
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.
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 !