Controller for 4 servos

I try to control 4 servos for rover. 2 servos for the left side and 2 for the right side. The servos on the same side works in the same rotation. I have a problemL The rotation of servo are not stable. If I put 1500microseconds for the neutral position, they move a little.

I connected directly on the arduino pin the wired of the servo. I should put a resistance ?

int sensorIR = 0;
int sensorUS = 1;
int calibIR = 10000;
int valIR;

int servoTU = 8;
int servoLF = 9;
int servoLB = 10;
int servoRF = 11;
int servoRB = 12;
int lastPulseR;
int lastPulseL;
int pulseWidthR = 1250;
int pulseWidthL = 1750;
int refreshTimeServoL = 20;
int refreshTimeServoR = 20;

void servoL(int pulseWidth){
 if(millis()-lastPulseL >= refreshTimeServoL){
  digitalWrite(servoLF,HIGH);
  digitalWrite(servoLB,HIGH); 
  delayMicroseconds(pulseWidth);
  digitalWrite(servoLF,LOW);
  digitalWrite(servoLB,LOW); 
  lastPulseL=millis();
 }
}

void servoR(int pulseWidth){
 if(millis()-lastPulseR >= refreshTimeServoR){
  digitalWrite(servoRF,HIGH);
  digitalWrite(servoRB,HIGH); 
  delayMicroseconds(pulseWidth);
  digitalWrite(servoRF,LOW);
  digitalWrite(servoRB,LOW); 
  lastPulseR=millis();
 }
}

int readSensorIR(){
  return (1000/analogRead(sensorIR));         
}
           
void setup(){
 pinMode(servoTU,OUTPUT); 
 pinMode(servoLF,OUTPUT); 
 pinMode(servoLB,OUTPUT); 
 pinMode(servoRF,OUTPUT); 
 pinMode(servoRB,OUTPUT); 
 pinMode(sensorIR,OUTPUT);
 pinMode(sensorUS,OUTPUT);
 lastPulseL=millis();
 lastPulseR=millis();
}

void loop(){
  servoL(pulseWidthL); 
  servoR(pulseWidthR);             
}

Sounds like your servos aren't properly trimmed. You need to connect them up and adjust the pot inside the servos till they stop moving at 1500us.

Also, you need to used unsigned long for the pulse timer, otherwise it'll over flow in a minute or so.

-Z-

I find the problem :D. I use a battery for the servo and an extern power for the arduino board. I connect the ground together and now everything works fine.