and when I upload the sketch to my MCU(Arduinio nano) it all works CW and CCW but registers 4 times at a tick (in both libraries) instead of a single time as expected.
MikeLemon:
Well that won't do sins I don't want to put /4 all over the code that's why I use a library to keep it neat.
I'm glad that it's so virtuous. You shouldn't have to put /4 everywhere for anything in a properly constructed C++ program. That's what functions are for.
aarg:
I'm glad that it's so virtuous. You shouldn't have to put /4 everywhere for anything in a properly constructed C++ program. That's what functions are for.
But why not solve the problem from the roots? it will just bee an exponential issue like that.
MikeLemon:
But why not solve the problem from the roots? it will just bee an exponential issue like that.
Unfortunately, most hardware is kind of dumb and requires a "wrapper" of some kind to manage the complexity and hide the details that are not important in the user application. In my view, a well constructed interface is a root solution.
Dividing by 4 would be a kind of kludge, I admit. I would be surprised if the library doesn't have some feature to handle this problem.
But it is not hard to encapsulate it, like:
long getEncPosition()
{
return myEnc.read()/4;
}
Then you just call getEncPosition() any place you would have called myEnc.read(). You can even use find and replace if you already have the sketch written.
Delta_G:
I'd skip the library. All you need is one interrupt routine for one of the pins if you only want one tick per click. I mean libraries are nice, but if it doesn't work like you want then why spend a day trying to change the library when you can spend 30 seconds writing the one function you actually need?
Delta_G:
I'd skip the library. All you need is one interrupt routine for one of the pins if you only want one tick per click. I mean libraries are nice, but if it doesn't work like you want then why spend a day trying to change the library when you can spend 30 seconds writing the one function you actually need?
Great, so the work is finished by the time I post this! Excellent!
To do this effectively, you really need to "debouce" the switch. I've never been able to effectively do this in software. I use 10000pF capacitors between the A pin and GND and B pin GND. This makes the circuit very well protected from bad reads. Furthermore, you need to check the value more frequently than it can change otherwise the quadrature signal will become out of sync and you will not know what direction you are turning.
You might consider upgrading to a photoelectric encoder. These usually employ an optical disk with phototransistors and several choices of solid state outputs. No debouncing, standard A, B, Z output channels, various PPR rates.
A caveat: They're not Ebay cheap but if you balance encoder cost against your programming time it may work for you.
Why @MikeLemon is focused on using a library, and not a very flexible or powerful one, inspite of all the simple demonstration code previously offered (and now yours) along with advice to not use a library is a mystery.