[SOLVED]Dfficulties with turning on LED's based on a String input from the user.

Hello, good people.

EXPECTED OUTCOME:
I am brand new to learning Arduino. I'm having difficulties with having LED's light up based upon the input the user gives in the Serial Monitor. The outcome that I'm attempting to make happen is the Serial Monitor asks the user to type RED, YELLOW, or GREEN. When the user inputs a color, the corresponding LED lights up.

SETUP:
I have a red, yellow, and green LED attached to a breadboard with 330 ohm resistors. The Arduino I'm using is a Mega 2650. I have the wires hooked up to three PWM slots. I first made sure that the LED setup is working. I set all 3 LEDs to HIGH with no input from the user, and all of the LEDs lit up just fine. The baud rate is set to 9600 on the Serial Monitor as well. I coded the sketch to have one LED on at a time just to make sure that I declared each output slot on the Arduino correctly (meaning I wanted to make sure I didn't have "int redLED = 6;" when it's actually the green LED that's wired to slot 6).

FIRST ATTEMPT AT HOPED-FOR OUTCOME:
After I made sure all of that is working properly, I coded the sketch to where it will ask the user for an input of a String and light up the corresponding LED while having the other 2 turn off. When I type RED, YELLOW, or GREEN, none of the LED's light up. I made sure that I typed the response in all CAPS since that's how I set up the IF statements.

TROUBLESHOOTING TO SEE IF THE COMMAND TO TURN ON LEDs EXECUTES AFTER ANY INPUT:
What I did next was have it ask the user for an input, turn on all of the LEDs regardless of the input just to see if it's executing any code after the input command. All of the LED's turned on when I typed anything.

TROUBLESHOOTING TO SEE IF ANYTHING WITHIN THE "IF" COMMAND IS EXECUTED:
I then added an output of "Hello" after the LED's turned on just to make sure it was outputting something, and then I added the IF statements after the "Hello" output. The Serial Monitor asks the user for the input, I typed a color, all of the LEDs turned on, the word "Hello" displayed, and I expected two of the lights to turn off after the output of "Hello" while keeping on the LED of the color I typed. All of the LEDs just stayed on. I then have it coded to where it asks for a color, turns on the corresponding LED, and then prints the input of the string just to see if it was accepting the input. When I type a color, no LEDs turn on and no text prints in the Serial Monitor to show what color I typed.

CONCLUSION:
I have a circuit set up on the site TinkerCAD of this setup (except it's an Arduino UNO) and it's working just fine. I even copied and pasted exactly what I have on TinkerCAD, and it still won't work. Sorry if this description ends up not being clear to anyone who reads it. I've re-read it several times and tried to make it as clear as I could. I'll provide any clarification if needed. Is anyone able to offer guidance on where I'm going wrong with this? Thanks a bunch.

int delayChoice = 1000;
int redLED = 7;
int greLED = 6;
int yelLED = 5;
String askColor = "Please enter RED, GREEN, or YELLOW: ";
String color;

void setup(){
Serial.begin(9600);
  pinMode(redLED, OUTPUT);
  pinMode(greLED, OUTPUT);
  pinMode(yelLED, OUTPUT);
}

void loop(){
Serial.println(askColor);
  while (Serial.available() == 0){
  }
  
  color = Serial.readString();
  
  if (color == "RED"){
    digitalWrite(yelLED, LOW);
    digitalWrite(greLED, LOW);
    digitalWrite(redLED, HIGH);
    delay(delayChoice);
    }
  
  if (color == "YELLOW"){
    digitalWrite(greLED, LOW);
    digitalWrite(redLED, LOW);
    digitalWrite(yelLED, HIGH);
    delay(delayChoice);
  }
  
  if (color == "GREEN"){
    digitalWrite(yelLED, LOW);
    digitalWrite(redLED, LOW);
    digitalWrite(greLED, HIGH);
    delay(delayChoice);
  }
}
if (color == "RED"){

should be

if (color.compareTo("RED") == 0) {

Welcome to the forum and thank you for using code tags, but your large slab of text is difficult to read and take in. If you edit it and beak it into paragraphs I would expect that you to get more replies because people will read it

UKHeliBob:
Welcome to the forum and thank you for using code tags, but your large slab of text is difficult to read and take in. If you edit it and beak it into paragraphs I would expect that you to get more replies because people will read it

Thank you. I'm unsure as to what can be omitted and/or broken into segments without ruining context. To me, since I'm new, it seems that ALL of the text is important to paste at once since everything seems contingent upon everything else in the code. I'll try to see how I can break it down and then edit the post.

---EDIT----
I realize now you're referring to the text where I'm describing the issue. That I can probably break apart easier than the code.

In serial monitor, set line endings to None and try your code. Serial monitor is sending a '\r' and/or '\n' with your input with line endings not set to None. "RED\r\n" is not equal to "RED".

I do not think that there is a compareTo() function in C++. At least Cplusplus.com does not find it. The == comparison works with Strings.

line ending.jpg

groundFungus:
The == comparison works with strings.

for ANSI C strings, we have

#include <string.h>
if (strcmp (color,"RED") == 0) {

or

#include <string.h>
if (strncmp (color,"RED", 3) == 0) {

but the line

if (color == "RED"){

will not work under any circumstances.

The == comparison will not work with strings (null terminated character arrays), agreed.

The == comparison will work with String objects.

Try this modified code from the OP. Set line endings in serial monitor to none, first.

int delayChoice = 1000;
int redLED = 7;
int greLED = 6;
int yelLED = 5;
String askColor = "Please enter RED, GREEN, or YELLOW: "; // String object
String color;  // String object

void setup()
{
   Serial.begin(9600);
   pinMode(redLED, OUTPUT);
   pinMode(greLED, OUTPUT);
   pinMode(yelLED, OUTPUT);
}

void loop()
{
   Serial.println(askColor);
   while (Serial.available() == 0)
   {
   }

   color = Serial.readString();
   Serial.print("<");
   Serial.print(color);
   Serial.println(">");
   if (color == "RED")
   {
      Serial.println("chose red");
      digitalWrite(yelLED, LOW);
      digitalWrite(greLED, LOW);
      digitalWrite(redLED, HIGH);
      delay(delayChoice);
   }

   if (color == "YELLOW")
   {
      Serial.println("chose yellow");
      digitalWrite(greLED, LOW);
      digitalWrite(redLED, LOW);
      digitalWrite(yelLED, HIGH);
      delay(delayChoice);
   }

   if (color == "GREEN")
   {
      Serial.println("chose green");
      digitalWrite(yelLED, LOW);
      digitalWrite(redLED, LOW);
      digitalWrite(greLED, HIGH);
      delay(delayChoice);
   }
}

I'm unsure as to what can be omitted and/or broken into segments without ruining context. To me, since I'm new, it seems that ALL of the text is important to paste at once since everything seems contingent upon everything else in the code. I'll try to see how I can break it down and then edit the post.

I am not suggesting removing anything, just splitting it into logical paragraphs. Something like this

I am brand new to learning Arduino. I'm having difficulties with having LED's light up based upon the input the user gives in the Serial Monitor.

The outcome that I'm attempting to make happen is the Serial Monitor asks the user to type RED, YELLOW, or GREEN. When the user inputs a color, the corresponding LED lights up. I have a red, yellow, and green LED attached to a breadboard with 330 ohm resistors. The Arduino I'm using is a Mega 2650. I have the wires hooked up to three PWM slots.

I first made sure that the LED setup is working. I set all 3 LEDs to HIGH with no input from the user, and all of the LEDs lit up just fine. The baud rate is set to 9600 on the Serial Monitor as well. Then I coded the sketch to have one LED on at a time just to make sure that I declared each output slot on the Arduino correctly (meaning I wanted to make sure I didn't have "int redLED = 6;" when it's actually the green LED that's wired to slot 6).

After I made sure all of that is working properly, I coded the sketch to where it will ask the user for an input of a String and light up the corresponding LED while having the other 2 turn off. When I type RED, YELLOW, or GREEN, none of the LED's light up. I made sure that I typed the response in all CAPS since that's how I set up the IF statements.

What I did next was have it ask the user for an input, turn on all of the LEDs regardless of the input just to see if it's executing any code after the input command. All of the LED's turned on when I typed anything.

I then added an output of "Hello" after the LED's turned on just to make sure it was outputting something, and then I added the IF statements after the "Hello" output and command of all the LED's turning on.

The Serial Monitor asks the user for the input, I typed a color, all of the LEDs turned on, the word "Hello" displayed, and I expected two of the lights to turn off after the output of "Hello" while keeping on the LED of the color I typed. All of the LEDs just stayed on.

I then have it coded to where it asks for a color, turns on the corresponding LED, and then prints the input of the string just to see if it was accepting the input. When I type a color, no LEDs turn on and no text prints in the Serial Monitor to show what color I typed.

I have a circuit set up on the site TinkerCAD of this setup (except it's an Arduino UNO) and it's working just fine. I even copied and pasted exactly what I have on TinkerCAD, and it still won't work.

Sorry if this description ends up not being clear to anyone who reads it. I've re-read it several times and tried to make it as clear as I could. I'll provide any clarification if needed.

Is anyone able to offer guidance on where I'm going wrong with this?

Thanks a bunch.

UKHeliBob:
I am not suggesting removing anything, just splitting it into logical paragraphs. Something like this

My mistake. I realized what you meant after I posted my response. I did edit it a bit and added headers to the paragraphs. Thank you.

I did edit it a bit and added headers to the paragraphs.

That looks much better and I will forgive you for modifying a post that I had already commented on that makes nonsense of that reply. Please don't do that again

groundFungus:
In serial monitor, set line endings to None and try your code. Serial monitor is sending a '\r' and/or '\n' with your input with line endings not set to None. "RED\r\n" is not equal to "RED".

I do not think that there is a compareTo() function in C++. At least Cplusplus.com does not find it. The == comparison works with Strings.

line ending.jpg

Not sure what happened to my reply to this from a few minutes ago, but here's attempt 2 at this reply. Thank you! That worked and everything is good with the set-up. I'm going to read more about those drop-down options since I didn't even realize there was an option for that drop-down.

The String class has the trim() function that can be used to eliminate any non-characters from the end of the String. Use trim() as shown below and the line ending setting is irrelevant.

   color = Serial.readString();   
   color.trim();

    if (color == "RED")
   {

Karma for a good OP and for marking the thread as Solved.