Hi everybody, i have a problem with optical encoder and the motor shield. When the two motors are stopped, the encoders function properly: making them rotate by hand, they count 20 steps per spin but when the motors are on, some interferences make much many steps be counted instead of their real number. I paste the code but not the whole one because it's too long. I don't think there's something wrong, but there could be something to better.

It sounds like the PWM motor current is interfering with the signal from the encoders. What are you using to drive the motors, and how do you have the encoders connected?

You may need to use screened (shielded) cable to connect the encoders to the Arduino, and you should definitely ensure that the power and ground lines to the encoders are completely separate from the power and ground lines to the motors or motor drivers.

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

I suggest you use shielded cable to connect the encoders. Use the shield as the ground connection, connecting it to the ground side of the encoders (and nothing else) at the encoder end, and to one of the Arduino ground pins. Use a different Arduino ground pin to connect power and the servo to the Arduino.

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.