How do i compare a string if it has been entered two time using serial.read input?

If the same string is read by serial.read then display that it has been already entered.

Please post a complete sketch showing what you are trying to do

Do yo, for instance, need to compare 2 Strings (capital S) or 2 strings (lowercase s) ?

See:

strcmp

Read the forum guidelines to see how to properly post code.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

int count = 0;
char c;
String id;

void setup() {
Serial.begin(9600);
}

void loop() {
while (Serial.available() > 0)
{
c = Serial.read();
count++;
id += c;
if (count == 12)
{
String v = "AB123456789B";

  Serial.print(id);

  if (id == v)
  {
    Serial.print(id);
    Serial.println("First Valid Tag");

    char cc = Serial.read();

    if ( v == cc) {
      Serial.println("Enter other id");
    }
  }
  else if (id == "DB123456789C") {
    Serial.println("Second valid tag");
  }
  else
  {
    Serial.println("no tag");
  }
}

}
count = 0;
id = "";
delay(500);
}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

This will never be equal:

        char cc = Serial.read();

        if ( v == cc) {
          Serial.println("Enter other id");
        }

V is type String and cc is a single char.

It looks like you want two 12-character ID's entered in order.

String id;

const char *IDList[] = {"AB123456789B", "DB123456789C"};
int IDCount = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available() > 0)
  {
    char c = Serial.read();
    id += c;
    if (id.length() == 12)
    {
      // We have a complete tag ID.
      // Check it against the current ID
      if (id == IDList[IDCount])
      {
        Serial.print("Valid match for ");
        Serial.println(IDList[IDCount]);
        IDCount++;
        id = "";

        if (IDCount == 2)
        {
          // All ID's matched.  What do you want to do?
          Serial.println("All tags matched.");
          IDCount = 0;
        }
      }
      else
      {
        // Does not match
        Serial.print(id);
        Serial.print(" is not a match for ");
        Serial.println(IDList[IDCount]);
        id = "";
      }
    }
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.