I am working on a code for an ATM Machine with an MFRC52 Reader, The code has to read points from a particular block on the rfid card which is like the debit card then Display it on an LCD display with an I2C module and when a button is pressed add one point to the cards internal storage and then display the new value on an LCD display after that, update the new value to the same block on the card which is like adding money to a debit card and then display that you can remove your card now
This is Where I have gotten so far
#include <Wire.h> // Include Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include LiquidCrystal_I2C library for LCD display
#include <SPI.h> // Include SPI library for MFRC52 reader
#include <MFRC522.h> // Include MFRC52 library for RFID reader
#define SS_PIN 10 // Define SS_PIN for MFRC52 reader
#define RST_PIN 9 // Define RST_PIN for MFRC52 reader
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC52 object
LiquidCrystal_I2C lcd(0x27, 16, 2); // Create LCD object with I2C address and size
const int buttonPin = 2; // Define button pin
int buttonState = 0; // Initialize button state
void setup() {
Serial.begin(9600); // Initialize serial communication
SPI.begin(); // Initialize SPI communication
mfrc522.PCD_Init(); // Initialize MFRC52 reader
lcd.init(); // Initialize LCD display
lcd.backlight(); // Turn on backlight
}
void loop() {
// Check if a card is present
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
Serial.println("Card detected!");
// Read data from the card
byte blockNumber = 4; // Define block number to read
byte buffer[18];
byte size = sizeof(buffer);
MFRC522::StatusCode status = mfrc522.MIFARE_Read(blockNumber, buffer, &size);
if (status == MFRC522::STATUS_OK) {
Serial.print("Points: ");
Serial.println(buffer[0]);
// Display data on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Points: ");
lcd.print(buffer[0]);
// Wait for button press
while (digitalRead(buttonPin) == LOW) {
delay(10);
}
// Increment points and update the card
buffer[0]++; // Increment points
status = mfrc522.MIFARE_Write(blockNumber, buffer, 16);
if (status == MFRC522::STATUS_OK) {
// Display updated data on the LCD
lcd.setCursor(0, 1);
lcd.print("New points: ");
lcd.print(buffer[0]);
delay(1000);
lcd.clear();
lcd.print("Remove card.");
}
} else {
Serial.println("Error reading data from card.");
}
mfrc522.PICC_HaltA(); // Halt card
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
}
}
But Something is wrong and I really need your help
THis is the Serial Monitor
And This is What is Happening