i am not able to control the speed of dc motor

Hi

I am making an arduino project in which i have to control speed of DC motor with ultrsonic sensor. I have prepared and arduino code to do this work but unfortunately my motor only rotates in forward and backward direction and when it come in specific range like (50-95),(105-150) where i have to decrease the motor speed by 50 and 40 % then the motor only do some noise it dose not rotate can any one please help me.

Components i am using is
1 ) arduino uno
2 ) L293D motor driver ic
3 ) external 5 V/1.5 A power supply
4 ) 9 volt motor.

Below i have mention the code and circuit diaram

#define echoPin 2 
#define trigPin 4 
int enA = 9;
int in1 = 8;
int in2 = 7;
// defines variables
long duration; 
int distance; 

void setup() {
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
  Serial.begin(9600); 
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
}
void loop() {
 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  
  distance = duration * 0.034 / 2; 
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
   if (95<=distance && distance<=105)
  {
  Serial.println("forwrd");
  analogWrite(enA, 255);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(2000);
  }
  else if(50<=distance && distance <=95){
  Serial.println("forward 40 %");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);// forward
  analogWrite(enA,102);
  delay(2000);
  }
  else if(105<=distance && distance <=150){
    Serial.println("reverse 50%");
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
    analogWrite(enA,127);
    delay(2000);
  }
  else if(10<= distance && distance<=50){
  Serial.println("10-50 forwrd 40 %");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);// forward
  analogWrite(enA,102);
  delay(2000);
  }
  else if(150<= distance && distance<=200){
    Serial.println("105-200 reverse 50%");
     digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
    analogWrite(enA,127);
    delay(2000);
  }

  
}

3 ) external 5 V/1.5 A power supply
4 ) 9 volt motor.

feels weird..

Because i am not attaching any load to motor so it is running on this power supply

also then L293D requires at least 4.5 V. Is your 5V really precise and will it stay there when larger intensity are drawn ?

you should try with a 9V power supply

An L293 typically drops around 2V to 4V so when you run it off only 5V you're only getting at best a couple of volts at the motor. So you're lucky that it will move at all even with 255 on ENA. As soon as you go below 255 you're not giving the motor enough power to move.

Try powering the L293/motor with a separate battery with 9 to 12V (but not a little rectangular 9V smoke detector battery).

Steve