Recording Serial for Comparison to desired value

So what I'm intending for this program is to have it await signal from an RFID reader. That much has been done already.

However, I want the program to then compare the reading to the desired value I want it to respond to (My tag's specific ID), then perform some kind of action - we'll print a line just to test it out.

I've got most of it down, but what I can't seem to figure out how to make that comparison. My sample code is (obviously) wrong, and I'm sure I need to do store the ID in some kind of string. I have looked at many other similar cases on the web, but most responses are tailored to that specific situation, and doesn't give me a whole lot of room to understand it and apply it to my situation.

Here it goes:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(A5, 11); 
void setup()  
{
  Serial.begin(57600);
  mySerial.begin(9600);
}

void loop()
{
  if (mySerial.available())
    Serial.write(mySerial.read());
    

    if (mySerial.read() == "000095CD1C44")
    {
      Serial.println("fsdsk");
    }
  
  
}

The problem is of course comparing a pointer to a value and that's where I'm stuck.

If you want to compare 2 strings, you must use the function strcmp:

If the 2 strings are equal the function returns 0.

I went through a few resources and have modified to fit what I need. It's still wrong, but it should be generally right, I'm just not familiar with the commands.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(A5, 11); // RX, TX
char ID1[] = "7500483A7C7B";
char inData[13];
char inChar=0;
byte index = 0;
void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  Serial.println("Begin Scanning.");
  mySerial.begin(9600);
}

void loop() // run over and over
{
  if(mySerial.available() > 0)
  {
   if(index < 12) // One less than the size of the array
        {
            inChar = mySerial.read(); // Read a character
            inData[index] = inChar; // Store it
            index++; // Increment where to write next
            inData[index] = '\0'; // Null terminate the string
            
        }
  }
   
  if(strcmp(inData,ID1)==0)
  {
    Serial.println("BSDF");
  }
}

If you're only ever comparing against a constant string, then there's no need to buffer the whole incoming string, just compare it a character at a time, and reset the index as soon as you find a mismatch.

I don't see any problem. Why you don't write the value stored after read it only to see if what have stored is what it should be? For example something like this:

   if(index < 12) // One less than the size of the array
        {
            inChar = mySerial.read(); // Read a character
            inData[index] = inChar; // Store it
            index++; // Increment where to write next
            inData[index] = '\0'; // Null terminate the string
            
        }
        else {
           Serial.println(inData);
        }
  }
   
  if(strcmp(inData,ID1)==0)
  {
    Serial.println("BSDF");
  }

SO with your help Luis, as well as some of AWOL's previous posts on clearing arrays and what not, I came up with a working script.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(A5, 11); // RX, TX
char ID1[] = "7500483A7C7B";
char inChar=0;
char inData[13];
byte index = 0;

void setup()  
{
  Serial.begin(57600);
  Serial.println("Begin Scanning.");
  mySerial.begin(9600);
}

void loop() // run over and over
{
  if(mySerial.available() > 0)
  {
    if(index < 12)
        {
            inChar = mySerial.read();
            inData[index] = inChar;
            index++;
            inData[index] = '\0';
        }
        
  }
   
  if(strcmp(inData,ID1)==0)
  {
    Serial.println("BSDF");   
    memset(inData, 0, sizeof(inData)); 
    index = 0; 
  }
}

I believe that you don't need this:

memset(inData, 0, sizeof(inData));

but only this:

index = 0;

Does the serial input from your device have a delimiting character to separate the data packets, or is the data just sent on a timed basis? Below is simple code for serial capture and comparison.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

luisilva:
I believe that you don't need this:

memset(inData, 0, sizeof(inData));

but only this:

index = 0;

I believe you do need it to reset the value of inData, otherwise, the string comparison will always be true and the action will continually be performed. It does not reset on its own because it is only rewritten when a new serial comm is available.

In the actual code, when strcmp returns 0, I use this:

knob.attach(8); //attach the servo to pin 8
    knob.write(pos); //sets servo to initial 0 position
    knob.write(90); // turn 90 degrees
    delay(2500); // wait 2.5 seconds
    knob.write(pos); // return to 0 position
    memset(inData, 0, sizeof(inData)); //reset value of inData
    index = 0; //reset index

It's an RFID activated door lock. When scanned, the servo rotates a 1/4 turn to unlock, then turns it back after 2.5 secs to relock the door.

I believe you do need it to reset the value of inData,

The NULL is a stop sign. You don't need one in every position in the array. You need one, in the 0th position.

Yes, I think like PaulS. If you do this:

inData[0]='\0';

the string becomes empty.

luisilva:
Yes, I think like PaulS. If you do this:

inData[0]='\0';

the string becomes empty.

And that goes after the index++; in the recording sequence, correct?

And that goes after the index++; in the recording sequence, correct?

No. Iit goes after the index = 0; statement.

PaulS:
No. Iit goes after the index = 0; statement.

Oh okay I understand. Thank you two so much.