Arduino Serial.readString() trouble Hello everyone I write my code and have some troubles.
When I write on in my serial monitor my buzzer condition wasn't changed.
Can you help me realize the reason. I wrote code with int type and it works with Serial.parseInt() function properly.
String ask=&quot;Please enter buzzer condition&quot;;
int buzzerPin=2;
String state;
void setup() {
  pinMode(buzzerPin,OUTPUT);
  Serial.begin(115200);

}

void loop() {
 Serial.println(ask);
 state=Serial.readString();
 Serial.println(state);
 while(Serial.available()==0){
  
 }
 if(state==&quot;on&quot;){
  digitalWrite(buzzerPin,HIGH);
 }
 if(state==&quot;off&quot;){
  digitalWrite(buzzerPin,LOW);
}
}
You need to use Serial.readStringUntil('\n') instead of Serial.readString if you use the Arduino IDE's serial monitor (where a \n is added as termination character when you press enter).
Also remove that while(Serial.available()==0). It will wait as long as there are any bytes in the serial buffer and as you don't Serial.read() within the loop, the buffer will not be emptied. So, if anything is received while Serial.println(state) is called or if you send two termination characters, the program will be stuck in that loop.