controlling a DC motor

Hi everyone,
I'm trying to control a DC motor, it has a gearbox and an encoder attached o the shaft, it's a 24v DC motor, I've got an H-bridge LMD18200(I attach datasheet), so initially the idea is to keep motor turning in a range 0º-120º. I'm using a for loop, but, the motor doesn't track pwm signal as indicated, and the encoder doesn't send data, I made codes for H-bridge and encoder and works great, but when I put both together nothing works, the motor just spins without any control. WHAT I'M DOING WRONG ??

/*Program to keep motor spinning in a range  0º and 120º
  using a for structure*/
#define A 20 /* channel A*/
#define B 21 /* channel B*/
#define dir 12 /* input for direction pin on LMD18200*/
#define pwm 11 /*  input for pwm pin on LMD18200*/
#define brake 10 /* input for brake pin on LMD18200*/
float pwmsignal = 50;/*pwm signal*/
volatile float count = 0; /* global variable to store encoder data*/

void setup()
{
  TCCR1A = 0x01; /* on pin PB5 (raise pwm frequency to a no audible range, just to avoid the noise comming from the motor)*/
Serial.begin (19200); Serial.println("START READING");
pinMode (dir, OUTPUT); /* arduino output to H-bridge*/
pinMode (pwm, OUTPUT); /* arduino output to H-bridge*/
pinMode (brake, OUTPUT); /* arduino output to H-bridge*/
pinMode(A, INPUT); /* input channel A*/
pinMode(B, INPUT); /* input channel B*/
attachInterrupt(3, doEncoderA, CHANGE);
attachInterrupt(2, doEncoderB, CHANGE); 
}

void doEncoderA(){
  // look for a low-to-high on channel A
  if (digitalRead(A) == HIGH) { 
    // check channel B to see which way encoder is turning
    if (digitalRead(B) == LOW) {  
      count = count + 1;         // CW
    } 
    else {
      count = count - 1;         // CCW
    }
  }
  else   // must be a high-to-low edge on channel A                                       
  { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(B) == HIGH) {   
      count = count + 1;          // CW
    } 
    else {
      count = count - 1;          // CCW
    }
  }
  
}
void doEncoderB(){
  // look for a low-to-high on channel B
  if (digitalRead(B) == HIGH) {   
   // check channel A to see which way encoder is turning
    if (digitalRead(A) == HIGH) {  
      count = count + 1;         // CW
    } 
    else {
      count = count - 1;         // CCW
    }
  }
  // Look for a high-to-low on channel B
  else { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(A) == LOW) {   
      count = count + 1;          // CW
    } 
    else {
      count = count - 1;          // CCW
    }
  }
}
void loop()
{
  float angle=count*(360.0/400.0); /*local variable to convert pulses to mechanical degrees
                                   (encoder is 100cpr but has two channel
                                    and attachInterrupt is taking into account
                                    both raising and falling edges, that's why 400 in 360/400)*/
  int pos=int(angle); /*local variable to convert angle variable to an integer one*/
for (pos=0;pos<120;)
  {/*source1,sink2, the motor spins CW*/
  digitalWrite (dir, HIGH);
  delay(1);
  digitalWrite (pwm, HIGH);
  delay(1);
  digitalWrite (brake, LOW);
  delay(1);
  analogWrite(pwm,pwmsignal); 
  }
for(pos=120;pos>0;)
  {/*sink1,source2, the motor spins CCW*/
  digitalWrite (dir, LOW);
  delay(1);
  digitalWrite (pwm, HIGH);
  delay(1);
  digitalWrite (brake, LOW);
  delay(1);
  analogWrite(pwm,pwmsignal); 
  } 
Serial.print("pos =");
Serial.print(pos);
Serial.println();
}

LMD18200.pdf (338 KB)