Building a tamiya tracked robot with analog stick controller. Software has bugs

Hi i'm new to the world or arduino.
Im building a little robot using my arduino uno, tamiya tracked robot and two analog stick from a ps2 controller.




here is my code

int analogLeftForward;//used to drive left motor through analog pin 0
int analogLeftBack;
int analogLeftValue;
int analogRightForward;//used to drive left motor forward through analog pin 1
int analogRightBack;
int analogRightValue;
void setup(){
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}
void loop(){
  int analogLeftValue = analogRead(A0);//reading analog stick position
  int analogRightValue = analogRead(A1);//reading analog stick position
  analogLeftForward = map(analogLeftValue, 0, 500, 0, 255);//mapping from centre position of analog stick to forward position for driving motor forward.
  analogLeftBack = map(analogLeftValue, 500, 1023, 255, 0);//mapping from centre position of analog stick to back position for driving motor in reverse.
  analogRightForward = map(analogRightValue, 0, 500, 0, 255);//mapping from centre position of analog stick to forward position for driving motor forward.
  analogRightBack = map(analogRightValue, 500, 1023, 255, 0);//mapping from centre position of analog stick to forward position for driving motor in reverse.
  if(analogLeftForward > analogLeftBack){
    analogWrite(9,analogLeftForward);// using digital PWM output to drive motor enable pin for different speeds
    digitalWrite(2, HIGH);//using L293DNE H-bridge sets pins to rotate motor forward
    digitalWrite(3, LOW);//using L293DNE H-bridge sets pins to rotate motor forward
    delay(100);}
  if (analogLeftBack > analogLeftForward){
    analogWrite(9,analogLeftBack);// using digital PWM output to drive motor enable pin for different speeds
    digitalWrite(2, LOW);//using L293DNE H-bridge sets pins to rotate motor back
    digitalWrite(3, HIGH);//using L293DNE H-bridge sets pins to rotate motor back
    delay(100);}
  if (analogRightForward > analogRightBack){
    analogWrite(11,analogRightForward);// using digital PWM output to drive motor enable pin for different speeds
    digitalWrite(10, HIGH);//using L293DNE H-bridge sets pins to rotate motor forward
    digitalWrite(8, LOW);//using L293DNE H-bridge sets pins to rotate motor forward
    delay(100);}
  is (analogRightBack > analogRightForward){
    analogWrite(11,analogRightBack);// using digital PWM output to drive motor enable pin for different speeds
    digitalWrite(10, LOW);//using L293DNE H-bridge sets pins to rotate motor back
    digitalWrite(8, HIGH);//using L293DNE H-bridge sets pins to rotate motor back
    delay(100);
}
}

it works some times and others it seems to hang at the first if statement.

any suggestions would be much appreciated.
Jonathan

Where are your serial debug prints?

  analogLeftForward = map(analogLeftValue, 0, 500, 0, 255);//mapping from centre position of analog stick to forward position for driving motor forward.

What happens if the value of analogLeftValue is 800?

You should be testing that the stick IS forward (though the range suggests back) before mapping.