Problems with optical encoder and motor shield.

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.

#define LEFT 0
#define RIGHT 1

//i pin usati per i due motori
int SX = 6;
int M1 = 7;
int DX = 5;
int M2 = 4;
//le due "velocità" dei motori
int velocita_sx = 100;                                       
int velocita_dx = 100;

//array per l'encoder 
long coder[2] = {0,0};

void setup(){
  attachInterrupt(LEFT, LwheelSpeed, CHANGE);    //inizializzo il pin 2 per l'interrupt
  attachInterrupt(RIGHT, RwheelSpeed, CHANGE);   //inizializzo il pin 3 per l'interrupt
  pinMode(M1, OUTPUT);                           //pin per il controllo dei due motori
  pinMode(M2, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  Serial.print("Coder value: ");               //stampo a monitor il valore dell'array
  Serial.print(coder[LEFT]);
  Serial.print("[Left Wheel] ");
  Serial.print(coder[RIGHT]);
  Serial.println("[Right Wheel]");
  digitalWrite(M1,LOW);                     //per definire il senso di rotazione
  digitalWrite(M2, LOW);
  if(coder[LEFT] < 50 && coder[RIGHT] < 50){   //dovrebbe fare 50 passi dell'encoder e poi fermarsi
  analogWrite(SX, velocita_sx);     
  analogWrite(DX, velocita_dx);
  } 
  
  
  if(coder[LEFT] >= 50 && coder[RIGHT] >= 50){
    analogWrite(DX, 0);
    analogWrite(SX, 0);
  }
}


//le due funzioni chiamate dall'interrupt per aumentare il contatore dei passi
void LwheelSpeed()
{
  coder[LEFT] ++;  
}
 
 
void RwheelSpeed()
{
  coder[RIGHT] ++; 
}

Thanks for your help.

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.

I'm using this [motor shield](DFRobot Open-Source Hardware Electronics and Kits?
route=product/product&filter_name=motor&product_id=69#.ULVGg4ZxV8E) and there are two power lines, the first 9V for arduino which feeds a servomotor, a ultrasonic sensor and the two encoders, the second for motor shield and motors.

How can i screen the connection from encoders to Arduino?

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.