#include <SPI.h>
#include <MFRC522.h>
#include <EEPROM.h>
#define RST_PIN 9
#define SS_PIN 10
byte readCard[4];
int successRead;
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("RFID KART KAYIT UYGULAMASI");
Serial.println("--------------------------");
Serial.println("Lutfen karti okutun");
Serial.println();
}
void loop()
{
//yeni kart okununmadıkça devam etme
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
getID(); //kart ID oku
delay(100);
SektorOku(); //Sektör oku
delay(100);
SektorYaz();
//**********************************************************************************
delay(200);
}
///Rfid kartı id sini oku.
int getID() {
Serial.print("Kart UID'si: ");
//kartın UID'sini byte byte oku ve seri monitöre yaz
for (int i = 0; i < mfrc522.uid.size; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
}
Serial.println("");
//kart okumayı durdur ve 1 döndür (okuma başarılı)
mfrc522.PICC_HaltA();
return 1;
}
void SektorOku()
{
// In this sample we use the second sector,
// that is: sector #1, covering block #4 up to and including block #7
byte sector = 1;
byte blockAddr = 4;
byte trailerBlock = 7;
byte status;
byte buffer[18];
byte size = sizeof(buffer);
// Read data from the block
Serial.print(F("Reading data into block ")); Serial.print(blockAddr);
Serial.println(F(" ..."));
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size); //sektörü oku buffer ata
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Hata: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.print(F("Okunan Data- (Blok)")); Serial.print(blockAddr); Serial.println(F(":"));
dump_byte_array(buffer, 16); //gelen datayı diziye ata göster
Serial.println();
}
void SektorYaz()
{
byte blockAddr = 4;
byte dataBlock[] = {
0x01, 0x02, 0x03, 0x04, // 1, 2, 3, 4,
0x05, 0x06, 0x07, 0x08, // 5, 6, 7, 8,
0x08, 0x09, 0xff, 0x0b, // 9, 10, 255, 12,
0x0c, 0x0d, 0x0e, 0x0f // 13, 14, 15, 16
};
byte status;
byte buffer[18];
byte size = sizeof(buffer);
// Write data to the block
Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
Serial.println(F(" ..."));
status = mfrc522.MIFARE_Write(blockAddr, dataBlock, 16); //dataBlock dizisindeki yeni değerleri karta yaz
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Hata: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
Serial.println();
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
[code]