Problem whit this code pls help

!!!!!
when i input a color(like bleu or green or red ) to the arduino the if loops doesnt seem to think that the condition is verified even though the speling is not wrong .
!!!!!!

int redled=10;
int bleuled=9;
int greenled=8; 
String chosen;
int j;
String msg1="what led would you like to light up .";
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(redled,OUTPUT);
pinMode(bleuled,OUTPUT);
pinMode(greenled,OUTPUT);
}
void loop() {
  Serial.println(msg1);
  while(Serial.available()==0){}
  chosen=Serial.readString();
  Serial.println(chosen);
      if(chosen=="green" ){
    digitalWrite(greenled,HIGH);
    digitalWrite(redled,LOW);
    digitalWrite(bleuled,LOW);
    j=1;
    }
      if(chosen=="red" ){
    digitalWrite(redled,HIGH);
    digitalWrite(greenled,LOW);
    digitalWrite(bleuled,LOW);    
    j=1;
    } 
      if(chosen== "bleu" ){
        digitalWrite(redled,HIGH);
        digitalWrite(redled,LOW);
        digitalWrite(greenled,LOW);
        j=1;
    }
    Serial.println(j);
      if(j!=1){
        Serial.println("invalid color please chose from the folowing :");
        Serial.println("bleu,red,green");
         digitalWrite(redled,LOW);
         digitalWrite(greenled,LOW);
         digitalWrite(bleuled,LOW);
        
        }
}
1 Like

Change the line ending setting on the serial monitor to "none"

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

thx you very much

thx i will try to properly post it next time

You can go back and edit your original post to include code tags. Just highlight the code and click on the </> in the menu bar. It is that simple.

thx

Well done, thank you.

are you certain about the commands for the "bleu" light ?

if(chosen== "bleu" ){
digitalWrite(redled,HIGH);
digitalWrite(redled,LOW);
digitalWrite(greenled,LOW);
j=1;

??
bleu is blue in french
??

ah i see i fixed the error thx anyway

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