I'm having an issue with rotary encoders - I've tried two rotary encoders (similar, from different manufacturers), and tried a few different code samples/examples, but I'm getting the same results... perhaps my understanding of a rotary encoder is incorrect... here's the issue...
When I turn a rotary encoder, there are divots or "notches", and the encoder locks into these notches as I turn. I'd like to get the value only when the dial is "in the notch". One notch equals one value. What I'm seeing however, is that as I turn the encoder, the value changes "between the notches" For example, lets say I have an int which keeps track of a value, and the value is changed when the rotary encoder is turned. If the current value is 7, as I turn the encoder, the value changes several times before the encoder stops at the next notch... usually 3 or 4 values, and the next notch has a value of 11. I'd like it to be only one per notch.
For referenced, I've tried the following tutorials:
I'm wired up to pin14 & pin15 (just like in the "reading-rotary-encoder-on-arduino" tutorial).
When I turn the encoder, it works, in the sense that I do get notified that the encoder has changed, and which direction, it's just that the value is changing not just when the encoder is at the notch, but several times between each notch.
Or am I misunderstanding how rotary encoders and graycode work is wrong (and this is standard and how they work)?
What I'm seeing however, is that as I turn the encoder, the value changes "between the notches"
Many of the knob-type encoders work that way. They make electrical contacts only during the transition. Shaft encoders make electrical contacts at all times.
it's just that the value is changing not just when the encoder is at the notch, but several times between each notch.
Have a look at my "tutorial"- I use the word loosely....
The snippet from a datasheet bears out what you say, and the serial output and the LEDs agree with each other. That is, the switches A and B are off at a detent then as you turn CW, A goes on before B does- that's how it knows which way the knob is turning, If you went CCW from that first detent, B goes on before A.
As you turn the knob sloooowly between detents, you can see the LEDs react to the changes in A and B.
I don't know if you have the datasheet for your actual model, but I suspect it will have a "wave diagram" showing exactly how the outputs change as you turn the knob in either direction.
edit.... all of which might be irrelevant for your grey code device :~
As you know, there are four different logic states of the hi/low outputs of pins A and B. The relationship between the detents and the logic states varies from encoder to encoder. Some have a detent at every different state, some have two detents for the four positions, and some, like yours, have only one. If the code you are using counts each state change, then you will increment by four between notches. Some versions of code only count half the changes.
If you have only one detent per four states, you should be able to determine the pattern of A and B at the detent. It should be the same at each detent. You will change your count by plus or minus one each time you see that pattern. The direction of rotation to determines whether you are increasing or decreasing your count.
As JimboZA says, your data sheet should have a diagram of the pulse pattern. Study that, and you will see that for each direction of rotation one of the two pins changes first from the detent position. Find out which is first to change after the detent and you can determine direction of rotation and whether to increment or decrement at the next detent.