Pin interrupts to measure time with Micros()

Hi there,

I have made a project to measure the speed of a projectile form an airsoft (bb) rifle. At present the main Loop constantly polls the fist in a series of 2 infrared gates, when the first infrared gate is triggered by the projectile the program records the time and waits for the second gate to be triggered. when this happens it records the second gate entry time, some simple maths and I have a projectile velocity.

I have this working well and now ant to move the program on

I want to be able to do other things with the program like change the 'mode' to measure energy in joules in stead of speed, I figure that using an interrupt for the first infrared gate would be better that constantly polling the gate in the main loop. But i'm confused with interrupts and the Microsec() function - can I use and interrupt 2 record 1 time, wait for the second gate and record the second time?

Thanks for your help.

Mark

Yes, you can. You can use attachInterrupt() or even pin change interrupts.

[ Note that some Arduino boards don't have quartz crystals and their
time values can be +/- 0.3% or so should that matter. ]

Thank you very helpful,

In my 'prototyping' / breadboarding I just use the arduino boards with the 0.3% inaccuracy although I have purchased parts - including a crystal- for putting together a more permanent solution. I must admit I didn't know that though it was purely co-incidence!

you say I can use attachInterrupt() or even pin change interrupts - are these note the same? Sorry if that's a silly question - I haven't used interrupts before so they are a bit foreign to me!

The Arduino software gives potted access to the the INT0 and INT1 interrupts
using attachInterrupt. Pin change interrupts are configured port-by-port and aren't
directly supported but there will be libraries and tutorials out there. On the Uno
and similar every pin can be used with pin change interrupts (not true on the Mega,
note).

The Due has a completely different set up and all pins can be handled via attachInterrupt
using the pin number (rather than interrupt channel number).

thank you very much, very informative!

Can't wait to try it out

Yes, you can do a lot using the low level AVR functions.

For example, once you figure out how to set up Timer1 @ 16MHz (prescaler =1), you can just read TCNT1 from within the interrupt routine (ISR) and get the time resolution down to 0.0625 uSecs (62.5 nSecs).

Using the standard Arduino clock which is on Timer0, you get a resolution of 'only' 4 uSecs, via micros() {or directly via TCNT0}.