Hello,
I have recently been working on a code that will read and write data to and from a Mifare Classic 1k card. I created a code from scratch using the IDE program. It receives a 3 digit number from a 4x4 keypad, reads the initial value on the card, adds it to the keypad value, then writes it to the card, overwriting the previous number. I also added some serial print outs in the code to help me diagnose the problem, and it seems as though it gets the number, but has trouble writing it to the card. I am using a RC-522 shield on an Arduino Uno. (I know this is unrelated to the problem, but I am also using a 16x2 LCD with a I2c backpack to display other information) Here is the code:
#include <SPI.h>
#include <Wire.h>
#include <Keypad.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <LiquidCrystal_I2C.h>
#define ss_pin 10
#define rst_pin 9
int character = 0;
char Str[16] = {'A', 'm', 'o', 'u', 'n', 't', ':', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
MFRC522 rfid(ss_pin, rst_pin);
MFRC522::MIFARE_Key key;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
bool addMode = false;
const byte rows = 4;
const byte cols = 4;
char keys[rows][cols] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[rows] = {8,7,6,5};
byte colPins[cols] = {4,3,2,A1};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols);
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
//LCD Init
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.clear();
lcd.display();
lcd.clear();
lcd.print("Insert Card");
lcd.setCursor(0,1);
lcd.print("and Type Amount");
lcd.display();
}
void loop() {
// put your main code here, to run repeatedly:
char keyp = keypad.getKey();
if (keyp){
if(character == 0)
{
Serial.println(keyp);
Str[9] = keyp;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" -Type Amount-");
lcd.setCursor(0,1);
lcd.print(Str);
}
if(character == 1)
{
Serial.println(keyp);
Str[10] = keyp;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" -Type Amount-");
lcd.setCursor(0,1);
lcd.print(Str);
}
if(character == 2)
{
Serial.println(keyp);
Str[11] = keyp;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" -Type Amount-");
lcd.setCursor(0,1);
lcd.print(Str);
}
character=character+1;
}
byte amt1 = ((Str[9] - 48) * 100);
byte amt2 = ((Str[10] - 48) * 10);
byte amt3 = (Str[11] - 48);
if (character == 3)
{
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
byte sector = 1;
byte blockAddr = 4;
byte trailerblock = 7;
byte dataBlock[16];
byte buffer[18];
byte size = sizeof(buffer);
byte amt = (amt1 + amt2 + amt3);
MFRC522::StatusCode status;
Serial.println(F("Authenticating using key A...."));
status = (MFRC522::StatusCode) rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerblock, &key, &(rfid.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(rfid.GetStatusCodeName(status));
return;
}
Serial.print("Current Balence: ");
Serial.println(rfid.MIFARE_Read(blockAddr, buffer, &size));
byte tamt = amt + (rfid.MIFARE_Read(blockAddr, buffer, &size));
Serial.print("New Balence should be: ");
Serial.println(tamt);
tamt = buffer[15];
dataBlock[15] = tamt;
status = (MFRC522::StatusCode) rfid.MIFARE_Write(blockAddr, dataBlock, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(rfid.GetStatusCodeName(status));
}
Serial.print(F("New Balence Written: "));
Serial.println(rfid.MIFARE_Read(blockAddr, buffer, &size));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(rfid.GetStatusCodeName(status));
return;
}
Serial.println(F("Current data in sector:"));
rfid.PICC_DumpMifareClassicSectorToSerial(&(rfid.uid), &key, sector);
Serial.println();
}
}
Any ideas? Did I do anything stupid? Any suggestions are highly appreciated. Thanks for your help!
-Samuel Johnson