Problem Interfacing with NANO using COMport

Hello Everyone!

I am having problem talking properly to the comport using the NANO.

In the entirety of the code that i have to make, I only need characters ‘G’ and ‘g’ to GET Analog or digital signal from an input and also characters ‘S’ and ‘s’ to SET a digital or PWM output. If the character is not on the list then my program should reply something like “Invalid command”.

The short-code provided below is a part of my approximately 300 line code and is the main problem.

void loop() {
  while (Serial.available() > 0){
    Command = Serial.read();                       // read Command
   
    // will be false if command is equal to one of the specified characters
    if (Command != 'G' && Command != 'g' && Command != 'S' && Command != 's'){ 
      Serial.print(Command);
      Serial.print("\t--Invalid command\n"); 
    }
    // this is the scope where i should process more if command is valid
    else{
      Serial.print(Command);
      Serial.print("\t--Valid command\n");
    }
  }// end (Serial.available() > 0)
}// end void loop

I am getting a weird reply by sending characters ‘S’, ‘s’,‘G’, or ‘g’. Please see attached file debugging1.png
On my understanding , the reply should only be “‘S’, ‘s’,‘G’, or ‘g’ --Valid command”. It adds more than what I thought it would though and I don’t know why.

And here are the results if my command is invalid. Please see attached file. Please see attached file debugging2.png
The reply should only be “wrongCharacter --Invalid Command”, however it adds more than what I thought it would and i don’t know why.

edit:
I cant find a solution for this on google.
I have tried using Serial.flush() but still didn’t fix the problem.
Am still searching for other functions that might help.

debugging1.png

debugging2.png

You are sending a newline character with each command character you send. Your code interprets this as an invalid command.

There is a setting at the bottom of the serial monitor window to turn this off. Alternatively, add some code that ignores the carriage return and newline characters completely.

Dear Mr. Hackscribe,

I cant thank you enough!

It fixed my problem.

I was thinking that i have to put newline to terminate my command everytime :(

I realize you've figured things out, but the code below shows an alternative. Note how the while loop is gone and toupper() simplifies the code. You're code is actually about 90 bytes smaller, so you have to decide if the readability is worth the cost. Either way, it's another tool on your tool belt. After all, if your only tool is a hammer, all your problems look like a nail.

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    char Command = toupper(Serial.read());    // read Command and upper case it
    switch (Command) {
      case 'G':
        Serial.print(Command);
        Serial.print("\t--Valid command\n");
        break;
      case 'S':
        Serial.print(Command);
        Serial.print("\t--Valid command\n");
        break;
      default:
        Serial.print(Command);
        Serial.println("\tInvalid command");
        break;
    }
  }// end (Serial.available() > 0)
}