programming statement command

How can I print an error Msg. at the end of each else if statement? When I put a Serial.print (Msg3); command after any of the else it statements the ERROR Msg prints No matter what color I pick even if the color is spelled right.

   int ledR=8;
   int ledG=9;
   int ledB=10;
   int motspd=3;
   String Msg1= "What color do you want  ";
   String Msg2= "You picked:   ";
   String Msg3="ERROR";
   String Yourcolor;
   int dt=3000;
   
   void setup() {
  // put your setup code here, to run once:
   Serial.begin (9600);
   pinMode (ledR,OUTPUT);
   pinMode (ledG,OUTPUT);
   pinMode (ledB,OUTPUT);}
   
   void loop() {
  // put your main code here, to run repeatedly:
  
    digitalWrite (ledR,LOW);
    digitalWrite (ledG,LOW);
    digitalWrite (ledB,LOW);
  
    Serial.println(Msg1);
    while (Serial.available()==0);{
    }
    Yourcolor=Serial.readString();


    
    if (Yourcolor=="red" || Yourcolor=="Red" || Yourcolor=="RED"){
    digitalWrite(ledR,HIGH);
   // digitalWrite(ledG,LOW);
   // digitalWrite(ledB,LOW);
    }
    
    else if   (Yourcolor!="red" || Yourcolor!="Red" || Yourcolor!="RED"){
    Serial.println(Msg3);
    }
    
    if (Yourcolor=="green" ||Yourcolor=="Green" ||Yourcolor=="GREEN"){
    //digitalWrite(ledR,LOW);
      digitalWrite(ledG,HIGH);
    //digitalWrite(ledB,LOW);
    }


    //else if (Yourcolor!="green" || Yourcolor!="Green" || Yourcolor!="GREEN"){
    //digitalWrite(ledG,LOW);}
    
    
    if (Yourcolor=="blue" ||Yourcolor=="Blue" ||Yourcolor=="BLUE"){
    //digitalWrite(ledR,LOW);
   // digitalWrite(ledG,LOW);
      digitalWrite(ledB,HIGH);}
     


   // else if (Yourcolor!="blue" ||Yourcolor!="Blue" ||Yourcolor!="BLUE"){
   // digitalWrite(ledB,LOW);}
    
   
   Serial.print (Msg2);
   Serial.println (Yourcolor);
   delay(dt);
    }

You really can't do it for each if statement because blue would trigger the error message in the if for red. You need to keep track of whether any valid color was found and show error if none was.

You need to wait until you have done ALL your tests to see if you have valid input or not. You can also simplify your code by converting the input to lowercase (or uppercase) or use the equalsIgnoreCase() function which is part of the String class.

const int ledR = 8;
const int ledG = 9;
const int ledB = 10;
int motspd = 3;
String Msg1 = "What color do you want  ";
String Msg2 = "You picked:   ";
String Msg3 = "ERROR";
const int dt = 3000;

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);
  pinMode (ledR, OUTPUT);
  pinMode (ledG, OUTPUT);
  pinMode (ledB, OUTPUT);
}

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

  digitalWrite (ledR, LOW);
  digitalWrite (ledG, LOW);
  digitalWrite (ledB, LOW);

  Serial.println(Msg1);
  while (Serial.available() == 0);
  
  String Yourcolor = Serial.readString();
  bool isValid = false;

  if (Yourcolor.equalsIgnoreCase("red")) {
    digitalWrite(ledR, HIGH);
    isValid = true;
  }

  if (Yourcolor.equalsIgnoreCase("green")) {
    digitalWrite(ledG, HIGH);
    isValid = true;
  }

  if (Yourcolor.equalsIgnoreCase("blue")) {
    digitalWrite(ledB, HIGH);
    isValid = true;
  }

  if( !isValid ) {
    Serial.print(Msg3);
  }

  Serial.print (Msg2);
  Serial.println (Yourcolor);
  delay(dt);
}

Thanks for the help. I’m new to programing and I guess I have a lot to learn

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.