microsecond timer between two events

Hello

I am looking for a way of timing a air rifle bullet over a distance of 0.1 m. The velocity is in the neighbourhood of 200 m/s, so the time is 1/2000 sec.
If the resolution is going to be 4 m/s at that speed, the time is only 1/100 000 of a sec.
It that possible?

I am going to look for pin4 and 5 to go from high to low and meassure the time between that.
Output to a LCD. I have it working with millis() from the Arduino Playground - Stopwatch , but I need a better timer.

Any help?

To get 10 us resolution you're going to need some efficient programming. I'd suggest you write your timing routine in assembly to make it optimally tight, and I'd also suggest you disable the timer0 overflow interrupt that drives millis() and delay() to avoid having it mess up your timing. If you did this you could probably actually get around 1 or 2 us resolution, depending on the response time of your sensors.

At any rate, you want to look into using the hardware timers on the mega168 (see the mega168 datasheet for more information).

  • Ben

Check out input capture on timer1, all the timing is done in hardware so you won't need to worry about assembler. There is some code [u]posted here[/u] that uses this to measure pulses from a radio control transmitter that is accurate to a half a microsecond. Your application is much simpler but I hope that code helps as an example to see how the info in the datasheet on input capture can be turned into arduino code.

Thanks for your reply.

I tried some code based on timer0_overflow_count that I found on the forum, and a loop with a delay(1) instead of pressing two buttons with a very short interval repeatedly.

extern volatile unsigned long timer0_overflow_count;
unsigned long hpticks (void)
{
return (timer0_overflow_count << 8) + TCNT0;
}
...

and a loop with two timers on each side of a delay of 1ms;
time1 = hpticks();
time3 = millis();
delay(1);
time2 = hpticks();
time4 = millis();
//calculate time
spe=(time2-time1); //hm, unit??? usec?
spe2=(time4-time3); //hm, unit??? msec

I got the following response for spe and spe2;
87 1
139 1
171 1
164 1
120 1
45 1
194 1
46 1
34 1
197 1
44 1
217 1
129 2
255 1
87 2
204 1

The millis() timer is mostly 1, as expected, but the hpticks() does not give a usable result.

ok I am doing the same thing (basically). However I don't believe that I am following your code. I am using 2 led's and 2 photoresistors. Did you find something better? and could you kindly share your code for me to learn from :slight_smile: Thanks.

.dok

Sorry, no success in timing the bullet. I ended up with a flash delay system for high speed photo... ::slight_smile:
http://karlander.net/projekt/arduino-flash-trigger.html

Haha, well thanks anyway. I'm using mine for my potato cannon over the span of 1ft. I'm expecting anywhere between 450 and 700fps. I think i have it working actually. We will find out tomorrow!

.dok