Hi all
I'm working on a project that uses a rotary encoder knob. I want to use interrupts to avoid missing any pulses from the encoder. I can see how to do this without much trouble. I'm going to use both interrupts to catch the rising and falling edges of both switches in the encoder. To keep my interrupt code as quick as possible, it's simply going to increment or decrement a counter. I will read that counter in my main loop.
My main loop will be something like this: encoderCount is incremented or decremented by the interrupt.
byte previousEncoderCount;
void loop() { byte count = encoderCount; byte distanceMoved = count - previousEncoderCount;
if (distanceMoved != 0) { // Do something previousEncoderCount = count; } }
I know I need to add some logic to cope with the counter overflowing.
Here's my question. Is it safe to do count = encoderCount without stopping interrupts? I assume that using a byte makes it a safe indivisible transfer but I'd like to confirm that. I don't think using an int would be safe because that would need to move two bytes and the interrupt might occur between them producing a wrong answer especially if the interrupt caused the LSB to roll over in that time.
I'm interested in any comments in case there is something the compiler does behind the scenes that I'm not aware of.
A note to anyone playing with encoders. I bought an expensive optical encoder ($17 from Mouser) because I wanted 64 steps per revolution. I missed the fact that if you catch all edges then you can get four times the number of steps. My next one will use a $2 mechanical encoder with 16 steps to achieve 64. I'll probably need some debounce circuitry because I can't really software debounce if it's causing interrupts.
Thanks Ross