For analysing such things you can print the received data to the serial monitor
Serial.print("#");
Serial.print(RGBcolor);
Serial.println("#");
This code has a variant of readin-in serial data
// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
#define dbgi(myFixedText, variableName,timeInterval) \
{ \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
}
#define dbgc(myFixedText, variableName) \
{ \
static long lastState; \
if ( lastState != variableName ){ \
Serial.print( F(#myFixedText " " #variableName" changed from ") ); \
Serial.print(lastState); \
Serial.print( F(" to ") ); \
Serial.println(variableName); \
lastState = variableName; \
} \
}
#define dbgcf(myFixedText, variableName) \
{ \
static float lastState; \
if ( lastState != variableName ){ \
Serial.print( F(#myFixedText " " #variableName" changed from ") ); \
Serial.print(lastState); \
Serial.print( F(" to ") ); \
Serial.println(variableName); \
lastState = variableName; \
} \
}
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *
int redPin = 5;
int greenPin = 6;
int bluePin = 9;
String RGBcolor;
String msg = "What Color Do You Want?";
void setup() {
Serial.begin(9600);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}
void loop() {
dbgi("01:",digitalRead(redPin),1000);
dbgi("01:",digitalRead(greenPin),1000);
dbgi("01:",digitalRead(bluePin),1000);
Serial.println(msg);
while (Serial.available() == 0) {}
RGBcolor = Serial.readStringUntil(0x0A);
//RGBcolor = Serial.readString();
Serial.print("#");
Serial.print(RGBcolor);
Serial.println("#");
if(RGBcolor == "red") {
Serial.println("received red");
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);
}
if(RGBcolor == "green") {
digitalWrite(redPin,LOW);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,LOW);
}
if(RGBcolor == "blue") {
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,HIGH);
}
}
//RGBcolor = Serial.readStringUntil(0x0A);
RGBcolor = Serial.readString();
This variant reads in until a terminating character (the line-ending that you disabled)
If you have the line-ending activated and it is received the serial printing looks like this
What Color Do You Want?
#red
#
What Color Do You Want?
the second double-cross in a new line indicates that there is a line-ending character in the string
without line-ending it looks like this
What Color Do You Want?
#red#
What Color Do You Want?
second double-cross in the same line
another approach would be to examine the received string if there is a line-ending-character and if yes to keep only the characters before the line-ending
then the if-condition fits
best regards Stefan