[SOLVED] If Statement Not Working With String - Arduino Forum Home » Arduino If Else Statement Not Working » [SOLVED] If Statement Not Working With String - Arduino Forum Maybe your like Arduino If Else While Arduino #ifndef Esp8266 Arduino Ifndef Example Arduino Ifndef Not Working Arduino If Statement [SOLVED] if statement not working with String Projects Programming Simon_Teoh August 3, 2019, 3:50am 1 Hi All, I'm new to Arduino and doing a tutorial. purpose is to input a color in Serial and on that specific color on RGB LED. I checked the input with serial print and it seems correct, but LED doesn't light up at all. it always give only feedback from last if statement saying not valid input. Can please advise what i did wrong?? int redPin=11; // Set red LED pin to 11 int greenPin=10; // Set green LED pin to 10 int bluePin=6; // Set blue LED pin to 6 int brightness = 255; // Set brightness to full String colorChoice; // Set up variable to hold user input void setup() { Serial.begin(9600); // Turn on Serial port to 9600 baud pinMode (redPin, OUTPUT); // Set pinmodes to output pinMode (greenPin, OUTPUT); // Set pinmodes to output pinMode (bluePin, OUTPUT); // Set pinmodes to output } void loop() { Serial.println("Please input your color choice red, green or blue "); // prompting user for input while (Serial.available()==0) { } colorChoice = Serial.readString (); // read string from serial port Serial.println(colorChoice); if (colorChoice == "red") { analogWrite (redPin, brightness); // Turn red on analogWrite (bluePin, 0); // Turn blue off analogWrite (greenPin, 0); // Turn green off } if (colorChoice == "green") { analogWrite (redPin, 0); // Turn red on analogWrite (bluePin, 0); // Turn blue off analogWrite (greenPin, brightness); // Turn green off } if (colorChoice == "blue") { analogWrite (redPin, 0); // Turn red on analogWrite (bluePin, brightness); // Turn blue off analogWrite (greenPin, 0); // Turn green off } if (colorChoice != "red" && colorChoice != "green" && colorChoice != "blue") { Serial.println (""); Serial.println ("You have not entered a valid color, please enter red, green or blue"); Serial.println (""); } } johnwasser August 3, 2019, 3:53am 2 You are probably including the line endings in your serial input. The name with the line ending doesn't match the name without the line ending. Set the line ending in Serial Monitor to "No line ending" to work around the problem. 8 Likes Simon_Teoh August 3, 2019, 4:01am 3 johnwasser: You are probably including the line endings in your serial input. The name with the line ending doesn't match the name without the line ending. Set the line ending in Serial Monitor to "No line ending" to work around the problem. Wow, such a simple setting, yet it took me a whole morning of trial and error. Thanks very much, it works perfectly now. 1 Like Related topics Topic Replies Views Activity I have no idea why this code not working Programming 11 1354 May 5, 2021 Problem whit this code pls help Programming 11 1720 December 14, 2021 [SOLVED]Dfficulties with turning on LED's based on a String input from the user. Programming 12 5187 May 6, 2021 My LED won't blink with if statement Programming 7 1674 December 3, 2021 Trying to get "if" statements and "Strings" working ? Programming 7 102 August 8, 2025 Tag » Arduino If Else Statement Not Working If And Else Statements, If Statement Not Working - Arduino Forum If Statement Does Not Work! What Am I Doing Wrong? - Arduino Forum If Else Statements Not Executing As Expected - Arduino Forum Why Does "if...else If...else" Not Work, But "if...if...else" Does? 2nd If Statement Not Working - Arduino Forum If Statement Not Working - Programming Questions - Arduino Forum IF Statement Doesn't Work Properly - Arduino Forum If Statement Not Working - Programming Questions - Arduino Forum If-else Statement Not Working - Arduino Stack Exchange Arduino IDE: Conditional(if-else-if) Statements - STEMpedia Arduino Project Issues With IF Statements - Stack Overflow Arduino If-else Statement - Linux Hint 2.7 Understanding If / Else And While Statement In Arduino Tutorial 11: If Statement (and Else-if), Comparison Operators And ...