PROBLEM reading from serial monitor

This simple project is taken from a YouTube tutorial that I've watched many times. It's just lighting an RGB LED via imput from the serial monitor. For some reason, the light will not light with any colour I type in. The baud rate is 112500. I recently updated to Big Sur, so maybe they're connected? I know the circuit is fine because I can easily to digitalWrite commands to get any of the colours to light. It's just trying to get the Serial.readString to work...

Any help would be greatly appreciated!

In serial monitor, set line endings to "No line ending". "Red" is not equal to "Red\r\n". Make sure that the baud rate is set to 115200.

Images of code are nearly worthless. I can't copy the code into my IDE to format for readability nor test it.

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

Hi, thanks for the quick reply! I'm at work, but will try that when I get home. I'll also try and post code that can be copied to the IDE so you can test it. Thanks again.

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

The serial input basics tutorial shows how to read serial data using non-blocking methods and without using the problematic String class.

It would be good to add these two lines after the
myColour = Serial.readString();
They will allow your compares to work even if someone accidentally adds a space before or after or accidentally types in upper or mixed case.

myColour.trim();  // Remove leading and trailing spaces
myColour.toLowerCase();

Thanks! I'm still very new to this. I guess your idea is much easier than adding multiple cases of the same answer:
if (myColour=="red") || (myColour=="RED") || (myColour=="Red")
etc.

Then someone will have the shift-lock on and get "rED" when trying to type "Red"... :slight_smile:

1 Like

Following on from @johnwasser solution
you could also look at my tutorial Arduino Software Solutions
which has various sketches for reading from Serial, with their pros and cons.
Also check out Taming Arduino Strings

int RedLED = 11;
int GreenLED = 10;
int BlueLED = 9;
String msg = "What colour do you want?";
String myColour;

void setup() {
// put your setup code here, to run once:
pinMode (RedLED, OUTPUT);
pinMode (GreenLED, OUTPUT);
pinMode (BlueLED, OUTPUT);
Serial.begin (115200);
}

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

}
myColour = Serial.readString();

if (myColour == "red") {
digitalWrite (RedLED, HIGH);
digitalWrite (GreenLED, LOW);
digitalWrite (BlueLED, LOW);
}
if (myColour == "green") {
digitalWrite (RedLED, LOW);
digitalWrite (GreenLED, HIGH);
digitalWrite (BlueLED, LOW);
}
if (myColour == "blue") {
digitalWrite (RedLED, LOW);
digitalWrite (GreenLED, LOW);
digitalWrite (BlueLED, HIGH);
}
}

After all that fuss, it was the "no line ending" on the serial screen. Thank you so much for your insight. I was driving myself crazy for 2 days trying to figure this out!

@mirapirate please learn to post code properly using code tags to make it easy to examine and copy

See the advice in How to get the best out of this forum

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