if statement not doing anything through serial port?

String colorChoice;

void loop() {
  Serial.println("What Color Would You Like (RGB)?");
  while (Serial.available()==0);{
  }
  colorChoice = Serial.readString();

  if (colorChoice=="red"){
    Serial.println("red");
    analogWrite (redPin, 0);
    analogWrite (greenPin, 255);
    analogWrite (bluePin, 255);
  }
  
}

This is a simple program for switching the colors on a Common Anode RGB. I am trying to get just the red color to work through the serial port. I know the circuit is connected OK because I was a able to get them to switch between colors inside a simple For loop. However when I try to read the serial data using a specific string, it doesn't do what it is told in the scope of the if statement and continues to loop the prompt "What Color Would You Like (RGB)?"

I am entering "red" and not "R" inside the prompt, just to be clear.

It won't even do the Serial.println("red"); when the conditional string "red" is entered. I'm lost. It compiles fine and I don't see any syntax problems here.

here is a screenshot with additional debugging. You can see it reads everything outside of the scope of the if statement but won't print "applying" or do anything else. As far as I can tell, the if statement syntax looks fine to me.

aspen1135:

String colorChoice;

void loop() {
  Serial.println("What Color Would You Like (RGB)?");
  while (Serial.available()==0);{
  }
  colorChoice = Serial.readString();

if (colorChoice=="red"){
    Serial.println("red");
    analogWrite (redPin, 0);
    analogWrite (greenPin, 255);
    analogWrite (bluePin, 255);
  }
 
}




This is a simple program for switching the colors on a Common Anode RGB. I am trying to get just the red color to work through the serial port. I know the circuit is connected OK because I was a able to get them to switch between colors inside a simple For loop. However when I try to read the serial data using a specific string, it doesn't do what it is told in the scope of the if statement and continues to loop the prompt "What Color Would You Like (RGB)?"

I am entering "red" and not "R" inside the prompt, just to be clear.

It won't even do the Serial.println("red"); when the conditional string "red" is entered. I'm lost. It compiles fine and I don't see any syntax problems here.

try this if (strstr(colorChoice, "red") != NULL) instead of if (colorChoice=="red")

Doing that gives this error:

exit status 1
cannot convert 'String' to 'const char*' for argument '1' to 'char* strstr(const char*, const char*)'

Typed:

 if (strstr(colorChoice, "red") != NULL){
    Serial.println("applying...");
    delay(1000);
    analogWrite (redPin, 0);
    analogWrite (greenPin, 255);
    analogWrite (bluePin, 255);
  }

How about you post your complete original program ?

Switch the line endings in the serial monitor to "no line ending".

UKHeliBob:
How about you post your complete original program ?

Sure, here.

//Because of the LED provided in the kit, we are forced to use some work-arounds to get the RGB working for a Common Anode LED. The original lesson material was directed for a Common Cathode.

int LEDPower = 8;
int redPin = 6;
int greenPin = 5;
int bluePin = 3;
int voltInput;
String colorChoice;

//FUNCTIONS FOR SIMPLER IMPLIMENTATION
void redColor (){
  analogWrite (redPin, 0);
  analogWrite (greenPin, 255);
  analogWrite (bluePin, 255);
}

void greenColor (){
  analogWrite (redPin, 255);
  analogWrite (greenPin, 0);
  analogWrite (bluePin, 255);
  }

void blueColor (){
  analogWrite (redPin, 255);
  analogWrite (greenPin, 255);
  analogWrite (bluePin, 0);
  }

void setup() {
  Serial.begin (2000000);
  pinMode (LEDPower, OUTPUT);
  pinMode (redPin, OUTPUT);
  pinMode (greenPin, OUTPUT);
  pinMode (bluePin, OUTPUT);
  digitalWrite (LEDPower, HIGH);


//THIS COMMENTED OUT FOR LOOP WORKS FINE, PROVING CIRCUIT'S OK
  /*
  for (int j=1; j<3; j+1){
    redColor();
    delay(100);
    greenColor();
    delay(100);
    blueColor();
    delay(100);
  }
  */
   
}



void loop() {

  Serial.println("What Color Would You Like (RGB)?");
  while (Serial.available()==0);{
  }
  colorChoice = Serial.readString();
  Serial.println(colorChoice);

  if (colorChoice=="red"){
    redColor();
  }

  if (colorChoice=="green"){
    greenColor();
  }

  if (colorChoice=="blue"){
    blueColor();
  }

  //THROW EXCEPTION IF NOT VALID

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

}

It will throws the exception even when the string is exactly "red", which it shouldn't

Whandall:
Switch the line endings in the serial monitor to "no line ending".

I'm not sure I understand. Can you elaborate?

for (int j=1; j<3; j+1){"fine" ?

AWOL:
for (int j=1; j<3; j+1){"fine" ?

yeah what's wrong with that? it actually works and flips through the RGB in a loop. This was commented out anyway. It was just to make sure the functions and circuit connectors were working 100%.

Its the if statements that aren't working.

aspen1135:
Doing that gives this error:

exit status 1
cannot convert 'String' to 'const char*' for argument '1' to 'char* strstr(const char*, const char*)'

Typed:

 if (strstr(colorChoice, "red") != NULL){

Serial.println("applying...");
    delay(1000);
    analogWrite (redPin, 0);
    analogWrite (greenPin, 255);
    analogWrite (bluePin, 255);
  }

forgot to add to be able to use "strstr" you need to change "String colorChoice;" to "char colorChoice[];"

 j+1

That's what's wrong.

Use "j++" or "j+=1"

you need to change "String colorChoice;" to "char colorChoice[];"

readString() does not work with an array of chars. More radical changes are needed in order to us an array.

AWOL:

 j+1

That's what's wrong.

Use "j++" or "j+=1"

Ah. I've heard of j++ and was even going to use it. But j+1 seemed to be more clear for overview. I havn't heard of j+=1 though. Is there a reason why you need the = there or is it just good practice for my complex math down the road?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

Robin thank you for the response. You have provided that link in the past and I have reviewed the information. However some of the context is beyond me, as I have not learned arrays yet.

This code should work perfectly fine the way it is structured now. I am following a video series and it worked working in the exact context I am following now using colorChoice = Serial.readString(); Here is the video example This shows his code and This shows it changing colors.

I have not gotten a concrete solution yet and have been trying to troubleshoot this in many different ways for the last 2-3 hours now. I want to move on to the next material but I need to fully understand why this If statement will not read the string it's provided. It compiles OK but skips the if statement altogether.

You could use

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

To separate lines CR and LF are used, which become part of the string.
You could switch these delimiters off in the serial monitor (bottom),
or just test the start of the string for "red".

aspen1135:
Robin thank you for the response. You have provided that link in the past and I have reviewed the information. However some of the context is beyond me, as I have not learned arrays yet.

Arrays are so useful that I reckon an hour or two learning about them would repay itself 10 times over.

...R

Whandall:
You could use:


To separate lines CR and LF are used, which become part of the string.
You could switch these delimiters off in the serial monitor (bottom),
or just test the start of the string for "red".

Man, I just can't seem to get this to work lol. That threw a compiler error as well.

This was your code, as copied and pasted verbatum:

if (colorChoice.startsWith==("red")){
   redColor();
 }

And this was the compiler error:

exit status 1
invalid operands of types '' and 'const char [4]' to binary 'operator=='

EDIT: OH- I'm dumb. I forgot to delete the ==.

EDIT EDIT: OK it compiles now but I get the same problem where it won't read the string "red".

HOLLLLLLY CRAP. :o

Okay. I got it now.

So Whandalls suggestion works. However it does not work without the "startsWith" command

The reason why it didn't work the first time I tried that method is because the colorChoice = Serial.readString(); was not before the if statement in the program, so the input was not yet stored in the colorChoice string yet.

However, it still does not really explain why it will not read the string straight from (colorChoice=="red") even though it has been moved before the IF statements now.

And furthermore I can't add a != or && statements to colorChoice.startsWith so I cannot throw an exception now with this method. I understand there's many ways to do this but this is the first way we are learning to throw exceptions (with an if statement). I am just stumped why it can't read the string directly without the startsWith part.