Optical encoder: read engine speed

Thank you cattledog!! With your code rotating by hand the motor I visualize the number of turns (which should be) corrected on the serial monitor. I will test this code when I provide a specific voltage value to the motor at the laboratory.
One last question: Do you know why when I turn from vertical position in the horizontal position the motor the value of speed varies by 2-3 orders of magnitude?