Question about the comparison

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available()){
    Serial.println(Serial.readString() =='stop');
  }

}

Why the output is always 1, no matter the content I typed in ? it should be 0 right, if the input is not stop.

Single quotes?
How does that work?

but if i changes to "", even the input is stop, the output is still 0.

Which?

What is the line ending setting on the serial monitor?

The result I am expected is that when I enter stop in the serial monitor, the value of Serial.readString() =='stop' should be zero.

Can you explain your expectations, please?

What is the line-ending setting in the serial monitor?
I think I may have already asked.

probably getting a newline in the string..
try..

Serial.println(Serial.readString() =="stop\n");

good luck.. ~q

1 Like

...but it won't be zero :wink:

1 Like

then maybe..

Serial.println(!Serial.readString() =="stop\n");

happy trails.. ~q

1 Like

It works, thanks you !

1 Like

...or you could take the hint, and change the line-ending setting in the serial monitor.

1 Like

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