I am trying to read a serial data into a string with is capable of being compared against another string. I am using if (inputString.equals(“test”)) to test the boolean value but it is always returning as false as THEY ARE EQUAL is never displayed when test is typed in the serial monitor (which does echo back what ever I sent to the arduino). Any ideas? Is concating the string this way adding extra, non-displayed, bytes to the string?
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
if (inputString.equals("test")) {
Serial.print("THEY ARE EQUAL");
}
Serial.print(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
stringComplete = true;
}
}
Thanks!
You need to allow enough time for the serial data to be read. Add a delay