Your while loop logic is incompatible with the conditions inside it.
Your while loop is saying:
as long as BOTH wheel encoders read
less than clicks, execute the code
inside
and your if statements are saying:
if any of the wheel encoders is greater or equal to clicks, stop
the corresponding motor
This is generally false except for the rare condition where one of the encoders happen to increment between the arduino evaluating the while and the if statements. In order words just about never.
So the first thing you need to do is fix your logic. Then you need to investigate more sophisticated means of keeping the wheels in sync, such as computing an error term from the different in ticks, and employing a PID controller algorithm to adjust the speed of both motors via analogWrite
.
Magnetic encoders for most intents and purposes are an excellent low cost alternative to traditional optical devices. There are some concerns related to their repeatability, accuracy and jitter but if you are not on the bleeding edge of servo design, they should suit your purposes very well.
If you are looking to purchase an encoder off the shelf as an integrated product, I've used ones from Baumer before with great success (they are a little expensive). Otherwise for an IC level solution, the most mainstream ones are from AMS (Austria Microsystems). I know that the AMS devices are often used in automotive applications like accelerator position sensing, steering wheel position sensing, etc. RLS (Rennishaw) is another IC vendor.
One thing to be careful of when using an IC solution is that the quality of the magnet matters so be sure to use one that is recommended by the manufacturer. Also placement of this magnet is important, make sure that it is in a non-ferrous shaft like aluminum, Stainless Steel or plastic.
In general for servos, I wouldn't recommend closing a velocity loop on absolute position feedback but rather use quadrature signals. You can close position loop by accumulating quadrature as well or use absolute position. The systems that I worked on use absolute feedback only to perform the homing function at power on and then quadrature for everything else.
Best Answer
Chattering is the time during which the output is not stable when a transition from ON to OFF or from OFF to ON occurs. The signal quickly jumps between both states during a few ms.
Bounce (you didn't mention it but it is indicated in the datasheet) is the time, when the output is supposed to be stable in the ON state (between chatters), during which the slider doesn't touch the contact well, and the output is actually OFF for a few ms. It can happen when you move the slider, even if you stay on the contact area.
Masking time is the solution to that: it is the time during which you ignore transitions, to overcome both chattering and bounce. It can be done either in your software (e.g. waiting for the value to be the same over several samples taken at regular intervals), or using hardware (e.g. RC filter). This process is called debouncing, which is why your qustion is highly related to the "Debouncing buttons" question, in which you may find interesting solutions for implementing this.
Here is a diagram showing these effects:
t1 and t3 are the chattering times, and t2 is the bounce time.