need help with accurate frequency measurement

pulseInLong() is verry similar to pulseIn(), but is supposed to work better with enabled interrupts. Since I have a JK trigger connected at the output of the optocoupler one HIGH or LOW period is equal to the full period of the signal from the Lm331, seen in the picture below


Yellow line is the output from the lm331,
blue line is from the optocoupler
and pink line is from the output of the JK trigger and is connected to pin 30 of the Arduino MEGA.