Stop a dc motor with ultrasonic sensor

I have the following problem in programming , I enter a code on a keypad 4x4 and I displayed what I want to write , once you put the code you want , a DC motor moves controlled by an ultrasonic sensor.

The problem is that the engine does not stop when the sensor senses a distance.

Thanks !

Did you want someone to find the problem in your code? Kind of hard to do that without seeing the code don't you think?

//---------------------------------------[Librerias]--------
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//---------------------------------------[Variables]-------------------
 long duration, mmEjeX, cmEjeY;
//---[Ultrasonico Eje X]---
const int trigEjeX= 30;

//---[Motores DC]---
int MX1 = 24;
int MX2 = 25;
//PWM 2

//---[LCD]---
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

//---[Teclado]--- 
int espacio = 0;
int cuadro = 0;
int fila = 0;
const byte Filas = 4;          //KeyPad de 4 filas
const byte Cols = 4;           //y 4 columnas
byte Pins_Filas[] = {53, 51, 49, 47};     //Pines Arduino para las filas.
byte Pins_Cols[] = {45, 43, 41, 39};     // Pines Arduino para las columnas.

char Teclas [ Filas ][ Cols ] =
    {
        {'1','2','3','A'},
        {'4','5','6','B'},
        {'7','8','9','C'},
        {'*','0','#','D'}
     };
     
Keypad Teclado = Keypad(makeKeymap(Teclas), Pins_Filas, Pins_Cols, Filas, Cols);


//---------------------------------------[Main]-------------------
void setup(){
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("Codigo libro:");
  pinMode(MX1,OUTPUT);
  pinMode(MX2,OUTPUT);
}



void loop(){

 
  pinMode(trigEjeX, OUTPUT);          // Set pin to OUTPUT
  digitalWrite(trigEjeX, LOW);        // Ensure pin is low
  delayMicroseconds(2);
  digitalWrite(trigEjeX, HIGH);       // Start ranging
  delayMicroseconds(5);              //   with 5 microsecond burst
  digitalWrite(trigEjeX, LOW);        // End ranging
  pinMode(trigEjeX, INPUT);           // Set pin to INPUT
  duration = pulseIn(trigEjeX, HIGH); // Read echo pulse

  // convert the time into a distance
  mmEjeX= microsecondsToCentimetersX(duration);
  
  Serial.print(mmEjeX);
  Serial.print("mm Eje X: ");
  Serial.println();
  delay(100);

//---[Teclado + LCD]---
  static int myVal = 0;
  lcd_key = read_LCD_buttons();
  char tecla = Teclado.getKey();
  if (tecla != 0){             
     lcd.setCursor(espacio,1);
     myVal = (myVal * 10) + tecla - '0';
     lcd.print(myVal);
   }  
  if( lcd_key == btnUP){
    lcd.setCursor(4,1);
    lcd.print("Aceptado");
  }
  if( lcd_key == btnSELECT){
    lcd.setCursor(0,1);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Codigo libro:");
    espacio=0;
  }

 if(myVal == 63 && adc_key_in < 250){ // Coordenada X=63, Y=3 
   analogWrite(2,210);
   digitalWrite(MX1,HIGH);
   digitalWrite(MX2,LOW);
  myVal = 0;
   if(mmEjeX >= 150 && mmEjeX <= 160){
    analogWrite(2,0);
    delay(2000);
   }
}

}//void loop


//---------------------------------------[Sub Rutinas]-------------------
long microsecondsToCentimetersX(long microsecondsX)
{ 
  return microsecondsX / 29 / 0.2;
} 

//Para leer los botones, vamos a definer una pequeña function;
int read_LCD_buttons()  
  { adc_key_in = analogRead(0);      // Leemos A0
    // Mis botones dan:  0, 145, 329,507,743
    // Y ahora los comparamos con un margen comodo
    if (adc_key_in > 900) return btnNONE;     // Ningun boton pulsado 
    if (adc_key_in < 50)   return btnRIGHT; 
    if (adc_key_in < 250)  return btnUP;
    if (adc_key_in < 450)  return btnDOWN;
    if (adc_key_in < 650)  return btnLEFT;
    if (adc_key_in < 850)  return btnSELECT; 

    return btnNONE;  // Por si todo falla
  }
    analogWrite(2,210);

?

AWOL:     analogWrite(2,210);

?

regulates the speed DC motor with H bridge and pwm.

analogWrite(Enable = PWM, Speed)

Ah! - it's a Mega.