If-statement issue

Hello
i am having an issue with my code, for some reason my if statements are always returning false. im inputing the value of RGB myself so i know that cant be true please take a look and help.

String RGB;
String msg2="Which of the RGB LED you want on:";
String msg3="Turning on ";
int p3=7;
int p2=6;
int p1=5;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(p3,OUTPUT);
pinMode(p2,OUTPUT);
pinMode(p1,OUTPUT);
}

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

}
RGB=Serial.readString();
//Serial.println(RGB);
Serial.print(RGB);
if(RGB=="r"){
Serial.print(RGB);
digitalWrite(p3,HIGH);
digitalWrite(p2,LOW);
digitalWrite(p1,LOW);
}
if (RGB=="g"){
Serial.print(RGB);
digitalWrite(p3,LOW);
digitalWrite(p2,HIGH);
digitalWrite(p1,LOW);
}
if (RGB=="b"){
Serial.print(RGB);
digitalWrite(p3,LOW);
digitalWrite(p2,LOW);
digitalWrite(p1,HIGH);
}

}

Welcome,

I never used readString but I assume it reads everything, including the possible carriage return and newline characters at the end of the string (depending on the setting of the serial monitor), so maybe your String is really "r\n" or "r\r\n".

See here for better options Serial Input Basics - updated - Introductory Tutorials - Arduino Forum

Also see https://www.gammon.com.au/serial

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with ‘\0’ (NULL).

When using Cstrings you must use strcmp() to compare values rather than ==

Use the strstr() function to see if a cstring is contained in another cstring.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

…R

these were my modification and unfortunately its still doesn't work please double check my if statement set up. :frowning: :frowning:

char RGB;
String msg2="Which of the RGB LED you want on:";
String msg3="Turning on ";
int p3=7;
int p2=6;
int p1=5;
boolean newdata=false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(p3,OUTPUT);
pinMode(p2,OUTPUT);
pinMode(p1,OUTPUT);
Serial.println(msg2);
}

void loop() {
// put your main code here, to run repeatedly:

recdata();
func();
}
void recdata() {

if(Serial.available() > 0 )
{
RGB=Serial.read();
newdata=true;
}
}
void func(){
if(newdata==true){
if(RGB=="r"){
Serial.println(RGB);
digitalWrite(p3,HIGH);
digitalWrite(p2,LOW);
digitalWrite(p1,LOW);
}
else{Serial.println("it is not");}
if (RGB=="g"){
Serial.print(RGB);
digitalWrite(p3,LOW);
digitalWrite(p2,HIGH);
digitalWrite(p1,LOW);
}
if (RGB=="b"){
Serial.print(RGB);
digitalWrite(p3,LOW);
digitalWrite(p2,LOW);
digitalWrite(p1,HIGH);
}
newdata=false;
}
}

So, what do the serial prints show?
Edit : use single quotes,not doubles.

thank you so much. i was following a youtube tutorial and the instructor was using double so i assumed i had to.