Pages: [1]   Go Down
Author Topic: Suggestion on adding a second rfid card  (Read 638 times)
0 Members and 1 Guest are viewing this topic.
Missouri
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey all, I have a UNO and Parallax RFID reader I am using to unlock a car door, the code work great but I am wanting to add a second card to the code to unlock but not to sure on how to do it. Looking for some suggestions or example on how to . Current code I am using below.
Thanks for any help.

Code:
char tag[12] = {'0', '7', '0', '0', '8', '0', 'B', 0', '6', 'F'};  //rfid code
char code[12];
int bytesread = 0;
int doorPin = 13; // Connect red LED to pin 13
int rfidPin = 2; // RFID enable pin connected to digital pin 2
int val=0;
long lastUpdate=0;
void setup() {
Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(rfidPin,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
pinMode(doorPin,OUTPUT); // Set doorPin to output
digitalWrite(rfidPin, LOW); // Activate the RFID reader
}

 void loop()
 {  char tagString[13];
  if(Serial.available() > 0) {          // if data available from reader
    if((val = Serial.read()) == 10) {   // check for header
      bytesread = 0;
      while(bytesread<10) {              // read 10 digit code
        if( Serial.available() > 0) {
          val = Serial.read();
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
            break;                       // stop reading
          }
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit 
        }
      }
      if(bytesread >= 10) {              // if 10 digit read is complete     
       if(strcmp(code, tag) == 0) {
         Serial.print("ACCESS GRANTED  : ");
         Serial.println(code);
         digitalWrite(rfidPin, HIGH); // diable rfid reader
         digitalWrite(doorPin,HIGH); // good read unlock door
         delay(800);                // wait a second
         digitalWrite(doorPin,LOW); // turn off door lock relay
         delay(600);                 //wait about half a second
         digitalWrite(doorPin,HIGH); // unlock door
         delay(800);                // wait a second
         digitalWrite(doorPin,LOW);  // turn off door lock relay
         delay(2000);                 // wait 2 seconds
         digitalWrite(rfidPin,LOW);   // enable rfid reader
         Serial.flush();        // flush all rfid reads, stops multiple reads from one rfid read
       }
       
        else {
         Serial.print("ACCESS DENIED   : ");
         Serial.println(code);
         digitalWrite(rfidPin, HIGH); // disable rfid reader     
         delay(3000);                 // wait 3 seconds
         digitalWrite(rfidPin,LOW);   // enable rfid reader
        Serial.flush();             // flush all rfid reads, stops multiple reads from one rfid read
       }
      }
      bytesread = 0;
           delay(500);                       // wait for a 1/2 second
    }
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The tag variable defines the valid tag ID. The strcmp function compares the scanned tag ID to the valid tag ID, and does something if they match.

Add another valid tag variable with the 2nd valid tag ID, and another if(strcmp...) block to test the scanned tag to the 2nd valid tag.
Logged

Missouri
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks PaulS for the reply, I Forgot to include this code to. The code works for both cards but has bug the first card "char tag" Will be granted and unlock the door and after the door unlocks and in the serial monitor wil then say access denied, it does not effect the unlocking . The second card works fine no error or nothing.  I guess it should be fine to just go ahead and use the code,  I guess OCD is saying don't haha.


Code:
char tag[12] = {'0', '7', '0', '0', '8', '0', 'B', '0', '6', 'F'};  //rfid code
char tag2[12] = {'0', '5', '6', '0','0', '1', 'M', '0', '7', '0'};
char code[12];
int bytesread = 0;
int doorPin = 13; // Connect red LED to pin 13
int rfidPin = 2; // RFID enable pin connected to digital pin 2
int val=0;
long lastUpdate=0;

void setup() {
Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(rfidPin,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
pinMode(doorPin,OUTPUT); // Set doorPin to output
digitalWrite(rfidPin, LOW); // Activate the RFID reader
}

 void loop()
 {  char tagString[13];
  if(Serial.available() > 0) {          // if data available from reader
    if((val = Serial.read()) == 10) {   // check for header
      bytesread = 0;
      while(bytesread<10) {              // read 10 digit code
        if( Serial.available() > 0) {
          val = Serial.read();
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
            break;                       // stop reading
          }
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit 
        }
      }
      if(bytesread >= 10) {              // if 10 digit read is complete     
       if(strcmp(code, tag) == 0)   {
         Serial.print("ACCESS GRANTED  : ");
         Serial.println(code);
         digitalWrite(rfidPin, HIGH); // diable rfid reader
         digitalWrite(doorPin,HIGH); // good read unlock door
         delay(800);                // wait a second
         digitalWrite(doorPin,LOW); // turn off door lock relay
         delay(600);                 //wait about half a second
         digitalWrite(doorPin,HIGH); // unlock door
         delay(800);                // wait a second
         digitalWrite(doorPin,LOW);  // turn off door lock relay
         delay(2000);                 // wait 2 seconds
         digitalWrite(rfidPin,LOW);   // enable rfid reader
         Serial.flush();        // flush all rfid reads, stops multiple reads from one rfid read
       }

      if(bytesread >= 10) {              // if 10 digit read is complete     
       if(strcmp(code, tag2) == 0)   {
         Serial.print("ACCESS GRANTED  : ");
         Serial.println(code);
         digitalWrite(rfidPin, HIGH); // diable rfid reader
         digitalWrite(doorPin,HIGH); // good read unlock door
         delay(800);                // wait a second
         digitalWrite(doorPin,LOW); // turn off door lock relay
         delay(600);                 //wait about half a second
         digitalWrite(doorPin,HIGH); // unlock door
         delay(800);                // wait a second
         digitalWrite(doorPin,LOW);  // turn off door lock relay
         delay(2000);                 // wait 2 seconds
         digitalWrite(rfidPin,LOW);   // enable rfid reader
         Serial.flush();        // flush all rfid reads, stops multiple reads from one rfid read
       }

        else {
         Serial.print("ACCESS DENIED   : ");
         Serial.println(code);
         digitalWrite(rfidPin, HIGH); // disable rfid reader     
         delay(3000);                 // wait 3 seconds
         digitalWrite(rfidPin,LOW);   // enable rfid reader
        Serial.flush();             // flush all rfid reads, stops multiple reads from one rfid read
       }
      }
      bytesread = 0;
           delay(500);                       // wait for a 1/2 second
    }
   }
  }
 }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Put each { and } on a separate line. Then use Tools + Auto Format. Then, look at the code where you test the tags. You have poorly nested if statements.

You want something like this:
if(bytesRead >= 10)
{
   if(strcmp(first tag...) == 0)
   {
   }
   else if(strcmp(second tag...) == 0)
   {
   }
   else
   {
      // Access denied...
   }
}
Logged

Missouri
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you PaulS, I knew it would be something simple and I just couldn't see it. Thanks for the format tip.
Logged

Pages: [1]   Go Up
Jump to: