I have no idea why this code not working

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 255 String colorChoice; //Will hold users input of color choice

void setup() { // put your setup code here, to run once:

Serial.begin(9600); //Turn on Serial port pinMode(redPin, OUTPUT); //Set redPin to be an output pinMode(greenPin, OUTPUT); //Set greenPin to be an output pinMode(bluePin, OUTPUT); //set bluePin to be an output

}

void loop() {

Serial.println("What color would you like the LED? (red, green, or blue)"); //Prompt user for color while (Serial.available()==0) { } //Wait for input colorChoice = Serial.readString();

if (colorChoice=="red") {

analogWrite(redPin, brightness); //turn on red pin analogWrite(greenPin, 0); //turn off green pin analogWrite(bluePin, 0); //write off blue pin }

if (colorChoice=="blue") {

analogWrite(redPin, 0); //turn off red pin analogWrite(greenPin, 0); //turn off green pin analogWrite(bluePin, brightness); //write on blue pin }

if (colorChoice=="green") {

analogWrite(redPin, 0); //turn off red pin analogWrite(greenPin, brightness); //turn on green pin analogWrite(bluePin, 0); //write off blue pin }

if (colorChoice!="red" && colorChoice!="green" && colorChoice != "blue") { Serial.println("That is not a valid color choice, please try again"); Serial.println(""); }

}

I open serial monitor and type red for example. I receive message That is not a valid color choice, please try again. Can you spot any mistakes on this? P.s The circuit itself is working. But code not

Use code tags to post code please, read how to use the forum Use correct comments int brightness=255; //Set brightness to [color=red]75[/color]

But I think your pb is that you most likely have \r\n characters at the end of what you get in the colorChoice variable depending on how you set up your console. What do you send when you press enter ? It's indicated next to the baud rate setting. You either need to get rid of those by not sending them but slow reading then because need timeout or add the chars in the comparison String

It's not the best idea in the world to use the String class though - see Robin's post about Serial basics

He won't see very well the \r\n this way... need to put something before and after printing colorChoice to notice the invisible characters

I get exactly what I have typed.

laikiux: I get exactly what I have typed.

so what's the problem?

Sure you most likely get exactly what you have typed and this is not what you are testing against... See my answer #3 and #1

I solved this problem by choosing no line ending next to baund rate settings. Thank you very much!

Sure if you iterate through each char in the c_str() representation of the String object

  if (colorChoice.startsWith ("red")) ....

avoids having to worry about line endings.

Personally, I'd just use .trim() and be done with it. That, and maybe .toUpperCase().

personally I would enter r for RED g for GREEN and b for blue and only deal with one single char read and ignore everything else and not use the String class…

const byte redPin = 11;     //set red LED pin to 11
const byte greenPin = 10;   //set green LED pin to 10
const byte bluePin = 6;     //set blue LED pin to 6

const byte brightness = 255; //Set brightness to 255

int colorChoice; //Will hold users input of color choice

void setup() {
  Serial.begin(115200);       //Turn on Serial port
  pinMode(redPin, OUTPUT);    //Set redPin to be an output
  pinMode(greenPin, OUTPUT);  //Set greenPin to be an output
  pinMode(bluePin, OUTPUT);   //set bluePin to be an output
}

void loop() {
  Serial.println("\nWhat color would you like the LED? (red (r), green (g), or blue (b))"); //Prompt user for color
  while (Serial.available() == 0); // wait for an input

  while (Serial.available()) { // handle all input
    colorChoice = Serial.read();
    Serial.print("[0x"); Serial.print((char)colorChoice, HEX); Serial.print("]");
    switch ((char) colorChoice) {
      case 'r':
      case 'R':
        analogWrite(redPin, brightness); //turn on red pin
        analogWrite(greenPin, 0); //turn off green pin
        analogWrite(bluePin, 0); //write off blue pin
        break;

      case 'g':
      case 'G':
        analogWrite(redPin, 0); //turn off red pin
        analogWrite(greenPin, brightness); //turn on green pin
        analogWrite(bluePin, 0); //write off blue pin
        break;

      case 'b':
      case 'B':
        analogWrite(redPin, 0); //turn off red pin
        analogWrite(greenPin, 0); //turn off green pin
        analogWrite(bluePin, brightness); //write on blue pin
        break;

      // these ones we ignore without error message
      case ' ':
      case '\n':
      case '\r':
        break;

      default:
        Serial.println("That is not a valid color choice, please try again");
        break;
    } // end switch
    delay(1);
  } // end handle all input
}