Serial.readString() not working

int redPin=5;
int greenPin=6;
int bluePin=7;
String myColour;
String msg="Colour pls:";

void setup() {
  Serial.begin(9600);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {

  Serial.println(msg);
  while (Serial.available()==0) {
  }

  myColour = Serial.readString();

  if (myColour=="red") {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  }

    if (myColour=="green") {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, LOW);
  }

    if (myColour=="blue") {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, HIGH);
  }
}`Use code tags to format code for the forum`

not sure why but my Serial.readString() command is not working. The IDE shows no syntax mistakes, have tried different pins with the RGB Led as well as the 5V pin where everything is working individually yet once i try to use the if statements to input a colour, the LED doesn't light up.

Welcome to the forum

What have you got the Line Ending set to in the Serial monitor ?

Have you tried printing the length of the String that you receive and does it match what you expect ?

myColour = Serial.readString();
Serial.print("Received [");
Serial.print(myColour);
Serial.println("]");

might give you some ideas.

It sounds like we are thinking the same thing :grinning:

Likely needs indeed something like

myColour = Serial.readString();
myColour.trim(); // get rid of « spaces » at start and end 

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