Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sporadic 255/-255 values at interruptRotator example when hovering around 0 #35

Open
Whahahahaha opened this issue Feb 28, 2022 · 1 comment

Comments

@Whahahahaha
Copy link

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.

@sysshad
Copy link
Contributor

sysshad commented Nov 7, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants