Open Whahahahaha opened 2 years ago
This is because position is saved in an long datatype (32 bits) and the MCU is 8-bit and handles instructions in an 8 bit manner.
The CPU needs 4 CPU cycles to fully read the value in that function, and in between those cycles an interrupt can occur which in turn can change any of the 8 bit parts of the 32 bit long value. Trust me on this, ive worked with Atmega's long before Arduino came, i know it sounds weird, but its how the 8 bit MCU handles instructions. long newPos = encoder->getPosition();
So the solution is either to turn off interrupts as you say, or change the code so the getPosition only returns an int8_t (or char)
the interruptRotator example causes some spikes of 255 or -255 for the encoder position when you rotate around the "0" position.
it's fixable by calling noInterrupts(); and interrupts(); around the "newPos = encoder->getPosition();" line to temporarily disable interrupts when getting the current value.