I can print initial sensor value with serial1 but this value doesnt change

Hi, I have a question I try to read sensor value and print this value with serial1. But I cant read with serial3 when read this value with serial monitor also I can read initial sensor value but this value doesnt change on serial3. How can I solve this problem(ignore error message)

// serial3 read code
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial3.begin(9600);
}
String totalstring;
char lastchar;
String hiz;
void loop() {
  // put your main code here, to run repeatedly:
while(lastchar != '\n')
    {
      if(Serial3.available())
      {
  lastchar=Serial3.read();
  totalstring=totalstring+lastchar;
  //Serial.println(lastchar);
      }
      
  //Serial.println(lastchar);
  
    }
    lastchar=' ';
    hiz=totalstring.substring(totalstring.indexOf("a"),totalstring.indexOf("k"));
   // Serial.println(hiz);
    Serial.println(totalstring.c_str());
    
}
//serial1 print code
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}

void loop() {
  int deger;
  deger =analogRead(A0);
deger=map(deger, 0, 1023, 0, 255);
Serial1.print("a");
Serial1.print(deger);
Serial1.println("k");
Serial.print("a");
Serial.print(deger);
Serial.println("k");
delay(100);
}

Welcome to the forum

Please do not post pictures of code

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

I can solve.It's because I forgot totalstring set " " bottom of code

totalstring="";

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.