Optogates for coilgun

Hi guys,
I am currently working on 2 stage coilgun and I need to measure speed of projectiles. In first version Ive just used ide functions like digitalRead() or digitalWrite() in main loop but it was slow for me because window for switching is arround 1ms. It worked perfectly for lower speeds.

So I decided to manage swtihching coils with interrupts and changed my functions to more faster AVR style.

Now my problem... Switching inside interupts work great every coil fires at time but I also want to know how long was every optogate dimmed (for determinig effiency). That function worked OK when I was not using interrupts and after every shot effiency and speed for every coil was displaed on LCD. But now it works realy weird like it counts time only in itnterrupt conditon or something like that and sometimes is my "startTime" higer than "endTime" which is even more weird :smiley:

I am using libary "eRCaGuy_Timer2_Counter.h" to get 0,5us resolution of func "timer2.get_count()" for more precise results.

Is there any possibity that interrupts somehow afects timer2 or something like that? Or maybye using that libary is wrong?

Thanks for anny help :wink:

PS. Program runs on arduino mega.

Coilgun_for_arduino.ino (3.93 KB)

math.ino (1.11 KB)