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.