Maybe I'm asking a stupid question, sorry.
I try to communicate, using serial.read with nextion display.
I have a function to transform strings into values.
As result, as I identified, the process does not wait for the function to end and go forward.
As result, I have the first variable but doesn't have a second.
this is a string from Nextion display: &SET_TEMP|22|&
Code: Select all
String screen_action = "";
uint8_t screen_value = 0;
void setup() {
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}
void loop(){
readDisplay();
if(screen_action == "SET_TEMP"){ /// at this moment I do have screen_action = "SET_TEMP" and screen_value
action_temp = screen_value;
Serial.print("SET action_temp to ");
Serial.print(action_temp);
screen_action = "";
}
}
void readDisplay(){
if (Serial2.available()) {
inc = Serial2.read();
if(inc == '|'){
if(position==1){
screen_action = incStr;
incStr = "";
debug_println("Screen action: "+x);
}
if(position==2){
screen_value = incStr.toInt();
incStr = "";
debug_print("Screen value: ");
debug_println(y);
}
position += 1;
}else{
incStr += inc;
}
if(inc == '&'){
incStr = "";
position = 1;
}
}
}
To solve the problem, I replace screen_action and screen_value with temporary variables val1 and val2 and do
Code: Select all
if(inc == '&'){
screen_action=val1;
screen_value = val2;
incStr = "";
position = 1;
}