'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
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.
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);
}
}