Comparing a String after reading it from Serial fails I have bought an Arduino starter kit online, everything seem to work but when I try for example to use ifs (and other conditional statements), the LEDs do not turn on. Here is a code for a little project I am currently working on:
int redpin = 3;
int greenpin = 2;
int bluepin = 4;
String color;
String msg = &quot;Insert color: &quot;;

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

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(msg);
  while (Serial.available() == 0) {}
  color = Serial.readString();

  if (color == &quot;red&quot;) {
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
    digitalWrite(bluepin, LOW);
  } 
  if (color == &quot;green&quot;) {
    digitalWrite(greenpin, HIGH);
    digitalWrite(redpin, LOW);
    digitalWrite(bluepin, LOW);
  }
  if (color == &quot;blue&quot;) {
    digitalWrite(greenpin, LOW);
    digitalWrite(redpin, LOW);
    digitalWrite(bluepin, HIGH);
  }

  if (color == &quot;off&quot;) {
    digitalWrite(greenpin, LOW);
    digitalWrite(redpin, LOW);
    digitalWrite(bluepin, LOW);  
  }
}

I tried turning on the RGB LED with single digitalWrite() functions and it works. I think that the if statements in the code are the problem.
I`m using an Arduino MEGA 2560 BTW.
After color = Serial.readString(); color also contains a newline character.
You can remove that by adding the line color.trim();, or even better, avoid the newline altogether by using Serial.readStringUntil('\n'); which discards the terminating newline character.
Also, you could, and maybe should, avoid using the String class and use:
char color[10]; // char array as buffer for reading from Serial            

int number = Serial.readBytesUntil('\n', color, 10); //Read input and length
color[number] = '\0'; //Terminate the string by adding '\0' at its end

if (strcmp(color, &quot;red&quot;) == 0) //Use strcmp(), not ==

respectively.