//This program reads and writes to TAGs
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>
//Pinos Reset e SS módulo MFRC522
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal lcd(2,3,4,5,6,7);
#define pino_botao_le A2
#define pino_botao_gr A3
MFRC522::MIFARE_Key key;
void setup()
{
pinMode(pino_botao_le, INPUT);
pinMode(pino_botao_gr, INPUT);
Serial.begin(9600); //Inicia a serial
SPI.begin(); //Inicia SPI bus
mfrc522.PCD_Init(); //Inicia MFRC522
//Inicializa o LCD 16x2
lcd.begin(16, 2);
mensageminicial();
//prepares key - factory standard = FFFFFFFFFFFFh
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
}
void loop()
{
//Checks whether the read mode button has been pressed
int modo_le = digitalRead(pino_botao_le);
if (modo_le != 0)
{
lcd.clear();
Serial.println("Reading mode selected");
lcd.setCursor(2, 0);
lcd.print("Reading mode");
lcd.setCursor(3, 1);
lcd.print("selected");
while (digitalRead(pino_botao_le) == 1) {}
delay(3000);
modo_leitura();
}
//Checks whether the recording mode button has been pressed
int modo_gr = digitalRead(pino_botao_gr);
if (modo_gr != 0)
{
lcd.clear();
Serial.println("Recording mode selected");
lcd.setCursor(2, 0);
lcd.print("Recording mode");
lcd.setCursor(3, 1);
lcd.print("selected");
while (digitalRead(pino_botao_gr) == 1) {}
delay(3000);
modo_gravacao();
}
}
void mensageminicial()
{
Serial.println("Select read or write mode...");
Serial.println();
lcd.clear();
lcd.print("Select the mode");
lcd.setCursor(0, 1);
lcd.print("reading or writing");
}
void mensagem_inicial_cartao()
{
Serial.println("Bring the TAG closer to the reader...");
lcd.clear();
lcd.print(" Bring the");
lcd.setCursor(0, 1);
lcd.print("TAG of reader");
}
void modo_leitura()
{
mensagem_inicial_cartao();
//Awaits TAG
while ( ! mfrc522.PICC_IsNewCardPresent())
{
delay(100);
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID na serial
Serial.print("UID da tag : ");
String conteudo = "";
byte letra;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
conteudo.concat(String(mfrc522.uid.uidByte[i]<0x10 ? " 0" : " "));
conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
//Get sector data 1, blcok 4 = Name of product
byte sector = 1;
byte blockAddr = 4;
byte trailerBlock = 7;
byte status;
byte buffer[18];
byte size = sizeof(buffer);
//Authentication using key A
status=mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
//Shows the name data on the Serial Monitor and LCD
lcd.clear();
lcd.setCursor(0, 0);
for (byte i = 1; i < 16; i++)
{
Serial.print(char(buffer[i]));
lcd.write(char(buffer[i]));
}
Serial.println();
//Get sector data 0, bloco 1 = Value
sector = 0;
blockAddr = 1;
trailerBlock = 3;
//Authentication using key A
status=mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
if (status != MFRC522::STATUS_OK)
{
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
//Shows the value data on the Serial Monitor and LCD
lcd.setCursor(0, 1);
for (byte i = 0; i < 16; i++)
{
Serial.print(char(buffer[i]));
lcd.write(char(buffer[i]));
}
Serial.println();
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
delay(3000);
mensageminicial();
}
void modo_gravacao()
{
mensagem_inicial_cartao();
//Aguarda cartao
while ( ! mfrc522.PICC_IsNewCardPresent()) {
delay(100);
}
if ( ! mfrc522.PICC_ReadCardSerial()) return;
//Mostra UID na serial
Serial.print(F("UID do Cartao: ")); //Dump UID
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
//Mostra o tipo do cartao
Serial.print(F("Tipo do PICC: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
byte buffer[34];
byte block;
byte status, len;
Serial.setTimeout(20000L) ;
Serial.println(F("Enter the Value plus the character #"));
lcd.clear();
lcd.print("Enter the Value:");
lcd.setCursor(0, 1);
lcd.print("me + #");
len = Serial.readBytesUntil('#', (char *) buffer, 30) ;
for (byte i = len; i < 30; i++) buffer[i] = ' ';
block = 1;
//Serial.println(F("Autenticacao usando chave A..."));
status=mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//Writes to block 1
status = mfrc522.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
block = 2;
//Serial.println(F("Autenticacao usando chave A..."));
status=mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//Grava no bloco 2
status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
Serial.println(F("Enter Product plus character #"));
lcd.clear();
lcd.print("Enter name of Product);
lcd.setCursor(0, 1);
lcd.print("after #");
len = Serial.readBytesUntil('#', (char *) buffer, 20) ;
for (byte i = len; i < 20; i++) buffer[i] = ' ';
block = 4;
//Serial.println(F("Authenticate using key A..."));
status=mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//Grava no bloco 4
status = mfrc522.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
block = 5;
//Serial.println(F("Authenticating using key A..."));
status=mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A,
block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//Grava no bloco 5
status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
//return;
}
else
{
Serial.println(F("Data successfully saved!"));
lcd.clear();
lcd.print("Success");
}
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
delay(5000);
mensageminicial();
}
The program is working correctly to write and read the tag information, however, I need help to make a program that performs only the reading and adds the values of the tags in a variable or vector and then shows on the display