-26-
v7.0
Code examples and extended information
void setup()
{
USB.ON();
USB.println(“RFID/NFC @ 13.56 MHz module started”);
// switchs ON the module, type B, and asigns the socket
RFID13.ON(SOCKET0);
delay(1000);
}
void loop()
{
USB.print(“\r\n++++++++++++++++++++++++++++++++++++”);
// **** init the RFID/NFC reader
state = RFID13.init(UID, aux);
if (aux[0] == aux[1]) // if so, there is no card on the EM field
{
USB.print(“\r\nRequest error - no card found”);
}
else // a card was found
{
// **** authenticate the key A of sector 1
state = RFID13.authenticate(UID, 1, keyAccess);
if (firstDone == false) // this part is for adding credit
{
// **** if passed authentication in block 1, we are able to set the inital credit
sprintf(text, “%d”, initialCredit);
RFID13.string2vector(text, aux);
// **** write aux in block number 1, and check afterwards
state = RFID13.writeAndCheck(aux, 1);
if (state == 0)
{
for (int i=0; i<sizeof(text); i++) // clear this variable
{
text[i] = ‘\0’;
}
firstDone = true;
// **** check the 16 bytes in block 1
state = RFID13.read(1, aux);
if (state == 0) // if the read command was successful, we show the data (16 bytes)
{
USB.print(“\r\n Initial credit set: “);
for (int i=0; i<16; i++)
{
if (aux[i] == ‘\0’) // to avoid showing voids
break;
USB.print(aux[i], BYTE); // print the 16 bits of block number 1, now in ASCII
code
}
}
}
} // end of “adding credit part”
// **** if passed authentication in block 1, we will be able to read the data in this
block (the credit)
state = RFID13.read(1, aux);
if (state == 0) // if the read command was successful, we show the data (16 bytes)
{
USB.print(“\r\n Credit before the trip: “);
for (int i=0; i<16; i++)
{
if (aux[i] == ‘\0’) // to avoid showing voids
break;
USB.print(aux[i], BYTE); // print the 16 bytes of the block 1 (the first ones are
the credit, the rest are NULLs)
}