Echo in Serial monitor

So just working with a very simple sketch using a Arduino Nana flashed to Uno . Not sure if that matters or not but what I am seeing is that I get what I expect for the most part from the serial monitor but what I call an echo. The sketch just ask which led color do you want to turn on and when you have answered the question properly it in deed turns on the led and prints the message "you selected red led" which is fine but then the next comes up with the question as it should but it also shows the color selected in the question. example :
What color do you want ?
You selected Red Led
What color do you want ? red
Doesn't affect the circuit but just looks bad in the serial monitor.
Serial monitor has to be set No Line Ending and baud rate doesn't affect anything , send it to serial monitor as a Uno or a Nano does not make a difference. I am sure it's the way I am writing the code, can someone point me in the right direction?

edited to remove space at the begining of the text, this is interpreted as code otherwise and makes the post unreadable. Please just type your text aligned to the left

String myColor;
int redLed = 8;
int blueLed = 7;
int greenLed = 6;
String msg = " What color do you want ?";
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(redLed, OUTPUT);
}

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


  if (myColor == ("red")) {
    digitalWrite(redLed, HIGH);
    Serial.println(" You Selected Red Led");
  }
}

get rid of the line ending in the Serial Monitor (don't send CR or LF or both - just nothing and let the Serial.readString time out to get the message)

this is not a great way to listen to the Serial port though. I would suggest to study Serial Input Basics to handle this

Ok I may be missing something in your response, I have serial monitor set to No Line Ending.
Are you saying I need to do something in the code ?

edited to remove space at the begining of the text, this is interpreted as code otherwise and makes the post unreadable. Please just type your text aligned to the left

I’m not sure exactly what you’re asking, but if you’re wondering why it displays the most recent myColor input after the prompt, this is the reason.

Perfect, thanks, figured it had to do with the way I wrote the code.

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