Distance Sensing Problem

Hello,

I have a project where I need to combine two distance sensors, JSN40RT ultrasonic sensor and TF Luna LIDAR, into one circuit with an LCD. The problem that I've encountered is when the sensors is not obstructed to any object in front of it, it'll display its max distance on the LCD, but when there's an object in front of it, It should've displayed like <100cm or double digit numbers. Instead, I got 3 digit numbers and it's been stuck that way after I put an object in front of it. I can't get two digit numbers after that.

For example,

Before there's an object in front it, LCD displayed:
D1: 416 D2: 409

After an object is placed in front of it, LCD displayed:
D1: 256 D2: 239
(Should be - D1: 25 D2: 23)

Here is the function code:

void dist1(){
  
    digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 
    delayMicroseconds(2); 
    
    digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging 
    delayMicroseconds(20); 
    
    digitalWrite(TRIG, LOW); // Send pin low again 
    int distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 
    
    distance= distance/58; //Convert the pulse duration to distance
                           //You can add other math functions to calibrate it well
    lcd.setCursor(0,0);
    lcd.print("D1:");
    lcd.print(distance);
    delay(100);
}

void dist2(){

     if (Serial1.available()) { //check if serial port has data input
     if(Serial1.read() == HEADER) { //assess data package frame header 0x59
     uart[0]=HEADER;
     if (Serial1.read() == HEADER) { //assess data package frame header 0x59
     uart[1] = HEADER;
     for (i = 2; i < 9; i++) { //save data in array
     uart[i] = Serial1.read();
     }
     check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
     if (uart[8] == (check & 0xff)){ //verify the received data as per protocol
     dist = uart[2] + uart[3] * 256; //calculate distance value

     lcd.setCursor(8,0);
     lcd.print("D2:");
     lcd.print(dist);

     delay(100);

     }
     }
     }
     }
}

Is there anything else I should add into the code?

The rest of it?

Please can we work hard at eradicating this construct?

your challenge is that you don't clear the LCD

so you display say 521
then you overwrite this with 30 but you did not erase the trailing 1

so what you see is 301

of course there is no color, so you can't tell that the last digit is from the previous display

print a few blank spaces after the number or clear the display before displaying a new value (check if it's the same as the previous one and don't clear the screen in that case otherwise you'll see a blinking screen)

[code]
#include <LiquidCrystal.h>
#include <SoftwareSerial.h> //header file of software serial port
SoftwareSerial Serial1(6,7); //define software serial port name as Serial1 and define pin2 as RX and pin3 
String Message;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


#define TRIG 8 //Module pins
#define ECHO 9 

int dist; //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
float temprature;
int check; //save check value
int i;
int uart[9]; //save data measured by LiDAR
const int HEADER=0x59; //frame header of data package

void setup() {
  Serial.begin(9600);
  Serial1.begin(115200); //set bit rate of serial port connecting LiDAR with Arduino
  Serial.setTimeout(50); // default is 1 second
   pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 
  pinMode(ECHO, INPUT_PULLUP);
  delay(100);
  lcd.begin(16, 2);
}

void loop() {
  if (Serial.available()) {
    Message = Serial.readString();
    }


  //delay(4);

    dist1();
    dist2();
    lcd.clear();
    delay(2000);

   /*if(Message == "10"){
    lcd.println("Speed: 10km/h");
  }else if (Message == "20"){
    lcd.println("Speed: 20km/h");
  }else if(Message == "INVALID"){
    lcd.println("NO SPEED");
    Serial.println("NO SPEED");
  }*/

  /*lcd.setCursor(0, 1);
  if(Message == "on"){
    lcd.println("Speed: 10 km/h");
    delay(100);
  }else if(Message == "NULL"){
    lcd.println("Speed: NULL");
    delay(100);
  }*/

  
}

void dist1(){
  
    digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 
    delayMicroseconds(2); 
    
    digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging 
    delayMicroseconds(20); 
    
    digitalWrite(TRIG, LOW); // Send pin low again 
    int distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 
    
    distance= distance/58; //Convert the pulse duration to distance
                           //You can add other math functions to calibrate it well
    lcd.setCursor(0,0);
    lcd.print("D1:");
    lcd.print(distance);
    delay(100);
}

void dist2(){

     if (Serial1.available()) { //check if serial port has data input
     if(Serial1.read() == HEADER) { //assess data package frame header 0x59
     uart[0]=HEADER;
     if (Serial1.read() == HEADER) { //assess data package frame header 0x59
     uart[1] = HEADER;
     for (i = 2; i < 9; i++) { //save data in array
     uart[i] = Serial1.read();
     }
     check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
     if (uart[8] == (check & 0xff)){ //verify the received data as per protocol
     dist = uart[2] + uart[3] * 256; //calculate distance value

     lcd.setCursor(8,0);
     lcd.print("D2:");
     lcd.print(dist);
     Serial.println(dist); //output measure distance value of LiDAR

     delay(100);

     }
     }
     }
     }
}
[/code]

Here is my full code. I'm still a beginner at this and am currently testing to combine them but it's messy.

I've tried adding lcd.clear() into the loop. The data displayed blinks and now I got confused.

as mentioned above...

try in dist1()

lcd.setCursor(0,0);
lcd.print("D1:");
lcd.print(distance);
lcd.print("  ");

and in dist2()

lcd.setCursor(8,0);
lcd.print("D2:");
lcd.print(dist);
lcd.print("  ");

adding some spaces after the distance

I've tried the clear one first because I didn't understood how the space thing worked. I've added the space and it worked. Thank you, it's solved