I am trying to print data from an rfid tag to an lcd display. the data is in hexadecimal(i think).
when i print it to my lcd, it shows all numbers and no text. please help me, i need to submit it quick.
Here's my code
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
LiquidCrystal_I2C lcd(0x27,20,4);
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
//*****************************************************************************************//
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init();
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
}
//*****************************************************************************************//
void loop() {
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
//some variables we need
byte block;
byte len;
MFRC522::StatusCode status;
//-------------------------------------------
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//-------------------------------------------
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
//-------------------------------------------
lcd.print(F("Name: "));
byte buffer1[18];
block = 2;
len = 18;
//------------------------------------------- GET FIRST NAME
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 2, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
if (status != MFRC522::STATUS_OK) {
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer1, &len);
if (status != MFRC522::STATUS_OK) {
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//PRINT FIRST NAME
for (uint8_t i = 0; i < 16; i++)
{
if (buffer1[i] != 32)
{
Serial.write(buffer1[i]);
lcd.print(buffer1[i]);
}
}
delay(3000);
lcd.clear();
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
Your array is of type byte - so the print will get numbers out.
If you want to see an hexadécimal string you need to check each value. If it’s between 0 and 9 then just print it but if it’s between 10 and 15 you need to print A to F.
Thanks for replying so quickly on the forum, i think i need to be a little more accurate and specific with my question.
when i print 'buffer1[i]' to my lcd, i get numbers. I want text. i have stored the value 500 as in 500 points. this was for a credit card play system. instead of displaying 500 on the lcd, it is showing some numbers. Also, I want to know how i can compare and add values to the card for example, if this button pressed, then 500=500+1. I need a little help here as I am a new developer... Thanks.
Do you mean that buffer1 contains the ASCII characters for "500", or that the binary value of 500 is stored in buffer1? Since buffer1 is declared as a byte array, LCD.print will display the numeric value of the byte. If buffer1 contains ASCII characters, if there is no LCD.write() function you can cast to char when printing.
//PRINT FIRST NAME
for (uint8_t i = 0; i < 16; i++)
{
if (buffer1[i] != 32)
{
Serial.write(buffer1[i]);
lcd.print((char)buffer1[i]);
}
}
Likely problem is that there is some non-ASCII data being read off the rfid tag into buffer1. The code is checking for the ASCII code for space but any other non-printing character will give odd results on the LCD.
< edit >
You can try changing the if statement to skip over any non-printing characters, but there may still be some non ASCII content on the rfid tag.
//PRINT FIRST NAME
for (uint8_t i = 0; i < 16; i++)
{
if (buffer1[i] > 32) //do not print non-printing characters
{
Serial.write(buffer1[i]);
lcd.print((char)buffer1[i]);
}
}