Serial Monitor issue

Why does this not work?

int axleOn= 0;

void setup() {
Serial.begin(9600);

}

void loop() {

while(Serial.available() > 0){
String a=Serial.readString();
if(a == "axle"){
Serial.println("axle");
axleOn=1;
}
}
}

This code should in theory print axle when it is typed. Why does it not work, and is there a better way to do this?

What is the serial line-ending set to?

See if the length of the String is 4. You might have to strip control codes.

are you getting any value for the variable 'a'? see what it prints outside the if statement.

Shair76:
Why does this not work?

int axleOn= 0;

void setup() {
Serial.begin(9600);

}

void loop() {

while(Serial.available() > 0){
String a=Serial.readString();
if(a == "axle"){
Serial.println("axle");
axleOn=1;
}
}
}

This code should in theory print axle when it is typed. Why does it not work, and is there a better way to do this?

You neglect to say what it is doing...

The serial monitor does not send as you type, only on a newline, BTW.
You assume readString() knows to stop after 4 characters, which it doesn't/can't

If you correctly configure your Serial Monitor to send a new line, your code actually works. But, you should just test for data available in Serial like this:

int axleOn= 0;

void setup() {
Serial.begin(9600);

}

void loop() {

  if(Serial.available())
  {
    String a=Serial.readString();
    if(a == "axle")
    {
      Serial.println("axle");
      axleOn=1;
    }
    else
    {
      Serial.println("NOT axle");
    }
  }
}

remember that readString() times out in one second by default, and will block until that second is up.