ID-20 RFID reader for Arduino YUN code

Hello friends, I have a problem with the code I’m working, it is a simple card reader rfid 125khz, specifically the ID-20 reader, when I try to read the code for my token, I do not throw anything on the screen serial. If someone could help I would appreciate it a lot.

Here’s the code:

#include <SoftwareSerial.h>
#include <Bridge.h>

SoftwareSerial rfid(2,3); // RX,TX (el TX no se usa)

void setup() {
  Serial.begin(9600);
  rfid.begin(9600);
  Bridge.begin();
  while (!Serial);
  
  Serial.print("Procesos iniciados.");

}

void loop() 
{
  
    while(rfid.available()) {
      
      byte i = 0;
      byte val = 0;
      byte code[6];
      byte checksum = 0;
      byte bytesread = 0;
      byte tempbyte = 0;
      
      Serial.print("Listo para leer");
      
      if((val = rfid.read()) == 2) {                  // check for header 
        bytesread = 0; 
        while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
          if( rfid.available() > 0) { 
            val = rfid.read();
            if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading 
              break;                                    // stop reading
            }
  
            // Do Ascii/Hex conversion:
            if ((val >= '0') && (val <= '9')) {
              val = val - '0';
            } else if ((val >= 'A') && (val <= 'F')) {
              val = 10 + val - 'A';
            }
  
            // Every two hex-digits, add byte to code:
            if (bytesread & 1 == 1) {
              // make some space for this hex-digit by
              // shifting the previous hex-digit with 4 bits to the left:
              code[bytesread >> 1] = (val | (tempbyte << 4));
  
              if (bytesread >> 1 != 5) {                // If we're at the checksum byte,
                checksum ^= code[bytesread >> 1];       // Calculate the checksum... (XOR)
              };
            } else {
              tempbyte = val;                           // Store the first hex digit first...
            };
  
            bytesread++;                                // ready to read next digit
          } 
        } 
  
        // Output to Serial:
  
        if (bytesread == 12) {                          // if 12 digit read is complete
          Serial.print("5-byte code: ");
          for (i=0; i<5; i++) {
            if (code[i] < 16) Serial.print("0");
            Serial.print(code[i], HEX);
            Serial.print(" ");
          }
          Serial.println();
  
          Serial.print("Checksum: ");
          Serial.print(code[5], HEX);
          Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
          Serial.println();
        }
  
        bytesread = 0;
      }
      delay(1000);
    }
  }

All your variables are being reset on every digit read.

Upon changing the variables and declare void in the setup, I can not print the results in the Serial console.

Upon changing the variables and declare void in the setup, I can not print the results in the Serial console.

I changed my code. I'm not going to tell you how. It still doesn't work. Why not?

Who knows?

Hi PaulS, yes, I’ve changed the code but still have no result, I’ll let the code that I have now after the change of variables:

#include <SoftwareSerial.h>
#include <Bridge.h>

SoftwareSerial rfid(2,3); // RX,TX (el TX no se usa)
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;
  
  
void setup() {
  Serial.begin(9600);
  rfid.begin(9600);
  Bridge.begin();
  while (!Serial);
  
  Serial.print("Procesos iniciados.");

}

void loop() 
{
    while(rfid.available()) {
      
      Serial.print("Listo para leer");
      
      if((val = rfid.read()) == 2) {                  // check for header 
        bytesread = 0; 
        while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
          if( rfid.available() > 0) { 
            val = rfid.read();
            if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading 
              break;                                    // stop reading
            }
  
            // Do Ascii/Hex conversion:
            if ((val >= '0') && (val <= '9')) {
              val = val - '0';
            } else if ((val >= 'A') && (val <= 'F')) {
              val = 10 + val - 'A';
            }
  
            // Every two hex-digits, add byte to code:
            if (bytesread & 1 == 1) {
              // make some space for this hex-digit by
              // shifting the previous hex-digit with 4 bits to the left:
              code[bytesread >> 1] = (val | (tempbyte << 4));
  
              if (bytesread >> 1 != 5) {                // If we're at the checksum byte,
                checksum ^= code[bytesread >> 1];       // Calculate the checksum... (XOR)
              };
            } else {
              tempbyte = val;                           // Store the first hex digit first...
            };
  
            bytesread++;                                // ready to read next digit
          } 
        } 
  
        // Output to Serial:
  
        if (bytesread == 12) {                          // if 12 digit read is complete
          Serial.print("5-byte code: ");
          for (i=0; i<5; i++) {
            if (code[i] < 16) Serial.print("0");
            Serial.print(code[i], HEX);
            Serial.print(" ");
          }
          Serial.println();
  
          Serial.print("Checksum: ");
          Serial.print(code[5], HEX);
          Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
          Serial.println();
        }
  
        bytesread = 0;
      }
      delay(1000);
    }
  }

So exactly what do you see in the serial monitor window?

The only i can see in the monitor, it's the message that i put in the void setup when were all ready to use. I mean this part:

void setup() {
  Serial.begin(9600);
  rfid.begin(9600);
  Bridge.begin();
  while (!Serial);
  
  Serial.print("Procesos iniciados.");

}

The rest of the void setup does not work at all !

So forget about the other part of the code and just concentrate on getting bytes in from your reader. As it stands you are not getting past:-

void loop() 
{
    while(rfid.available()) {
      
      Serial.print("Listo para leer");

As soon as the first byte appears you should see this print out. The fact you are not seeing it means that your reader is not wired up right and sending serial data into your arduino. Check the grounds, check you have the TX and RX right. These can be confusing because you might have to wire the TX to TX or a TX to RX depending on how these things are defined on your reader. Check you have a card that is producing a valid read. A good idea is to wire a 1K resistor and LED to act as a probe. Connect the resistor to +5V and the LED to the TX and when card data is sent you should see it flash. Connecting it the other way round the LED will blink off when data is sent, this is very much harder to see.

The problem lies in the connection with the pins, the one way I could run with the ID-20 was connecting pins 10 and 11, with its RX and TX respectively.

Now 'running all code, but it has emerged a new problem, that maybe really maybe be a question rather than a problem, what happens is that the code prints me the ID of the RFID card, but printed out discontinuous, I mean it prints one by one, thanks to this code:

f (bytesread == 12) {                          // if 12 digit read is complete
          Serial.print("5-byte code: ");
          for (i=0; i<5; i++) {
            if (code[i] < 16) {
              Serial.print("0");
            }          
            Serial.print(code[i], HEX); 
            Serial.print(" "); 
          }
          Serial.println();
        }

What I need is this ID that prints on the Serial monitor, store it in a String with which to work, try to do something similar but does not work, this is the modified and finished code, including the shape of the string to store the id:

#include <SoftwareSerial.h>
#include <Bridge.h>

SoftwareSerial rfid(10,11); // RX,TX (el TX no se usa)
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;

String tag = "";
  
  
void setup() {
  Serial.begin(9600);
  rfid.begin(9600);
  Bridge.begin();
  while (!Serial);
  
  Serial.println("Procesos iniciados.");

}

void loop() 
{
    while(rfid.available()) {
      if((val = rfid.read()) == 2) {                  // check for header 
        bytesread = 0; 
        while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
          if( rfid.available() > 0) { 
            val = rfid.read();
            if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading 
              break;                                    // stop reading
            }
  
            // Do Ascii/Hex conversion:
            if ((val >= '0') && (val <= '9')) {
              val = val - '0';
            } else if ((val >= 'A') && (val <= 'F')) {
              val = 10 + val - 'A';
            }
  
            // Every two hex-digits, add byte to code:
            if (bytesread & 1 == 1) {
              // make some space for this hex-digit by
              // shifting the previous hex-digit with 4 bits to the left:
              code[bytesread >> 1] = (val | (tempbyte << 4));
  
              if (bytesread >> 1 != 5) {                // If we're at the checksum byte,
                checksum ^= code[bytesread >> 1];       // Calculate the checksum... (XOR)
              };
            } else {
              tempbyte = val;                           // Store the first hex digit first...
            };
  
            bytesread++;                                // ready to read next digit
          } 
        } 
  
        // Output to Serial:
  
        if (bytesread == 12) {                          // if 12 digit read is complete
          Serial.print("5-byte code: ");
          for (i=0; i<5; i++) {
            if (code[i] < 16) {
              Serial.print("0");
              tag = tag + ("0");
            }          
            Serial.print(code[i], HEX); 
            tag = tag +(code[i], HEX);
            Serial.print(" "); 
            tag = tag +(" ");
          }
          Serial.println();
          Serial.println(tag);
          Serial.println();
          tag = "";
        }
  
        bytesread = 0;
      }
      delay(1000);
    }
  }

but printed out discontinuous, I mean it prints one by one, thanks to this code:

That is because of:-

}
          Serial.println();
        }

change it to

} }
          Serial.println();

Also:-

||(val == 0x03)||(val == 0x02))

Why stop at the header, that is when you want to start.

i change the keys as you said but I still throwing Otherwise follows the String, attached a picture of the Serial monitor.
The problem is that of the “016 016 16 16 16” printed on the screen you see the string above.

Basically you have still not got the idea of reading in serial data. An important thing to remember is that you are processing data a lot quicker than it comes in. Look at what your code does when rfid.available() is equal to zero and you haven't yet got all the data in from the serial port. Your code assumes that all the serial data arrives at once. One simple solution might be to wait until there are 12 bytes in the buffer before trying to read any. However this is your code so you will have to try it.

What I need is this ID that prints on the Serial monitor, store it in a String with which to work

A String and a string are two different things. You should learn to use strings, not Strings.

what difference you mean in String and string ?

A string is a NULL terminated array of chars. A String is a class that wraps a NULL terminated array of chars. Nothing that the wrapper does is done using anything more than standard C functions. All that it offers is dynamic sizing of the string that it wraps. But, you don't need that because your strings are a known, static size.

jajaja great, now if I confused ... in what way does this affect my code? concatenate a NULL object would be??

in what way does this affect my code?

You haven't written any code to actually collect the data, yet. So, the choice of string vs. String doesn't yet affect your code.

concatenate a NULL object would be??

It's not a NULL object. It's a NULL.

char rfid_tag[16];
byte index = 0;

rfid_tag[index] = Serial.read();
index++;
rfid_tag[index] = '\0'; // NULL terminate the array

ok, now I understand better, I modify the code changed to a string, and of course, now if I works better than the other, I even had no need for a aregglo of 16 characters (char rfid_tag ??[16]) but leave only in 13 (IDString [13]).

In the end, I turned this code:

#include <SoftwareSerial.h>
#include <Bridge.h>

SoftwareSerial rfid(10,11); // RX,TX (el TX no se usa)

char val = 0;

void setup() {
    Serial.begin(9600); 
    rfid.begin(9600);
    Bridge.begin();
    while (!Serial);
    
    Serial.println("Procesos iniciados.");

}

void loop () {

    char IDstring[13];
    int  i;

    if (rfid.available() > 0 ) {
      if ( (val = rfid.read()) == 02 ) { 
        for ( i = 0; (val = rfid.read()) != 03 ; i++) {
          Serial.print(val, HEX);
          Serial.print(" ");
          IDstring[i] = val;
        }
        Serial.println();
        Serial.print("  IDString[");
        IDstring[10] = 0x00;
        Serial.print(IDstring);
        Serial.println("]");
        Serial.println();
        delay(100);
      }
    }
 
}

PaulS, thank you very much!