serial read not working correctly

So I just started learning arduino and have been playing around with Serial.read() and Serial.readString(). I made a sketch that will turn on and off a motor based on what input comes in from the serial port. “11” turns the motor on, “22” turns it in the other direction, and “33” turns it off, and all three are supposed to print a different string to the serial monitor, and also print back the string that I input. When I upload the sketch, the motor turns on, confirming that the sketch works, but nothing happens when I give it input using a serial monitor. I even made a windows forms app that writes to the serial port to see if the serial monitor was the problem, but that does not work either. I don’t really know if it is Serial.readString(), or if I just did something stupid to the if statements. My code should be attached and any help would be appreciated. Thanks in advance!

void setup() {
  pinMode(22, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(26, OUTPUT);
  digitalWrite(22,HIGH);
  digitalWrite(24,HIGH);
  digitalWrite(26,LOW);
  Serial.begin(57600);
}

void loop() {
  String readSerial;
  if(Serial.available() > 0) {
    readSerial = Serial.readString();
    
    if(readSerial == "11") {
        digitalWrite(24,HIGH);
        digitalWrite(26,LOW);
        Serial.print("asdf");
    }  
    else if(readSerial == "22") {
        digitalWrite(24,LOW);
        digitalWrite(26,HIGH);
        Serial.print("jkl;");
    }  
    else if(readSerial == "33") {
        digitalWrite(22,LOW);
        digitalWrite(24,LOW);
        digitalWrite(26,LOW);
        Serial.write("qwerty");
    }
    Serial.print(readSerial);
  }
}

SerialReadTest.ino (782 Bytes)

Please read the how to use the forum sticky post it will tell you about posting code correctly. The point is that a lot of mobile platforms will not cope with .ino files, even though they are only text files.

Look at your line endings, there is a an option to have a line feed, carriage return or both. Make sure you are sending what you are expecting to see.

Have you tried if(Serial.available() >= 2 )

I’ve figured it out-
For anyone reading this in the future, anything sent to the serial port of an arduino is read in ASCII, meaning that the if statements did not pick up that the string was worth “11” because it was reading it as it’s ASCII format. In order for it to work, you have to convert the input from ASCII to a normal string, which is where it is easier to do integers, because you don’t have to convert as much. 1=49, 2=50, 3=51, etc, and 10 marks the end of the transmission (at least from what I saw). Hope this answers future questions! :slight_smile:

“you have to convert the input from ASCII to a normal string”

Are you positive?

You are comparing String variable to “array of characters” , not to String variable.

if( readSerial == “11”)

My bad. I was talking about Serial.read(). When a characher such as “1” is sent over and read using Serial.read(), it is read as ASCII. readString() reads it in a different way.

You might get some ideas from Serial Input Basics