Pages: [1]   Go Down
Author Topic: RFID PARALLAX WITH LOCK PROBLEM  (Read 534 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hI !
I'm trying to make my project work but i'm a noob with arduino. I search on the forum the solution but i can't find my solution
I used the code described here http://www.gumbolabs.org/2009/10/17/parallax-rfid-reader-arduino/
NO problem with that but now i want to test if the tag match with the autorized tag.. i use strcmp but when i'm trying i doesn't work
Code:
/**
 * author Benjamin Eckel
 *
 * Adaptation pour integration validation badge et ouverture serrure
 */
#define RFID_ENABLE 2   //to RFID ENABLE
#define CODE_LEN 10      //Max length of RFID tag
#define VALIDATE_TAG 1  //should we validate tag?
#define VALIDATE_LENGTH  200 //maximum reads b/w tag read and validate
#define ITERATION_LENGTH 2000 //time, in ms, given to the user to move hand away
#define START_BYTE 0x0A
#define STOP_BYTE 0x0D
#define GACHE 13 // Commande de la gâche électrique sur pin13 => led
#define unlockSeconds 2 // Durée de la commande de la gâche
 
char tag[CODE_LEN]; 

char* BadgesAutorises[] = { // Définition de la liste des tags autorisés
"0000C153EC", // tag 1
"0000CAAAAA", // tag 2
"0001111A1A", // tag 3
};


char* TagNom[] = {
"Eleve 1", // tag 1
"Eleve 2", // tag 2
"Eleve 3", // tag 3
};

int NombreDeBadges=sizeof(BadgesAutorises)/sizeof(BadgesAutorises[0]);

void setup() {
  Serial.begin(2400); 
  pinMode(RFID_ENABLE,OUTPUT); 
  Serial.print(NombreDeBadges);
}
 
void loop() {
  char tagValue[11]; // declaration pour stockage des infos du badge
 
  enableRFID();
  getRFIDTag();
  if(isCodeValid()) {
disableRFID();
comparaison(); // Comparaison du tag lu et des tags references
    sendCode();
    delay(ITERATION_LENGTH);
  } else {
    disableRFID();
    Serial.println("Got some noise"); 
  }
  Serial.flush();
  clearCode();
}
 
/**
 * Clears out the memory space for the tag to 0s.
 */
void clearCode() {
  for(int i=0; i<CODE_LEN; i++) {
    tag[i] = 0;
  }
}
 
/**
 * Sends the tag to the computer.
 */
void sendCode() {
    Serial.print("TAG:"); 
    //Serial.println(tag);
    for(int i=0; i<CODE_LEN; i++) {
      Serial.print(tag[i]);
    }
}
 
/**************************************************************/
/********************   RFID Functions  ***********************/
/**************************************************************/
 
void enableRFID() {
   digitalWrite(RFID_ENABLE, LOW);   
}
 
void disableRFID() {
   digitalWrite(RFID_ENABLE, HIGH); 
}
 
/**
 * Blocking function, waits for and gets the RFID tag.
 */
void getRFIDTag() {
  byte next_byte;
  while(Serial.available() <= 0) {}
  if((next_byte = Serial.read()) == START_BYTE) {     
    byte bytesread = 0;
    while(bytesread < CODE_LEN) {
      if(Serial.available() > 0) { //wait for the next byte
          if((next_byte = Serial.read()) == STOP_BYTE) break;       
          tag[bytesread++] = next_byte;                   
      }
    }               
  }   
}
 
/**
 * Waits for the next incoming tag to see if it matches
 * the current tag.
 */
boolean isCodeValid() {
  byte next_byte;
  int count = 0;
  while (Serial.available() < 2) {  //there is already a STOP_BYTE in buffer
    delay(1); //probably not a very pure millisecond
    if(count++ > VALIDATE_LENGTH) return false;
  }
  Serial.read(); //throw away extra STOP_BYTE
  if ((next_byte = Serial.read()) == START_BYTE) { 
    byte bytes_read = 0;
    while (bytes_read < CODE_LEN) {
      if (Serial.available() > 0) { //wait for the next byte     
          if ((next_byte = Serial.read()) == STOP_BYTE) break;
          if (tag[bytes_read++] != next_byte) return false;                     
      }
    }               
  }
  return true;   
}

void comparaison()
{int tagId = findTag(tag);
  if (tagId>0)
    {Serial.print("Acces autorise");
    Serial.print(tagId);
    Serial.print(": Acces pour ");
    Serial.println(TagNom[tagId-1]);
    unlock();
    }
  else
  {
    Serial.println("Badge non autorise");
  }
}


void unlock()
{
digitalWrite(GACHE, HIGH);
delay(unlockSeconds*1000);
digitalWrite(GACHE, LOW);
}


int findTag(char tag[10])
{
for (int thisCard = 0; thisCard < NombreDeBadges; thisCard++)
{ if(strcmp(tag, BadgesAutorises[thisCard]) ==0)
{return(thisCard+1);
}
}
return(0);
}


Can someone help me and tell me what's wrong with this code ? I searched but didn't find so your help is welcomed. Sorry for my english.
Thanks for your help.

I use arduino 1.0 and i work on seven 64bits with an arduino uno.
Logged

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

Code:
      if(Serial.available() > 0) { //wait for the next byte
An if statement does NOT wait. If there is something to do, it does it. The while statement above it does cause waiting for all the data to be read. Put the comment where it belongs, or get rid of it.

Code:
  Serial.flush();
Get rid of this, unless you can explain what it does and why you put it here.

You really need to connect your RFID reader to other pins, and use SoftwareSerial to read it, so that you can use Serial for debugging.
Logged

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

Thank you Paul for your help. I used the sketch i mentionned in the link on my first post to read the tag on the parallax rfid reader. I take it like it was , i think . I'm a newbie, and i didn't know what to do with the serial.flush() etc...
But, i think my problem is for the comparaison between the tag which is scanned and the "good ones" .. I read a lot of posts but i'm not able to understand what i have to do to make my sketch correct.. Is my strcmp command correct ? Are the types of my variables correct ? Please everyone, help me getting this sketch correct. I really sorry if the solution is already in other posts but i didn't find it. THanks for your help.
Logged

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

Quote
Is my strcmp command correct ?
The code to call the function is correct. The arguments to the function are not. The function expects two string arguments. The 2nd argument, BadgesAutorises[n] is a string. The 1st argument, tag, is not a string.

A string is a NULL terminated array of characters.
The tag variable is an array of characters. It is NOT NULL terminated, therefore it is not a string. Therefore, it is not valid as an argument to strcmp.

Have you made any changes to the hardware or software? How do you know what values should be in the BadgesAutorises array? How do you know what value the scanned tag returned?
Logged

Pages: [1]   Go Up
Jump to: