Hi,
keyA is on line 32
Where did you get this code?
What is this KeyA?
Try this code:
Added line 14 and modified line 34.
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int buttonPin = 2;
int points = 0;
MFRC522::MIFARE_Key keyA;
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
lcd.init();
lcd.backlight();
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Look for new cards
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
byte blockNumber = 4;
byte trailerBlock = 7;
byte buffer[18];
byte size = sizeof(buffer);
// Authenticate using key A
if ((MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &keyA, &(mfrc522.uid))) {
// Read data from the block
if (mfrc522.MIFARE_Read(blockNumber, buffer, &size) == MFRC522::STATUS_OK) {
// Extract the points
points = buffer[0];
Serial.print("Points: ");
Serial.println(points);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Points: ");
lcd.print(points);
lcd.setCursor(0, 1);
lcd.print("Hold card & press");
// Wait for button press
while (digitalRead(buttonPin) == HIGH) {}
// Add ten points
points += 10;
Serial.print("New points: ");
Serial.println(points);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Credit Added");
lcd.setCursor(0, 1);
lcd.print("Remove card");
delay(1000);
// Write the new points to the block
buffer[0] = points;
if (mfrc522.MIFARE_Write(blockNumber, buffer, 16) == MFRC522::STATUS_OK) {
Serial.println("Points updated on card");
}
}
// Stop crypto1 communication
mfrc522.PCD_StopCrypto1();
}
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
delay(1000);
lcd.clear();
}
}