Lights not working

I am trying to make a program that just turns on one of three lights when their correspondent color is inputted. The circuit works fine as I set all of the lights to HIGH just to see if they would go off and they did so I think the code is the problem.

    String msg="What color light do you want";
    
    String Color;
    
    int bluepin=7;
    
    int greenpin=2;
    
    int redpin=4;
    
    void setup() {
    
    // put your setup code here, to run once:
    
    pinMode(greenpin,OUTPUT);
    
    pinMode(bluepin,OUTPUT);
    
    pinMode(redpin,OUTPUT);
    
    Serial.begin(9600);
    
    }
    
    void loop() {
    
    // put your main code here, to run repeatedly:
    
    Serial.println(msg);
    
    while(Serial.available()==0){
    
    }
    
    Color=Serial.readString();
    
    if(Color=="red"){
    
    digitalWrite(redpin,HIGH);
    
    digitalWrite(bluepin,LOW);
    
    digitalWrite(greenpin,LOW);
    
    }
    
    if(Color=="green"){
    
    digitalWrite(greenpin,HIGH);
    
    digitalWrite(redpin,LOW);
    
    digitalWrite(bluepin,LOW);
    
    }
    
    if(Color=="blue"){
    
    digitalWrite(bluepin,HIGH);
    
    digitalWrite(redpin,LOW);
    
    digitalWrite(greenpin,LOW);
    
    }
    
    }

anyone know where I went wrong?

Try printing Color immediately after you get it ... does it match what you are comparing?

Serial Input Basics - updated - Using Arduino / Introductory Tutorials - Arduino Forum

More hints:
Don't Cross The Streams (FP scientific calculator serial co-processor) - Community / Exhibition / Gallery - Arduino Forum

I printed color and it does match up to the color I type in but the lights refuse to go off

Are you sure the String doesn't contain line ending characters ?

Try to trim the received String

my guess, change colors like "red\n"

there's a newline in there..

have fun.. ~q

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