Nextion display problem

Hi,

I have a nextion NX8048P070 , I use it with an ESP32 . I display a progressbar and some buttons and float values.
I use an ultrasonic sensor to animate the progressbar and receive float values.

The problem is that when the progressbar displays the value of the ultrasonic sensor, the buttons take time to respond or even do not respond.

What to do?

Here is my code:

String  endChar = String(char(0xff)) + String(char(0xff)) + String(char(0xff));
String  dfd  = ""; // data from display
// NOTE : Initial Async Delay 
unsigned  long initDelayLong = 0;// NOTE : 4,294,967,295
int initDelayLength = 50;
const int trigger = 5;
const int echoPin = 4;  
float duration, distance,liter,pourcent;
uint8_t ndt[3] = {255,255,255};
//uint16_t  progressval;
//uint16_t  progressval1;
uint16_t   progressval,progressval1,progressvalliter;


void setup(){
Serial.begin(115200);
delay(100);
  Serial2.begin(115200);
 
  pinMode(trigger, OUTPUT);
  pinMode(echoPin, INPUT);
  // Led reto
  pinMode(22,OUTPUT);
  pinMode(18,INPUT_PULLUP);
  pinMode(13,OUTPUT);
  pinMode(19,OUTPUT);
  pinMode(21,OUTPUT); 
}
void loop() {
  
    //Clear the trigger 2 micro seconds//
   digitalWrite(trigger, LOW);
   delayMicroseconds(2);
    //Fire the trigger 10 micro seconds
   digitalWrite(trigger, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigger, LOW);
    //Get the duration
   duration = pulseIn(echoPin,HIGH);
 //distance from duration
   distance = (duration / 2) * 0.0343;
   liter = -((distance-309)/0.01315068493) ;
  //en pourcentage
  pourcent=0.00456621005*liter;
   delay(50);

  Serial.print("distance (cm): ");
  Serial.println(distance);
  Serial.print("liter(L): ");
  Serial.println(liter);
  Serial.print("pourcent (%): ");
  Serial.println(pourcent);
  delay(2000);

  //maka ny caractere avy amn nextion
    if(Serial2.available()){
    lcdInput();
  }
  // NOTE : ASYNC DELAY
  if(millis() > initDelayLong){
    initDelayLong+=initDelayLength;
    initAsyncDelay();
  }
  //NOTE : SOMETHING SENT FROM NEXTION I.E. GET REQEUEST  
  if(dfd.endsWith(endChar)){
    Serial.println(dfd);
    Serial.println("error");
    dfd = "";
  }
}
 void initAsyncDelay(){
  if(digitalRead(18) == LOW)
  {
    Serial.println("OUTPUT LOW");
    digitalWrite(22,LOW);
    Serial2.print("b0.txt=\"OFF\"" + endChar);
    Serial2.print("r0.val=0" + endChar);
    digitalWrite(13,LOW);
    Serial2.print("b1.txt=\"OFF\"" + endChar);
    Serial2.print("r1.val=0" + endChar);
    digitalWrite(19,LOW);
    Serial2.print("b2.txt=\"OFF\"" + endChar);
    Serial2.print("r2.val=0" + endChar);
    digitalWrite(21,LOW);
    Serial2.print("b3.txt=\"OFF\"" + endChar);
    Serial2.print("r3.val=0" + endChar);
  }
    if(digitalRead(22) == LOW)
  {
    Serial.println("INPUT LOW");

    digitalWrite(13,LOW);
    Serial2.print("b1.txt=\"OFF\"" + endChar);
    Serial2.print("r1.val=0" + endChar);
    digitalWrite(19,LOW);
    Serial2.print("b2.txt=\"OFF\"" + endChar);
    Serial2.print("r2.val=0" + endChar);
    digitalWrite(21,LOW);
    Serial2.print("b3.txt=\"OFF\"" + endChar);
    Serial2.print("r3.val=0" + endChar);
  }

  progressval = pourcent;
if(progressval!=pourcent){
  progressval=pourcent;
  Serial2.print("j0.val=");
  Serial2.print(progressval);
  Serial2.write(ndt,3);
 
 

if(progressval<35){
  Serial2.print("j0.pco=63488" );
  
Serial2.write(ndt,3);
Serial2.print("t0.txt=\"niveau bas\"" );
Serial2.write(ndt,3);
}else if (progressval<80){
 Serial2.print("j0.pco=65504" );
 
Serial2.write(ndt,3);
Serial2.print("t0.txt=\"Niveau normal\"" );
Serial2.write(ndt,3);
}else  {
Serial2.print("j0.pco=1024" );
Serial2.write(ndt,3);
Serial2.print("t0.txt=\"Niveau haut\"" );
Serial2.write(ndt,3);
}
}
progressval1=pourcent*10;
if(progressval1!=pourcent*10){
progressval1=pourcent*10;
Serial2.print("x0.val=");
Serial2.print(progressval1);
Serial2.write(ndt,3);   
}
progressvalliter=liter;
if(progressvalliter!=liter){
  progressvalliter=liter;
 Serial2.print("x1.val=");
 Serial2.print(progressvalliter);
 Serial2.write(ndt,3);
  

}
  }
void lcdInput(){
  dfd += char(Serial2.read());
  // NOTE : COMMAND is 3 characters after C:C
  // NOTE : RESET dfd if THREE characters received and not C:C
  if(dfd.length()>3 && dfd.substring(0,3)!="C:C") dfd="";
  else
  {
    // NOTE : If string ends in a ? then command completed
    if(dfd.substring((dfd.length()-1),dfd.length()) == "?"){
      // NOTE : Get the command
      String command = dfd.substring(3,6);
      // Reception value, soit string soit int
      String value = dfd.substring(6,dfd.length()-1);
      // test
      Serial.println(command + " : " + value);
      // envoi cmd sy execution
      if(command == "LE0"){
        value == "ON"?digitalWrite(22,HIGH):digitalWrite(22,LOW);
      }
      if(command == "LE1"){
        value == "ON"?digitalWrite(13,HIGH):digitalWrite(13,LOW);
      }
      if(command == "LE2"){
        value == "ON"?digitalWrite(19,HIGH):digitalWrite(19,LOW);
      }
      if(command == "LE3"){
        value == "ON"?digitalWrite(21,HIGH):digitalWrite(21,LOW);
      }

      dfd="";
    }
  }
}

@rickley75

This Topic has been locked as it is the identical or too similar your existing topic Nextion , Ultrasonic sensor with ESP 32

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.