Speed of motor with arduino and encoder

Can you print out the values of staT and stoT, or at least the difference between them?

I'm not measuring speed - all I care about is position so it's just a matter of counting pulses.

wildbill:
Can you print out the values of staT and stoT, or at least the difference between them?

I'm not measuring speed - all I care about is position so it's just a matter of counting pulses.

yes and the oscillations look the same.

I have an important update tho:
i hooked up the encoder to an oscilloscope and I see the pulses wabbling around at more or less the same frequency I see in the speed plot. So I think the arduino is actually measuring fine the motor speed and some high frequency component in the motor speed are shown in the plot.

So maybe my code (with your suggestions) indeed works and I rather need to work on the motor...

Thanks for now! I will write if I discover where the problem is =)

llea:
Robin thanks for your suggestion. My goal is to get to a point in which I can have a speed measurement after every pulse from the encoder.

If the thing is moving slowly enough then just change my suggested code from counting up to 12500 to counting up to 1

However if you have 8000 pulses per revolution I can't see any value in checking the speed that finely. I suspect 10 or 20 times per revolution would be sufficient.

What are you going to use the speed information for?

...R

Robin2:
If the thing is moving slowly enough then just change my suggested code from counting up to 12500 to counting up to 1

However if you have 8000 pulses per revolution I can't see any value in checking the speed that finely. I suspect 10 or 20 times per revolution would be sufficient.

What are you going to use the speed information for?

...R

Thanks Robin. I am using the encoder to read the position and speed of a motor that makes a diffraction grating rotate. This diffraction grating is part of a spectrometer and the information about position and speed at each point is used to infer the wavelength from the angle. Since the spectrometer needs very high resolution, I need to check the exact timing between each encoder pulse in order to adjust the wavelength axis accordingly.

I think you should look into stepper motors. That way you don't have to measure speed/position as you have absolute control over this. It sounds like the more appropriate tool for this job.
12,500 positions per rotation is nothing for a geared down stepper. Typical 200 steps per rotation, some stepper drivers allow up to 128x microstepping (how precise that is I really can't say) but say you use a more modest 8x microstepping, that's 1,600 steps per rotation. Gear it down 10 times and you have 16,000 steps for a rotation.
The stepper moves at the speed you send the pulses to the controller - no need to worry about time measurements and so, you simply give the number of pulses for the amount of movement you need to make at the speed you want it to go.