Optical shaft encoder not working

I have been using the 3 wire optical shaft encoders for programming. Today I noticed that sometimes they would output a very large number when they were kept perfectly still. After printing the encoder values to the terminal it turns out the left encoder sometimes gives a reading of 1.67772e+07 (always this number) after 5 iterations of the loop and then returns to outputting 0 (as it should).
This was originally only happening to the left encoder however I have now seen it happen from the back encoder and right encoder as well. The number is always 1.67772e+07.

My Encoder Setup:
Left encoder and back encoder connected to the 3 wire ports in the v5 brain
Right encoder connected to the 3 wire expander

Things I have tried :
Changing the ports - no difference
Changed the right encoder - no difference

Unfortunately I don’t have many spare encoders so won’t be able to change all 3.
Has anyone had this is issue before / know how to resolve it / knows why it is happening?

I can just add into the code to check if there is a very large difference between the previous encoder value and the current one and if there is then just use the previous encoder value. But it would be nice to get the encoders working.

Here is a picture of my code including the terminal

Note the encoders don’t always spike like this. I would say one in 3 times.

Any help would be greatly appreciated.