Calculate distance Ultrasonic sensor + LCD display

Hi!
I have a problem with the distance calculation of my ultrasonic sensor. He calculates the distance well but apparently from time to time a value appears: 72cm / 0.72cm when losing the signal. I have tried to do a reset so that this number does not appear and the previous value is maintained but it does not work.
Attached code:

#include <LiquidCrystal_I2C.h>

#include <LiquidCrystal.h>


int VO = 3;
int RS = 4;
int E = 5;
int D4 = 6;
int D5 = 7;
int D6 = 8;
int D7 = 9;
LiquidCrystal lcd (RS, E, D4, D5, D6, D7);

int pin_TRIG = 10; //Pin para enviar pulsos
int pin_ECHO = 11; //Pin para recibir la señal de rebote

int tiempoSenal;
int distanciaCM;
float distanciaM;
int ultimaDistancia = 0;

void setup() {

 pinMode(pin_TRIG, OUTPUT);
  pinMode(pin_ECHO, INPUT);
  lcd.begin(16, 2);

  analogWrite(VO, 0);
}
  
  



void loop() {
  
 digitalWrite(pin_TRIG, LOW);
  delayMicroseconds(5); //Estabiliza el sensor

  digitalWrite(pin_TRIG, HIGH); //Envia el pulso para iniciar el sensor
  delayMicroseconds(10);

  tiempoSenal = pulseIn(pin_ECHO, HIGH); //Tiempo que dura el pin ECHO en HIGH
  distanciaCM = (0.034 * tiempoSenal) / 2; //Convierte el tiempo en distancia (cm)
  distanciaM = ((0.034 * tiempoSenal) / 2) / 100; //Convierte el tiempo en distancia (m)

if(distanciaCM != ultimaDistancia);
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("not found");   
  lcd.print(distanciaCM);       //Muestra en la pantalla la distancia en centímetros
  lcd.print(" :(");
  ultimaDistancia = distanciaCM;
  
  }


  lcd.setCursor(0, 0);
  lcd.print("Distancia: ");   
  lcd.print(distanciaCM);       //Muestra en la pantalla la distancia en centímetros
  lcd.print("cm ");

  
  lcd.setCursor(0, 1);
  lcd.print("Distancia: ");
  lcd.print(distanciaM);       //Muestra en la pantalla la distancia en metros
  lcd.print("m");

  delay(500);

}

Try

void loop() 
{
  digitalWrite(pin_TRIG, HIGH); //Envia el pulso para iniciar el sensor
  delayMicroseconds(10);
  digitalWrite(pin_TRIG, LOW);
  tiempoSenal = pulseIn(pin_ECHO, HIGH);

instead

Thanks! Even so it keeps giving the same error.

Should be "unsigned long" / "uint32_t", not "int"

(Also, there is no good reason it should have global scope)

Now it's happening the same but with an other value (11,87m)... so i don't know how can i fix it.
Now the code it's like this.. any other idea?

#include <LiquidCrystal_I2C.h>

#include <LiquidCrystal.h>


int VO = 3;
int RS = 4;
int E = 5;
int D4 = 6;
int D5 = 7;
int D6 = 8;
int D7 = 9;
LiquidCrystal lcd (RS, E, D4, D5, D6, D7);

int pin_TRIG = 10; //Pin para enviar pulsos
int pin_ECHO = 11; //Pin para recibir la señal de rebote

uint32_t tiempoSenal;
int distanciaCM;
float distanciaM;
int ultimaDistancia = 0;

void setup() {

 pinMode(pin_TRIG, OUTPUT);
  pinMode(pin_ECHO, INPUT);
  lcd.begin(16, 2);

  analogWrite(VO, 0);
}
  
  



void loop() {
  
 digitalWrite(pin_TRIG, HIGH); //Envia el pulso para iniciar el sensor
  delayMicroseconds(10);
  digitalWrite(pin_TRIG, LOW);
  tiempoSenal = pulseIn(pin_ECHO, HIGH); //Tiempo que dura el pin ECHO en HIGH
  distanciaCM = (0.034 * tiempoSenal) / 2; //Convierte el tiempo en distancia (cm)
  distanciaM = ((0.034 * tiempoSenal) / 2) / 100; //Convierte el tiempo en distancia (m)

if(distanciaCM != ultimaDistancia);
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("not found");   
  lcd.print(distanciaCM);       //Muestra en la pantalla la distancia en centímetros
  lcd.print(" :(");
  ultimaDistancia = distanciaCM;
  
  }


  lcd.setCursor(0, 0);
  lcd.print("Distancia: ");   
  lcd.print(distanciaCM);       //Muestra en la pantalla la distancia en centímetros
  lcd.print("cm ");

  
  lcd.setCursor(0, 1);
  lcd.print("Distancia: ");
  lcd.print(distanciaM);       //Muestra en la pantalla la distancia en metros
  lcd.print("m");

  delay(500);

}

when you "loose the signal" (too far, pulseIn() returns 0) the distance is 0.

So you still display 0 but because you did not clear the previous display, whatever was there before remains, you should see weird stuff like (if the previous distance was 72cm)

distanciaCM: 0cmm
distanciaCM: 0m72m

try the newPing library.

You could try changing the pulseIn timeout.

Sorry but I'm new with arduino, can you explain me this please? I dont understand

:warning:There is a bad semi colon in your code:
if(distanciaCM != ultimaDistancia);
--> loose the semicolon... of you'll always show not found quickly replaced by the new distance

Once you have done that, you start with ultimaDistancia = 0

if you read a valid distance, say 87cm it will be different than 0 and you modify ultimaDistancia and then you go write the screen which shows

distanciaCM: 87cm
distanciaCM: 0.87m

this blue thing is what's on screen

now imagine that at the next loop() pulseIn() times out (can't read the echo). The doc states the function will return 0 into tiempoSenal so your maths

  distanciaCM = (0.034 * tiempoSenal) / 2; //Convierte el tiempo en distancia (cm)
  distanciaM = ((0.034 * tiempoSenal) / 2) / 100; //Convierte el tiempo en distancia (m)

will be 0 for both variables, so you write again on top of what was displayed previously. So I write in red what you send to the screen, but the blue has not been erased so you get

distanciaCM: 0cmm
distanciaCM: 0m87m

of course there is no color on your screen, so it will look weird

PS/ the code about ultimaDistancia is weird as you still display anyway the distance later on so your not found thingy goes away super quickly