I have researched several topics that dealt with string comparisons and have failed to get the gist of what was trying to be conveyed. I got the following code from a topic here on this forum and the string comparison just does not work and I don't see why it doesn't.
I also tried to put the following code into a code boxfor this post, but I can't find the selection for that while I'm composing this message. So I apologize for that and apologize for being an old retired man trying to learn electronics and programming when that should be left to younger more flexible minds.
For my project, I am inputting a text string through the Arduino IDE Serial Monitor. (I have an authentic Arduino Uno). I know that the text is getting to the Arduino's program because I do a Serial.print of the input and it shows up exactly as I typed it in. So the text is there in my string variable. However when I execute the 'If' statement to compare the string variable with a string contained in double quotes, it fails the test and executes the 'else' portion of the program. What am I doing wrong? Code is:
// send data only when you receive data:
if (Serial.available() > 0) {
String incoming = ""; // for incoming serial string data
// read the incoming:
incoming = Serial.readString();
// say what you got:
Serial.println(incoming);
if (strcmp(incoming,"demo") == 0) {
//demo routine
Serial.println("you started the demo routine");
}
else if (incoming == "sort") {
//sort routine
Serial.println("you started the sort routine");
}
else {
//junk
Serial.println("something else");
incoming = "";
}
Serial.flush();
}
Note that I initialize the Serial communciations with a 'Serial.begin(9600);' in the setup portion of my code.
All that comes out on the Serial Monitor' is exactly what I typed in, even if it's 'demo' or 'sort' and it always outputs 'something else', indicating that it is not getting a true evaluation in the If statement of the code.
I copied this example from another part of this forum and I didn't understand the responses to that post as to its operation or lack thereof.
Thank you for your quick response. Just so I "get it" my code snippet should look like this:
if (Serial.available() > 0) {
String incoming = ""; // for incoming serial string data
// read the incoming:
incoming = Serial.readString();
// say what you got:
Serial.println(incoming);
if (strcmp(incoming,"demo") == 0) {
//demo routine
Serial.println("you started the demo routine");
}
else if (strcmp(incoming,"sort" == 0) {
//sort routine
Serial.println("you started the sort routine");
}
else {
//junk
Serial.println("something else");
incoming = "";
}
Serial.flush();
}
Is this correct?
Because I get this error on compiling... "error: cannot convert 'String' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'"
Actually, believe it or not, you helped. I see that what is entered in the Serial Monitor is a 5 long string even though I entered a 4 long string. The last character must be the return character when I hit enter in the Serial monitor. So if I can strip off that last character before doing the 'If' comparison as in your stupid Frank and stupid Bob example, then the comparison of what is typed in and the 'If' statement will be true.
Now, all I need is to know how to strip off that last character of the inputted string and I'm sure there is a command for doing that hidden somewhere in the documentation.
So if I can strip off that last character before doing the 'If' comparison as in your stupid Frank and stupid Bob example, then the comparison of what is typed in and the 'If' statement will be true.
Have you looked at the String class documentation? Did you see the trim() method? Can you guess, or find out, what it does?
if (strcmp(incoming.c_str(),"demo") == 0) {
//demo routine
Serial.println("you started the demo routine");
}
else if (strcmp(incoming.c_str(),"sort" == 0) {
Or this:
Serial.println(incoming);
if (incoming == "demo") {
//demo routine
Serial.println("you started the demo routine");
}
else if (incoming == "sort") {
Or this (preferred method!):
// send data only when you receive data:
if (Serial.available() > 0) {
char incoming[MAXLEN] = ""; // for incoming serial string data
// read the incoming:
Serial.readBytesUntil('\n', incoming, MAXLEN);
// say what you got:
Serial.println(incoming);
I'm experimenting with the functions you guys suggested and getting interesting results. I think I'm on the path of solving the problem now. I appreciate you pointing me in the right direction. I don't know how many people in the forum just get their answers and leave without thanking you for your effort, but I'd like to thank you for what you have suggested.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
When using Cstrings you must use strcmp() to compare values rather than ==
My Serial String Read from the Serial Monitor is now working thanks to a Youtube video which explained how to read in a String and compare it with a Boolean operator to a defined String in quotes...
// This Serial String and Compare works!
// From Youtube video: https://www.youtube.com/watch?v=UhjGqib11sA
// Note that the baud rate is changed to 9600 to match Serial Monitor baud rate
//
String textInput;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available() == 0);
textInput = Serial.readStringUntil('\n');
Serial.print("Text entered: ");
Serial.println(textInput);
if(textInput == "test")
{
Serial.println(textInput + ", Correct!");
}
}
The key here is the Serial.readStringUntil command where it reads the whole string that is being inputted on the Serial Monitor and waits until you hit Enter which generates the "\n" (which is a linefeed).
Note that, for this sketch to work, the Serial Monitor must be set for "linefeed" in the lower right of the Serial Monitor immediately to the left of the baud rate. Also, set the baud rate to match the baud rate that you are using in the Serial.begin command (or vice versa, they must be the same. Otherwise, you get garbage).