trouble getting my Serial read Character array to compare to another array

I’ve been working on a module to control my car over bluetooth, but I’m having trouble getting the Serial read input to.
I attached my code below, but here’s a copy of my void loop and the variable declaration.

char character[100];
char rgb[][100] = {"Horn", "Remote Start", "Headlights", "Accessory", "Trunk", "Open sunroof", "Close sunroof",
                   "Dwindow up", "Dwindow down", "Pwindow up", "Pwindow down", "Lock",
                   "Unlock", "all lights", "Fades", "Strobe", "Halos", "Underbody Lights",
                   "Interior Lights"
                  };
 if (Serial.available() > 0) {
    while (Serial.available()) {
      delay(20);
      character[t] = Serial.read();
      ++t;
      //red = Serial.parseInt();
      //green = Serial.parseInt();
      //blue = Serial.parseInt();
    }
    character[t] = '\0';
    Serial.println(character);
    for (i = 0; i < 19; i++) {
      Serial.println(rgb[i]);
      Serial.println(character);
      int result = strcmp(character, rgb[i]);
      if (result == 0) {
        BCM(i);
        break;
      } else {
        Serial.println("String's don't match");
      }
    }
  }

Can someone help me figure out whats wrong with my code? It always skips to the else line after it compares the char array.

sketch_may01a.ino (13 KB)

It goes to the else part because the 2 strings are not matched.

The way you handle the incoming data is not correct. You are saying that: "Is there a piece of data available? If yes, read the whole thing". But what you should really do is: "is there data available? If yes, read that data and store it. Have I read a complete command? If yes, compare the command to derive it's meaning"

The stick post: read from serial input will be helpful to check out.

You need to reset t=0 before you read each new string. You should have seen that Serial.println(character) is showing a previous entry as well as the current one.

if (Serial.available() > 0) {
    t=0;//add this line to reset index
    while (Serial.available()) {
      delay(20);
      character[t] = Serial.read();
      ++t;
      //red = Serial.parseInt();
      //green = Serial.parseInt();
      //blue = Serial.parseInt();
    }
    character[t] = '\0';
    Serial.println(character);
    for (i = 0; i < 19; i++) {
      Serial.println(rgb[i]);
      Serial.println(character);
      int result = strcmp(character, rgb[i]);
      if (result == 0) {
        BCM(i);
        break;
      } else {
        Serial.println("String's don't match");
      }
    }
  }

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

When the complete message has been received the variable newData will be true and that is the time to do the comparisons.

...R

Thank you all for your inputs. I followed your suggestions and got my code up my Serial input working great now.

char rgb[][100] = {"Horn", "Remote Start", "Headlights", "Accessory", "Trunk", "Open sunroof", "Close sunroof",

"Dwindow up", "Dwindow down", "Pwindow up", "Pwindow down", "Lock",
                  "Unlock", "all lights", "Fades", "Strobe", "Halos", "Underbody Lights",
                  "Interior Lights"
                  };

That is 19 strings times 100 bytes; out of curiosity, which Arduino are you using?

This would be a far more efficient approach (saves around 1600 bytes of RAM).

char *rgb[] =
{ "Horn", "Remote Start", "Headlights", "Accessory", "Trunk", "Open sunroof", "Close sunroof",
  "Dwindow up", "Dwindow down", "Pwindow up", "Pwindow down", "Lock",
  "Unlock", "all lights", "Fades", "Strobe", "Halos", "Underbody Lights",
  "Interior Lights"
};

And you can consider to move it to flash (PROGMEM) if needed.