Pages: [1]   Go Down
Author Topic: How to measure a digital pulse ?  (Read 643 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I want to measure the time one of the digital input pins is high (or low). I'm doing the following:

Code:
estat=digitalRead(4);
do {} while(digitalRead(4)==estat); // wait until changes

t1=millis(); // measure the change
do {} while(digitalRead(4)==!estat);
t2=millis();
t=t2-t1;

Serial.println(t);

My questions are:
1-How accurate you think that is? The time for calling millis() and the digital read eat time, so..
2-How can I do it for intervals below 1ms?

Thanks
Logged

Brisbane, Australia
Offline Offline
God Member
*****
Karma: 1
Posts: 593
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
2-How can I do it for intervals below 1ms?
You need to do something like counter++; every loop.
That creates your own arbitrary unit which is the amount of time one loop takes to complete.
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try the pulseIn() function: http://www.arduino.cc/en/Reference/PulseIn
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Try the pulseIn() function: http://www.arduino.cc/en/Reference/PulseIn

Thanks!! smiley

What if the pulse already began when you call the function?
ie, the pin is already high and you want to know how long does it take to go low.
It seems this is not supported, where can I find the library code to change it ?
Logged

Pages: [1]   Go Up
Jump to: