I've tried for a while to program a rotary encoder for controlling a dc motor. If I want the motor to rotate only 60 degrees, I would like my encoder to count 60 degrees and stop the motor when the motor have reach 60 degrees.
The encoder have 4 cases (00, 01, 10, 11) and two channels (a and b).
I've tried to use the function "attachInterrupt()" as well but didn't work so well.
The encoder have 360 CPR (cycles per revolution) but sometimes if I turn the shaft on the encoder really fast, the arduino board have problems counting all the phases/cycles.
I don't know what's left for me to do here.
Does someone know how I can make this work properly?
Let the encoder count i.e. 60 degrees when the motor is running and then stop the motor.
I'm frustrated over that I can't find any examples on how to program a rotary optical encoder like this. There is some code on the playground but its not the same case as mine.
Is this an optical or mechanical encoder? If it is mechanical then you could be suffering from contact bounce.
but its not the same case as mine.
in what way?
That sequence :- (00, 01, 10, 11) is not what it output is it, in that order, it looks wrong.
Also this line is all wrong :-
valAB = (10*valA) + valB;
Why are you trying to combine the values? It makes no sense and slows down the program a lot. You can't afford to be slow with a rotary encoder. Just use if(valA == 1 && valB==0) ... or whatever. This is much faster than an integer multiply.
I send as input "P160S100" P is for position and S is for speed.
The problem now is that the encoder rotate about 180 degrees when I specify that I want to rotate only 160 degrees. I know I need a PID controller soon anyway but it seems like the enoder counts to exactly 160 degrees and then stops. What am I doing wrong here? If the encoder rotates to 160 degrees and stops, why do it look like 180-200 degrees on my prototype?
I tried to attach a picture of my motor and encoder but I'm not sure how to attach a .jpg image.