I am having issues with IF Strings

Hi all, I am brand new to Arduino, and have been following Paul McWhorter tutorials. All has been good so far, but I am now experiencing issues. I have checked and checked everything but must be missing something.

I have successfully used strings via serial printer, but when it comes to asking what colour LED, it will not turn on the LED using the IF strings.

Please help, I have been really enjoying the tutorials so far, but this is frustrating.

It asks what colour, but when I type an answer, nothing happens in the circuit. I have proven the circuit so it has to be my code.

My code is as follows:
int redpin=8;

int greenpin=9;

int bluepin=10;

String mycolour;

String msg="What colour do you want?";

void setup() {

// put your setup code here, to run once:

Serial.begin(9600);

pinMode(redpin,OUTPUT);

pinMode(greenpin,OUTPUT);

pinMode(bluepin,OUTPUT);

}

void loop() {

// put your main code here, to run repeatedly:

digitalWrite(greenpin,HIGH);

delay(100);

digitalWrite(greenpin,LOW);

delay(100);

Serial.println(msg);

while (Serial.available()==0) {

}

mycolour=Serial.readString();

if (mycolour=="red") {

digitalWrite(redpin,HIGH);

digitalWrite(greenpin,LOW);

digitalWrite(bluepin,LOW);

}

if (mycolour=="red") {

digitalWrite(greenpin,HIGH);

digitalWrite(redpin,LOW);

digitalWrite(bluepin,LOW);

}

if (mycolour=="red") {

digitalWrite(bluepin,HIGH);

digitalWrite(greenpin,LOW);

digitalWrite(redpin,LOW);

}

if (mycolour=="off") {

digitalWrite(redpin,LOW);

digitalWrite(greenpin,LOW);

digitalWrite(redpin,LOW);

}

}

Help us help you.

Serial.println(msg);

What does that show?

Check the line ending control on the serial monitor.

It should be "no line ending"

I am sorry, new to all this. I will try and add code the way you said. I am experienced in electronics but the computer side is pretty new.

What does the serial print out show and I think post#4 beat me to the asnwer.

It was wrong. Thank you. I now have something happening but now pin 10 goes high every time and no pin 8 or 9.

You only test for "red" and "off"

I is the message above "What colour do you want?"

Hi,
modify this line: mycolour=Serial.readString();
so that it looks like this:
mycolour = Serial.readStringUntil(0xA);
and see if it works.

lol, oh yes. I will rectify and see what happens. Thanks

Thank you for all the help. There were copy and paste errors, which I did not pick up on until the serial monitor setting was correct.

It's not really "correct", just "convenient".

There are better ways of handling line endings, like @ruilviana's suggestion (but depends on what the serial monitor is set to)

Hi,
you're really right:
If the monitor is set to "No line endig", my suggestion will not work;
If it is configured as "New Line", it will work correctly;
If configured as "Carriage return" or Both CR & NL
the line should be:
mycolour = Serial.readStringUntil(0xD);

You can use "startsWith" too

if (mycolour.startsWith("off")) {
...
}

Did he fail to make a huge point of matching baud rate and knowing what is the appropriate setting for line endings in the serial monitor?

Just curious, ppl generally like the ice tea guy and seem to make progress with him.

a7

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