Arduino Nextion slow display

Hello,

Currently I am working on a project which contains a Nextion. I have an Arduino Nano. I sent from my pc a string with a lot of variables in it through serial. This string gets taken appart in the arduino program. But I can only send the string every 300ms. If I send it faster the variables get mixed-up. But I woul prefer that I can send the string every 200ms tops (even lower if possible).

This is my current code:

#include<Nextion.h>

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int Gear = 0;
int Speed = 0;
int ERS_Mode = 0;
int ERS_Percentage = 0;
int Car_Position = 0;
int Lapnumber = 0;
float Fuel_Tank = 0;
float Fuel_Remaining = 0;
int ledPin = 13;



boolean newData = false;

//============

void setup() {
  Serial.begin(9600);
  Serial2.begin(9600);
}

//============

void loop()
{
  delay(5);
  recvWithStartEndMarkers();

  if (newData == true)
  {
    strcpy(tempChars, receivedChars);
    parseData();

    showParsedData();

    newData = false;

  }
}

//============

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false)
  {
    rc = Serial.read();

    if (recvInProgress == true)
    {
      if (rc != endMarker)
      {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars)
        {
          ndx = numChars - 1;
        }
      }
      else
      {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker)
    {
      recvInProgress = true;
    }
  }
}

//============

void parseData()
{ // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");     // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  Gear = atoi(strtokIndx);     // convert this part to an integer

  strtokIndx = strtok(NULL, ",");
  Speed = atoi(strtokIndx);     // convert this part to a int

  strtokIndx = strtok(NULL, ",");
  ERS_Mode = atoi(strtokIndx);     // convert this part to a int

  strtokIndx = strtok(NULL, ",");
  ERS_Percentage = atoi(strtokIndx);     // convert this part to a int

  strtokIndx = strtok(NULL, ",");
  Car_Position = atoi(strtokIndx);     // convert this part to a int

  strtokIndx = strtok(NULL, ",");
  Lapnumber = atoi(strtokIndx);     // convert this part to a int

  strtokIndx = strtok(NULL, ",");
  Fuel_Tank = atof(strtokIndx);     // convert this part to a float

  strtokIndx = strtok(NULL, ",");
  Fuel_Remaining = atof(strtokIndx);     // convert this part to a float
}

//============

void showParsedData() {

  Serial2.print("Main_Laptime.txt=\"");
  Serial2.print(messageFromPC);
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  Serial2.print("Main_Gear.txt=\"");
  Serial2.print(String(Gear));
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  Serial2.print("Main_Speed.txt=\"");
  Serial2.print(String(Speed));
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  Serial2.print("Main_ERS_Bar.val=");
  Serial2.print(ERS_Percentage);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  Serial2.print("Main_ERS_Num.txt=\"");
  Serial2.print(String(ERS_Mode));
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  Serial2.print("Main_ERS_Perc.txt=\"");
  Serial2.print(String(ERS_Percentage));
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  Serial2.print("Main_Position.txt=\"");
  Serial2.print(String(Car_Position));
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  Serial2.print("Main_Lap_Num.txt=\"");
  Serial2.print(String(Lapnumber));
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  Serial2.print("Mai_Fuel_Tank.txt=\"");
  Serial2.print(String(Fuel_Tank));
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  Serial2.print("Main_Fuel_Tar.txt=\"");
  Serial2.print(String(Fuel_Remaining));
  Serial2.print("\"");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);

  if (Fuel_Remaining < 0)
  {
    Serial2.print("Main_Fuel_Tar.pco=\"");
    Serial2.print(63488);
    Serial2.print("\"");
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
  }
  else
  {
    Serial2.print("Main_Fuel_Tar.pco=\"");
    Serial2.print(1024);
    Serial2.print("\"");
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    
  }
}

I thought that my Arduino was too slow, so I bought a Teensy 4.0. When connecting this and altering my code with 2 serial connections, I saw that the same thing happened. But the Teensy displays all the variables with delay when I send the string faster then every 300ms. The delay increased a lot at a higher send frequentie (Lower ms). Under 250 ms it would display nothing.

I need to send a lot more variables to my Arduino then what I currently am sending. But I think that I need to extend the send frequentie a lot more, this is really not an option.

Does anyone have an idea what is happening and how I can fix this problem?

With kind regards.

  Serial.begin(9600);
  Serial2.begin(9600);

Move to higher serial speeds, to much higher on the PC connection.
The current setting allows 1000 characters per second.

Serial2.print(String(Gear));

There is no need to convert a variable to a String just to print it. That is completely unnecessary plus the String class is evil and should be avoided.

Thank you for your reply,

I already tried that. The Nextion can only operate on baud rate 9600.
On the pc side I already tried the baud rate of 38400. But this made no difference.

Are you sure that I don't need to convert the int to string? In my mind if I want to change the Text from the Nextion I need to send a string.

klompje5:
The Nextion can only operate on baud rate 9600.

That is not true, but I will not spoonfeed you the documentation that you seemingly did not read.

Good luck with your project.

Dear Whandall,

I am sorry that you see it that way. I've tried to change the baud rate from the Nextion, then I lost total connection to it. This made me think that there is only one baud rate allowed.

But none the less, thank you for your replies.

Okay, I've changed the baud rate from the Nextion, but still nothing.

klompje5:
Thank you for your reply,

I already tried that. The Nextion can only operate on baud rate 9600.
On the pc side I already tried the baud rate of 38400. But this made no difference.

NO, the Nextion can run up to 115,200 BAUD. You have to start at 9600 Baud, THEN issue a command to the Nextion to change its BAUD rate, THEN change the Arduino BAUD rate. Do some Googling and you'll find examples of how to do it.

Regards,
Ray L.

Okay, Now I am sure that I've set the baud rate from the Nextion high enough (115,200).
Still the delay is big. And when time progresses, the delay becomes bigger.

As if the information stacks-up and can't be displayed fast enough. So the stack becomes bigger and the process time becomes longer. (If that makes sense).

Anyone an idea?

With kind regards