RFID check

I would want to write a program for adding and reading tags..

The only code that works for me is this:

#include <SoftwareSerial.h>
SoftwareSerial rfid(10, 11); //Rx, Tx

void setup()
{
Serial.begin(9600);
Serial.println("Seriale:\n");

rfid.begin(9600);
rfid.write(0x02);
}
void loop(){
  if (rfid.available())
    Serial.print(rfid.read(),HEX); 
    
  if (Serial.available())
    rfid.write(Serial.read());

}

Changing rfid.write(0x02); to 0x03 or 0x04 i can add or delete id, but i can't do nothing, someone can help me?
My idea is tring to make a door lock with rfid

Here is the manual of my rfid reader/writer: 42125.rar - Google Drive

but i can't do nothing, someone can help me?

You need our help to do nothing?

lol sorry for my bad english..

I need help!

I need help!

You need help to do what?

google.docs sucks. It NEVER lets me see shit. Attach the pdf to your reply.

I want to check from arduino if the id is correct and match my id list (that i have to write in arduino)
So i can put an if, and inside the if put other code

13.56MHZ_RFID_Manual.pdf (1.41 MB)

I also tried to use the code below (and others code) but i got errors:

#include "SoftwareSerial.h"


#define txPin 11
#define rxPin 10

SoftwareSerial RFID(rxPin, txPin);

void setup()
{
		Serial.begin(9600);
		RFID.begin(9600);
		pinMode(txPin, OUTPUT); //pin 22
		pinMode(rxPin, INPUT); //pin 24

		Serial.println("Seriport Menu");
		Serial.println("a - Test Menu");
		Serial.println("b - Read card"); 
	 
                Serial.println("");
                delay(2000);   // RFID wait time:
}
 void loop() {
    
   if (Serial.available() > 0) {
      char incomingByte = Serial.read();    
      switch (incomingByte) {
      case 'a':            // if user enters 'a' show test print
      Serial.print("Test menu....");
      break;    
      case 'b':            // if user enters 'b' start card reading
      Serial.print("Card Reading Started");
      RFID.write(0xAB);
      RFID.write(0x02);
      RFID.write(0x02);
      if (RFID.available()>0)
        {
        Serial.print("Card number is:"); 
        Serial.print(RFID.read(), HEX);
        Serial.println(" ");
        }
      break;   
      }
   }
 }

ERRORS

avrdude: stk500v2_command(): unknown status 0xc8
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.

avrdude: stk500v2_command(): unknown status 0x01
avrdude: stk500v2_disable(): failed to leave programming mode

also tried to use the code below (and others code) but i got errors:

You seem to have a fundamental misunderstanding of serial communications.

      if (RFID.available()>0)
        {
        Serial.print("Card number is:"); 
        Serial.print(RFID.read(), HEX);
        Serial.println(" ");
        }

It is likely that the RFID will return several characters for each card scanned, not just one. You need to collect data in an array until the end of packet marker arrives. What is that end of packet marker? Beats me. You're the one with the no-link-provided RFID reader.

The first code that you posted should allow you to determine whether there is a common end-of-packet marker or a fixed tag size. Either way, you need to collect the characters that make up the packet into an array, NULL terminating it after each character addition, and then used strcmp() to see if the tag read matches a (or one of a list of) predefined tag strings.

As for the errors uploading, it appears that the RFID reader interferes with sketch loading, though it shouldn't.

Post a link to the reader/writer.

The manual of my rfid is on my previous post, however, i tried with arduino uno and i don't get the avrdude errors :astonished:

I need help

I make it works with the code below, but it's always checking id tags.. I want that the reader stay in stand-by if there is no card to scan, what i have to do?

#include "SoftwareSerial.h"

#define txPin 11
#define rxPin 10

SoftwareSerial RFID(rxPin, txPin);

int val=0;
byte data[5];
byte tag1[4] = {0x5E,0xE8,0xCB,0x61};
boolean tag1_card = true;

void setup(){
  
  pinMode(txPin, OUTPUT); 
  pinMode(rxPin, INPUT);
  pinMode(6,OUTPUT);
  
  Serial.begin(9600);
  RFID.begin(9600);
  
  Serial.println("");
 
  RFID.write(0xAB);
  RFID.write(0x02);
  RFID.write(0x02); 
 
  delay(100);   
  while(Serial.available()>0){
     RFID.read();
  }
  Serial.println();
  Serial.println("Waiting for Card...");
}



void loop() {
  val = RFID.read();
  Serial.println();
  while (val != 0xAB){  
    val = RFID.read();
    delay(10);
      
  }
   
  RFID.read();
  RFID.read();
  data[0] = RFID.read();    
  data[1] = RFID.read();   
  data[2] = RFID.read();    
  data[3] = RFID.read();    

  for (int i=0; i<4; i++){
    if (data[i] < 16) Serial.print("0");
    Serial.print(data[i], HEX);
    if (data[i] != tag1[i]) {
      tag1_card = false;
      digitalWrite(6,LOW);
    }
     
  }
  
  if (tag1_card) digitalWrite(6,HIGH);
  
  
  RFID.write(0xAB);
  RFID.write(0x02);
  RFID.write(0x02); 
 
 }

I make it works with the code below, but it's always checking id tags.. I want that the reader stay in stand-by if there is no card to scan, what i have to do?

You need to make up your mind what you want.

  RFID.write(0xAB);
  RFID.write(0x02);
  RFID.write(0x02);

You are telling the reader to send data whenever a card is scanned.

  val = RFID.read();
  Serial.println();
  while (val != 0xAB){  
    val = RFID.read();
    delay(10);
      
  }

Then, you go into an infinite loop waiting for the start of tag data.

As soon as the start of tag marker arrives:

  RFID.read();
  RFID.read();
  data[0] = RFID.read();    
  data[1] = RFID.read();   
  data[2] = RFID.read();    
  data[3] = RFID.read();

You read 6 bytes of data that have not yet arrived. How is that useful?

The RFID.available() function bears looking into. It will tell you how much data is available to read. Since you only seem to be concerned about the next 6 bytes, after the start marker arrives, you should have an if(RFID.available() > 0) block that then reads a character, and checks if it is a start marker. If it is, there should be a

while(RFID.available() < 6) { // Do nothing }

statement that blocks, waiting for the data of interest to arrive.

Ok, i placed

RFID.write(0xAB);
  RFID.write(0x02);
  RFID.write(0x02);

on the top of loop

but if i write if(RFID.available()>0)

void loop() {
  
  RFID.write(0xAB);
  RFID.write(0x02);
  RFID.write(0x02);
  
  if(RFID.available()>0){
    
  val = RFID.read();
  Serial.println();
  while (val != 0xAB){  
    val = RFID.read();
    delay(10);
      
  }
   
  RFID.read();
  RFID.read();
  data[0] = RFID.read();    
  data[1] = RFID.read();   
  data[2] = RFID.read();    
  data[3] = RFID.read();    

  for (int i=0; i<4; i++){
    if (data[i] < 16) Serial.print("0");
    Serial.print(data[i], HEX);
    if (data[i] != tag1[i]) {
      tag1_card = false;
      digitalWrite(6,LOW);
    }
     
  }
  
  if (tag1_card) digitalWrite(6,HIGH);
  }
   
 }

it never read code.. In serial monitor i see only this (one time only):

Waiting for Card...

FFFFFFFF

Ok, i placed...on the top of loop

If you think the device is so stupid that it needs to be told over and over what to do, perhaps you should just send it back, and get a different reader.

If not, put that code back in setup where it belongs.

  if(RFID.available()>0){
    
  val = RFID.read();
  Serial.println();
  while (val != 0xAB){  
    val = RFID.read();
    delay(10);
      
  }

So, now the spin waiting for a start marker will only happen if there is something to read. Great.

But, if there is nothing to read,

  RFID.read();
  RFID.read();
  data[0] = RFID.read();    
  data[1] = RFID.read();   
  data[2] = RFID.read();    
  data[3] = RFID.read();

read 6 bytes anyway.

Go back and re-read my previous reply.

You should only do something in loop IF there if RFID says that there is data to read.

it still don't sending nothing..
I put

 RFID.write(0xAB);
   RFID.write(0x02);
   RFID.write(0x02);

in void setup() and i added while(RFID.available() < 6) { } just after if(RFID.available()>0){ so now it wait until it receive at least 6 bytes, right?

I think is correct now, right?

I attach a screenshot of reading instructions from the reader manual

I think is correct now, right?

Beats me. My crystal ball is cracked.

I wrote a program for a door lock that would add tags using a master tag. It stores the tags in a text file on an SD card. I'll post the code and you can rip whatever you want from it. It works perfectly. You can also wipe the database by swiping the master card twice.

#include <SoftwareSerial.h>

#include <SD.h>

SoftwareSerial mySerial(2, 4);

int resetpin = 3, green = 7, red = 8, blue = 9, CS_pin = 10, relay = 6;

void setup() {

  Serial.begin(9600);  //Initialize main serial connection.
  mySerial.begin(9600);  //Initialize reader serial connection.
  pinMode(resetpin, OUTPUT);  //Define reset pin as an output.
  pinMode(CS_pin, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(resetpin, HIGH);  //Pull the reset pin high.
  Serial.println("Initializing SD Card");

  if (!SD.begin(CS_pin)) {
    Serial.println("Card Failed. Quitting");
    return;
  }

  Serial.println("SD Card Ready");
  Serial.println("Waiting for Tag");
}

void loop() {

  char readByte, tempByte, addByte[13], tagString[13], mastertag[13] = {
    '4', 'E', '0', '0', '0', '4', '3', 'D', '3', '8', '4', 'F'    };
  int index = 0, filesize;
  boolean reading = false, cardreading = false, cardready = false;

  while (mySerial.available() > 0) {

    readByte = mySerial.read();

    if(readByte == 2) reading = true;
    if(readByte == 3) reading = false;

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      tagString[index] = readByte;
      index ++;
    }
  }

  printtag(tagString);

  if (strlen(tagString) != 0) {
    if (database(tagString) == true) {
      Serial.println("Match Found");
      digitalWrite(relay, HIGH);
      digitalWrite(green, HIGH);
      delay(3000);
      digitalWrite(relay, LOW);
      digitalWrite(green, LOW);
    }
    else {
      
      digitalWrite(red, HIGH);
      Serial.println("No Match Found");
      delay(1000);
      digitalWrite(red, LOW);
    }

  }

  if (compare(mastertag, tagString)) {
    Serial.println("Master Tag Found, Entering Admin Mode");
    resetreader();
    index = 0;
    digitalWrite(blue, HIGH);
    delay(500);
    digitalWrite(blue, LOW);
    delay(500);
    delay(2000);

    while (cardready == false) {
      digitalWrite(blue, HIGH);
      delay(100);
      digitalWrite(blue, LOW);
      delay(100);
      while (mySerial.available() > 0) {
        tempByte = mySerial.read();

        if (tempByte == 2) cardreading = true;
        if (tempByte == 3) cardreading = false;

        if (cardreading && tempByte != 2 && tempByte != 10 && tempByte != 13) {
          addByte[index] = tempByte;
          index ++;
        }
      }
      if (index == 12) {
        Serial.println("Done Reading");
        cardready = true;
        if (database(addByte) == false) {
          File logread = SD.open("log.txt", FILE_WRITE);
          filesize = logread.size();
          logread.seek(filesize);
          logread.print(addByte[0]);
          logread.print(addByte[1]);
          logread.print(addByte[2]);
          logread.print(addByte[3]);
          logread.print(addByte[4]);
          logread.print(addByte[5]);
          logread.print(addByte[6]);
          logread.print(addByte[7]);
          logread.print(addByte[8]);
          logread.print(addByte[9]);
          logread.print(addByte[10]);
          logread.print(addByte[11]);
          logread.close();
        }
        if (compare(addByte, mastertag)) {
          SD.remove("log.txt");
          Serial.println("Database Cleared");
          digitalWrite(red, HIGH);
          delay(100);
          digitalWrite(red, LOW);
          delay(100);
          digitalWrite(red, HIGH);
          delay(100);
          digitalWrite(red, LOW);
          delay(100);
          digitalWrite(red, HIGH);
          delay(100);
          digitalWrite(red, LOW);
          delay(3000);
        }
      }
    }
  }
  cleartag(tagString);
  resetreader();
  delay(200);
}

void resetreader() {
  digitalWrite(resetpin, LOW);
  digitalWrite(resetpin, HIGH);
  delay(150);
}

boolean compare(char one[], char two[]) {

  for (int i = 0; i < 12; i++) {
    if (one[i] != two[i]) {
      return false;
    }
  }
  return true;
}

void cleartag(char one[]) {

  for (int i = 0; i < 13; i++) {
    one[i] = 0;
  }
}

void printtag(char one[]) {

  int printer = 0;

  if (!strlen(one) == 0) {

    for (int i = 0; i < 12; i++) {
      Serial.print(one[i]);
      printer++;
    }
    if (printer == 12) {
      Serial.println();
    }
  }
}

boolean database(char one[]) {

  char sdarray[13];
  boolean match = false;
  File logread = SD.open("log.txt", FILE_WRITE);

  if (logread) {
    logread.seek(0);
    while (logread.available() && match == false) {
      for (int i = 0; i < 12; i++) {
        sdarray[i] = logread.read();
      }

      if (compare(one, sdarray) && match == false) {
        match = true;
        logread.close();
        return true;
      }
    }

    if (match == false && !logread.available()) {
      logread.close();
      return false;
    }
  }
}

PaulS:

I think is correct now, right?

Beats me. My crystal ball is cracked.

I hope you like it now :grin:
Now it works..

#include "SoftwareSerial.h"

#define txPin 11
#define rxPin 10

SoftwareSerial RFID(rxPin, txPin);

int val=0;
byte data[5];
byte tag1[4] = {0x5E,0xE8,0xCB,0x61};
boolean tag1_card = false;

void setup(){
  
  pinMode(txPin, OUTPUT); 
  pinMode(rxPin, INPUT);
  pinMode(6,OUTPUT);
  
  RFID.begin(28800);
  
  RFID.write(0x02);
 
  delay(100);   

}



void loop() {
  
  if(RFID.available()>0){
    
  data[0] = RFID.read(); 
  data[1] = RFID.read(); 
  data[2] = RFID.read();
  data[3] = RFID.read(); 
  
  for (int i=0; i<4; i++){
    if ((data[0] == tag1[0])&&(data[1] == tag1[1])&&(data[2] == tag1[2])&&(data[3] == tag1[3])) {
      tag1_card = true;
    }
     
  }
  
  if (tag1_card==true) {
    digitalWrite(6,HIGH);
    delay(500);
    digitalWrite(6,LOW);
    tag1_card=false;
  }
 
  }
 }

I hope you like it now

Well, I don't.

  if(RFID.available()>0){
    
  data[0] = RFID.read(); 
  data[1] = RFID.read(); 
  data[2] = RFID.read();
  data[3] = RFID.read();

If there is at least one byte to read, read all 4 of them. Why can't you see that this is wrong?

    if ((data[0] == tag1[0])&&(data[1] == tag1[1])&&(data[2] == tag1[2])&&(data[3] == tag1[3])) {

Googling memcmp() is in your future.

  if (tag1_card==true) {

Don't you suppose that true == true evaluates to true, just like true does?

if(tag1_card)
{ // Down here where it belongs!

Better now?

void loop() {
  
  if(RFID.available()>0){
    while(RFID.available() < 4) { }
    
    for(int x=0; x<4; x++)
      data[x]=RFID.read();
     
    for (int i=0; i<4; i++)
      if(memcmp(data,tag1,4)==0) tag1_card = true;
        
    if (tag1_card) {
      digitalWrite(6,HIGH);
      delay(500);
      digitalWrite(6,LOW);
      tag1_card=false;
    }
  }
}
 for (int i=0; i<4; i++)
      if(memcmp(data,tag1,4)==0) tag1_card = true;

You only need to do the memcmp() once.

     if(memcmp(data,tag1,4)==0) tag1_card = true;
        
    if (tag1_card) {
      digitalWrite(6,HIGH);
      delay(500);
      digitalWrite(6,LOW);
      tag1_card=false;
    }

You don't need to set a flag, then immediately afterwards check the flag.

Better now?

Yes, but there is still room for improvement. You look to see if there is at least one byte to read. If so, you wait for the rest of the data to arrive. Why not skip the processing until there are 4 bytes to read?

if(RFID.available() >= 4)
{
   // No waiting required. Just read the 4 bytes