Contronling DC motor speed ?

hi
I’m trying to control a dc motor through l293d motor controller

const int controlPin2 = 4; // connected to pin 2 on the H-bridge
const int enablePin = 3;   // connected to pin 1 on the H-bridge


void setup(){

  pinMode(controlPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);

  digitalWrite(enablePin, LOW);
  digitalWrite(controlPin2, LOW);

Serial.begin(9600); 
Serial.println("Ready");
}


void loop(){
if (Serial.available() > 0) {
    int inByte = Serial.read();
    
    switch (inByte) {
    case '0':    
  digitalWrite(enablePin, LOW);
   digitalWrite(controlPin2, LOW);
 Serial.println("OFF");
      break;
    case '1':    

  digitalWrite(controlPin2, HIGH);
    analogWrite(enablePin, 50);
    Serial.println("CW slow");
      break;
          case '2':    

  digitalWrite(controlPin2, HIGH);
    analogWrite(enablePin, 100);
    Serial.println("CW slow +");
      break;
          case '3':    
  digitalWrite(controlPin2, HIGH);
    analogWrite(enablePin, 150);
    Serial.println("CW medium");
      break;
          case '4':    
  digitalWrite(controlPin2, HIGH);
    analogWrite(enablePin, 200);
    Serial.println("CW fast");
      break;
          case '5':    
  digitalWrite(controlPin2, HIGH);
    analogWrite(enablePin, 250);
    Serial.println("CW fast +");
      break;
    case '6':    
  digitalWrite(controlPin2, LOW);
    analogWrite(enablePin, 50);
    Serial.println("CCW slow");
      break;
      case '7':    
  digitalWrite(controlPin2, LOW);
    analogWrite(enablePin, 100);
        Serial.println("CCW slow +");
      break;
      case '8':   
  digitalWrite(controlPin2, LOW);
    analogWrite(enablePin, 150);
        Serial.println("CCW medium");
      break;
       case '9':    
  digitalWrite(controlPin2, LOW);
    analogWrite(enablePin, 250);
        Serial.println("CCW fast");
      break;
}
}
}

it is working well except 1 to 5 are revered
1 must be the minimum speed and 5 must be the maximum
it is acting 5 minimum speed and 1 maximum

any ideas ?

check how "enablePin" works. It might be "active low"

The L293 provides power to the motor only when the two inputs are different. If contolPin2 is HIGH, the motor gets power when the enablePin is LOW. So sending analog write(50) the PWM is high 25% of the time and low 75% of the time. The motor runs fast.

Thanks for fast response guys , how must the right code be ?

While pin 4 is high you need to "invert" the PWM. To do so, subtract the speed that you want from 255.

analogWrite(pin3, 255-speed);

Now if speed is 50 the result will be 205. The PWM is high about 75% and low about 25% and the motor will run slowly.