I have seen many posts on this topic but I still cant find solution.
I am using serialEvent() to read incoming data from serial port:
void serialEvent() {
if (!stringComplete) {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
Serial.println("COMPLETE");
}
}
}
}
I also have a function to compare strings and execute something when String is equal:
void setCMD(String a) {
if (a == "01*00") {
busACTIVE = 0;
// clear the string:
inputString = "";
stringComplete = false;
}
else if (a.equals("01*01")) {
busACTIVE = 1;
// clear the string:
inputString = "";
stringComplete = false;
}
I have several else if statements. I didn't bother copying them all. At the end of the else if statements I have a else statement that states that there was no match to the incoming string and prints out the string.
else {
Serial.println("Command not Found");
Serial.println(a);
// clear the string:
inputString = "";
stringComplete = false;
}
The setCMD() function is called in the main loop:
void loop() {
// Serial.println(busACTIVE);
serialEvent();
setCMD(inputString);
freqCheck();
spkrCheck();
svwkCheck();
svyrCheck();
svptCheck();
}
I test my code by sending a string from the serial monitor from Arduino IDE and what I get is in the image attached. As you can see the serial monitor shows 01*01 in the output but that did not equate to true for this else if statement:
else if (a.equals("01*01")) {
busACTIVE = 1;
// clear the string:
inputString = "";
stringComplete = false;
}
I tried using both equals() and == in the else if statement and neither works. Does anyone know what I am doing wrong?