[solved] Testing for a character input via serial monitor

New to Arduino, I'm trying to figure out how to test a character input I make via the serial monitor in the Arduino IDE.

I think my problem is with the input == "u", hopefully it's an easy fix to make this comparison properly.

void loop() {

  if (Serial.available()) {
    input = Serial.read();
    Serial.print("You typed: '");
    Serial.print(input);
    Serial.println("'");


    if (input == "u") {  // strings match
      Serial.println(" got a u");
    } else {
      Serial.println("was not interesting");
    }
    delay(1000);
  }
}

The code that you posted does not include the declaration of the input variable

My guess is that it is a char. If so then use

if (input == 'u')

to do the comparison

Please post your complete sketch

1 Like

'u' not "u"

1 Like

'u' instead of "u" fixed it. Thank you very much.

Do you understand what the problem was ?

In future please post your whole sketch if you have a question

1 Like

Do you understand what the problem was ?

Other than the syntax, no I do not. It was declared as char as you guessed. I do not understand the difference in 'u' and "u".

'u' is a single character. A char can only ever be a single character

"u" is a 2 character array. 1 character for the string of characters, in this case a single char, and a second one to hold the zero that signals the end of the string. A C style string can hold multiple characters

chars are compared using ==
strings are compared using the strcmp() function

1 Like

'u' is a character and "u" is a string. Does that make sense to you?

1 Like

Thank you for that detail, much appreciated. For my reference and education, can you show me how a comparison would look using strcmp and the string "uax" for example. Would it be:


if (strcmp(input),"uax") == 0) { 
//True, do stuff 
}

I saw strncmp too when I was trying to get this to work originally, but didn't have any sucess with it.

The syntax that you are looking for is

if (strcmp(input, "uax")) == 0  //true if the strings match
{
  //execute this code
}

However, for this to work the input variable must be a zero terminated array of chars. Please post your code that creates this array from user input

The strncmp() function works the same way but limits the search for a match to the number of characters specified

if (strncmp(input, "uax", 2))   //match the first 2 characters of the strings

I'm just typing in the serial monitor. That's whats creating the input. Whole sketch here. I see that I can't use it as listed for the "uax" example string as that pulls it in one character at a time.

char input;

void setup()
{
  // Start the serial port
  Serial.begin(115200);
  delay(1000);

  Serial.println("Enter command: ");

}

void loop() {

  if (Serial.available()) 
  {
    input = Serial.read();
    Serial.print("You typed: '");
    Serial.print(input);
    Serial.println("'");

    if (input == 'u') 
      {  // strings match
      Serial.println("Matched input = u");
      // do stuff triggered by u
      } 
    // else if (strcmp(input, "uax") == 0) 
    //   {  //true if the strings match
    //    Serial.println("Matched input = aux");
    //   }
    else 
      {
      Serial.println("no matches");
      // don't do anything
      }
    delay(1000);
  }
}

See Serial input basics - updated

1 Like

Very helpful, thank you for that link.

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