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.
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");
}
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="";
}
}
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.