Thank you so much for helping me out here but actually your code is giving me similar results as I was getting before the problem is that I have to measure the pulse widths between 10 to 15 microseconds accurately but your code as well as my code gives 8 or 12 microsecond
The micros() was just a reference timing, find below a stripped pulsewidth meter for periods smaller than 24576 micros based on a tight software loop. One iteration takes 6/16 microsecond so we can 'measure" the following steps between 10 and 15 usec. So approx 15 steps.
26 9,750
27 10,125
28 10,500
29 10,875
30 11,250
31 11,625
32 12,000
33 12,375
34 12,750
35 13,125
36 13,500
37 13,875
38 14,250
39 14,625
40 15,000
41 15,375
//
// FILE: PulseWidthMeter.pde
// AUTHOR: Rob Tillaart
// DATE: 2012-mar-20
//
// LINK: http://arduino.cc/forum/index.php?action=post;topic=96971.0
//
unsigned int count = 0;
void setup()
{
Serial.begin(9600);
Serial.println("pulse width meter 0.2");
pinMode(3, INPUT);
}
void loop()
{
count = 0;
while ((PIND & B00001000) == B00000000); // wait for HIGH
while ((PIND & B00001000) == B00001000) count++; // start counting until LOW
float usec = 1.0 * count * 6/16;
Serial.print("CNT: ");
Serial.println(count, DEC);
Serial.print(" equals ");
Serial.print(usec, 2);
Serial.println(" microseconds.");
delay(1000);
}
With the hardware timer more accuracy should be possible.