Turn on LED with String from Serial Monitor

Hello Guys

I want to turn the LED on with some String codes from Serial Monitor, but unfortunately, my code is not working for turning the LED on.
I checked all LEDS and wires well in the breadboard.
I am using Arduino UNO and Arduino IDE 2.1.0.
can you please help me?

int BL=6;
int GL=5;
int YL=4;
int RL=3;
String Numb;
String M1="What is your Color? ";

void setup() {
// put your setup code here, to run once:
pinMode(RL, OUTPUT);
pinMode(YL, OUTPUT);
pinMode(GL, OUTPUT);
pinMode(BL, OUTPUT);

Serial.begin(115200);
}

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

}
Numb=Serial.readString();
Serial.println(Numb);

if(Numb== "Red"){
digitalWrite(RL, HIGH);
digitalWrite(YL, LOW);
digitalWrite(GL, LOW);
digitalWrite(BL, LOW);
}

if(Numb== "Blue"){
digitalWrite(RL, LOW);
digitalWrite(YL, LOW);
digitalWrite(GL, LOW);
digitalWrite(BL, HIGH);
}

if(Numb== "Green"){
digitalWrite(RL, LOW);
digitalWrite(YL, LOW);
digitalWrite(GL, HIGH);
digitalWrite(BL, LOW);
}

if(Numb== "Yelow"){
digitalWrite(RL, LOW);
digitalWrite(YL, HIGH);
digitalWrite(GL, LOW);
digitalWrite(BL, LOW);
}
}

Check what the line-ending setting in the serial monitor is
It should be "no line-ending".

(Or better still, look at the serial handling basics topic to see how to do it properly, without Strings)

Also. sp. "Yellow"

2 Likes

Ouh My god!
It's work.
Thank you so much for your time.
Can you please explain what is different between "No Line Ending" and "New Line?"

If you have "no line ending", all that is sent is the string ("Red", for example) that you entered.
If you have, for example "newline", then an invisible newline character is appended.
readString sees this, and so the String comparison fails

The downside of using no line ending, is that you have to rely on the timeout of readString, so the code isn't as responsive.

2 Likes

No line ending

As a native English-speaker, I prefer the presence of the hyphen.
(See what I did there?)

The IDE is an International scheme.

I'm trying to educate the poor, benighted foreigners.

"Line Ending", as the name suggests, is what gets sent at the end of a line.

The common options are:

  • None - nothing at all is added to mark the end of a line;
  • CR (Carriage Return) only - a CR character (ASCII code 13, 0x0D) is added to mark the end of a line;
  • LF (Line Feed) only - a LF character (ASCII code 10, 0x0A) is added to mark the end of a line;
  • CR and LF - a CR and a LF character (usually in that order; hence "CRLF") are added to mark the end of a line.

In a terminal program, the "Line Ending" is added when you press the 'Enter' key.

On Windows, the standard Line Ending is CRLF (ie, CR followed immediately by LF);
On Linux, the standard Line Ending is LF only.

1 Like

I am very very appreciative of your response and help.
It's so a great lesson for me.

Or you have to manually add some other way to identify the end of a "message" ...

1 Like

No, the serial monitor will do that for you :roll_eyes:

Great Explanation.
I search about it and try to find more.

Not if you have it set to 'None' ?

Using String and relying on the timeout are learner-crutches.
If you're going to use the monitor, pretty soon you're going to change the line-ending to "CR", like the other grownups.

In Arduino IDE, the Line Ending options are (Fig-1):
lineEndingIDE
Figure-1:

If Both NL & CR option is selected, then the codes 0x0D (for CR = Carriage return) and 0x0A (for NL = Newline) are appended at the end of the string entered in the InputBox of Serial Monitor.

For example: if we enter 12A in the InputBox and then click on the Send Button, five frames (0x31 0x32 0x41 0x0D 0x0A) corresponding to printable characters 1 2 A and non-printable characters CR NL will be transmitted.

1 Like

No line ending

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