Update examples, use F() macro
Saves a lot of memory, examples now run at a arduino uno
This commit is contained in:
@@ -54,7 +54,7 @@ void setup() {
|
||||
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
|
||||
SPI.begin(); // Init SPI bus
|
||||
mfrc522.PCD_Init(); // Init MFRC522 card
|
||||
Serial.println("Warning: this example overwrites the UID of your UID changeable card, use with care!");
|
||||
Serial.println(F("Warning: this example overwrites the UID of your UID changeable card, use with care!"));
|
||||
|
||||
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
|
||||
for (byte i = 0; i < 6; i++) {
|
||||
@@ -83,7 +83,7 @@ void loop() {
|
||||
// Now a card is selected. The UID and SAK is in mfrc522.uid.
|
||||
|
||||
// Dump UID
|
||||
Serial.print("Card UID:");
|
||||
Serial.print(F("Card UID:"));
|
||||
for (byte i = 0; i < mfrc522.uid.size; i++) {
|
||||
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
|
||||
Serial.print(mfrc522.uid.uidByte[i], HEX);
|
||||
@@ -92,22 +92,22 @@ void loop() {
|
||||
|
||||
// Dump PICC type
|
||||
// byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
|
||||
// Serial.print("PICC type: ");
|
||||
// Serial.print(F("PICC type: "));
|
||||
// Serial.print(mfrc522.PICC_GetTypeName(piccType));
|
||||
// Serial.print(" (SAK ");
|
||||
// Serial.print(F(" (SAK "));
|
||||
// Serial.print(mfrc522.uid.sak);
|
||||
// Serial.print(")\r\n");
|
||||
// if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
|
||||
// && piccType != MFRC522::PICC_TYPE_MIFARE_1K
|
||||
// && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
|
||||
// Serial.println("This sample only works with MIFARE Classic cards.");
|
||||
// Serial.println(F("This sample only works with MIFARE Classic cards."));
|
||||
// return;
|
||||
// }
|
||||
|
||||
// Set new UID
|
||||
byte newUid[] = NEW_UID;
|
||||
if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
|
||||
Serial.println("Wrote new UID to card.");
|
||||
Serial.println(F("Wrote new UID to card."));
|
||||
}
|
||||
|
||||
// Halt PICC and re-select it so DumpToSerial doesn't get confused
|
||||
@@ -117,7 +117,7 @@ void loop() {
|
||||
}
|
||||
|
||||
// Dump the new memory contents
|
||||
Serial.println("New UID and contents:");
|
||||
Serial.println(F("New UID and contents:"));
|
||||
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
|
||||
|
||||
delay(2000);
|
||||
|
||||
@@ -48,11 +48,11 @@ MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600); // Initialize serial communications with the PC
|
||||
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
|
||||
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
|
||||
SPI.begin(); // Init SPI bus
|
||||
mfrc522.PCD_Init(); // Init MFRC522
|
||||
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
|
||||
Serial.println("Scan PICC to see UID, type, and data blocks...");
|
||||
Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -73,17 +73,17 @@ void loop() {
|
||||
void ShowReaderDetails() {
|
||||
// Get the MFRC522 software version
|
||||
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
||||
Serial.print("MFRC522 Software Version: 0x");
|
||||
Serial.print(F("MFRC522 Software Version: 0x"));
|
||||
Serial.print(v, HEX);
|
||||
if (v == 0x91)
|
||||
Serial.print(" = v1.0");
|
||||
Serial.print(F(" = v1.0"));
|
||||
else if (v == 0x92)
|
||||
Serial.print(" = v2.0");
|
||||
Serial.print(F(" = v2.0"));
|
||||
else
|
||||
Serial.print(" (unknown)");
|
||||
Serial.print(F(" (unknown)"));
|
||||
Serial.println("");
|
||||
// When 0x00 or 0xFF is returned, communication probably failed
|
||||
if ((v == 0x00) || (v == 0xFF)) {
|
||||
Serial.println("WARNING: Communication failure, is the MFRC522 properly connected?");
|
||||
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ void setup() {
|
||||
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
|
||||
SPI.begin(); // Init SPI bus
|
||||
mfrc522.PCD_Init(); // Init MFRC522 card
|
||||
Serial.println("Warning: this example clears your mifare UID, use with care!");
|
||||
Serial.println(F("Warning: this example clears your mifare UID, use with care!"));
|
||||
|
||||
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
|
||||
for (byte i = 0; i < 6; i++) {
|
||||
@@ -62,7 +62,7 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
if ( mfrc522.MIFARE_UnbrickUidSector(false) ) {
|
||||
Serial.println("Cleared sector 0, set UID to 1234. Card should be responsive again now.");
|
||||
Serial.println(F("Cleared sector 0, set UID to 1234. Card should be responsive again now."));
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
@@ -53,12 +53,12 @@ void setup() {
|
||||
key.keyByte[i] = 0xFF;
|
||||
}
|
||||
|
||||
Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Block mode.");
|
||||
Serial.print("Using key (for A and B):");
|
||||
Serial.println(F("Scan a MIFARE Classic PICC to demonstrate Value Block mode."));
|
||||
Serial.print(F("Using key (for A and B):"));
|
||||
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
|
||||
Serial.println();
|
||||
|
||||
Serial.println("BEWARE: Data will be written to the PICC, in sector #1");
|
||||
Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,10 +74,10 @@ void loop() {
|
||||
return;
|
||||
|
||||
// Show some details of the PICC (that is: the tag/card)
|
||||
Serial.print("Card UID:");
|
||||
Serial.print(F("Card UID:"));
|
||||
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
|
||||
Serial.println();
|
||||
Serial.print("PICC type: ");
|
||||
Serial.print(F("PICC type: "));
|
||||
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
|
||||
Serial.println(mfrc522.PICC_GetTypeName(piccType));
|
||||
|
||||
@@ -85,7 +85,7 @@ void loop() {
|
||||
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
|
||||
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
|
||||
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
|
||||
Serial.println("This sample only works with MIFARE Classic cards.");
|
||||
Serial.println(F("This sample only works with MIFARE Classic cards."));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -101,16 +101,16 @@ void loop() {
|
||||
long value;
|
||||
|
||||
// Authenticate using key A
|
||||
Serial.println("Authenticating using key A...");
|
||||
Serial.println(F("Authenticating using key A..."));
|
||||
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("PCD_Authenticate() failed: ");
|
||||
Serial.print(F("PCD_Authenticate() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
|
||||
// Show the whole sector as it currently is
|
||||
Serial.println("Current data in sector:");
|
||||
Serial.println(F("Current data in sector:"));
|
||||
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
|
||||
Serial.println();
|
||||
|
||||
@@ -156,10 +156,10 @@ void loop() {
|
||||
mfrc522.MIFARE_SetAccessBits(&trailerBuffer[6], 0, 6, 6, 3);
|
||||
|
||||
// Read the sector trailer as it is currently stored on the PICC
|
||||
Serial.println("Reading sector trailer...");
|
||||
Serial.println(F("Reading sector trailer..."));
|
||||
status = mfrc522.MIFARE_Read(trailerBlock, buffer, &size);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Read() failed: ");
|
||||
Serial.print(F("MIFARE_Read() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
@@ -169,20 +169,20 @@ void loop() {
|
||||
&& buffer[7] != trailerBuffer[7]
|
||||
&& buffer[8] != trailerBuffer[8]) {
|
||||
// They don't match (yet), so write it to the PICC
|
||||
Serial.println("Writing new sector trailer...");
|
||||
Serial.println(F("Writing new sector trailer..."));
|
||||
status = mfrc522.MIFARE_Write(trailerBlock, trailerBuffer, 16);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Write() failed: ");
|
||||
Serial.print(F("MIFARE_Write() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Authenticate using key B
|
||||
Serial.println("Authenticating again using key B...");
|
||||
Serial.println(F("Authenticating again using key B..."));
|
||||
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("PCD_Authenticate() failed: ");
|
||||
Serial.print(F("PCD_Authenticate() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
@@ -198,20 +198,20 @@ void loop() {
|
||||
Serial.print("Adding 1 to value of block "); Serial.println(valueBlockA);
|
||||
status = mfrc522.MIFARE_Increment(valueBlockA, 1);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Increment() failed: ");
|
||||
Serial.print(F("MIFARE_Increment() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
status = mfrc522.MIFARE_Transfer(valueBlockA);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Transfer() failed: ");
|
||||
Serial.print(F("MIFARE_Transfer() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
// Show the new value of valueBlockA
|
||||
status = mfrc522.MIFARE_GetValue(valueBlockA, &value);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("mifare_GetValue() failed: ");
|
||||
Serial.print(F("mifare_GetValue() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
@@ -222,31 +222,31 @@ void loop() {
|
||||
Serial.print("Subtracting 10 from value of block "); Serial.println(valueBlockB);
|
||||
status = mfrc522.MIFARE_Decrement(valueBlockB, 10);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Decrement() failed: ");
|
||||
Serial.print(F("MIFARE_Decrement() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
status = mfrc522.MIFARE_Transfer(valueBlockB);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Transfer() failed: ");
|
||||
Serial.print(F("MIFARE_Transfer() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
// Show the new value of valueBlockB
|
||||
status = mfrc522.MIFARE_GetValue(valueBlockB, &value);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("mifare_GetValue() failed: ");
|
||||
Serial.print(F("mifare_GetValue() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
Serial.print("New value of value block "); Serial.print(valueBlockB);
|
||||
Serial.print(" = "); Serial.println(value);
|
||||
Serial.print(F("New value of value block ")); Serial.print(valueBlockB);
|
||||
Serial.print(F(" = ")); Serial.println(value);
|
||||
// Check some boundary...
|
||||
if (value <= -100) {
|
||||
Serial.println("Below -100, so resetting it to 255 = 0xFF just for fun...");
|
||||
Serial.println(F("Below -100, so resetting it to 255 = 0xFF just for fun..."));
|
||||
status = mfrc522.MIFARE_SetValue(valueBlockB, 255);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("mifare_SetValue() failed: ");
|
||||
Serial.print(F("mifare_SetValue() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
@@ -280,10 +280,10 @@ void formatValueBlock(byte blockAddr) {
|
||||
byte size = sizeof(buffer);
|
||||
byte status;
|
||||
|
||||
Serial.print("Reading block "); Serial.println(blockAddr);
|
||||
Serial.print(F("Reading block ")); Serial.println(blockAddr);
|
||||
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Read() failed: ");
|
||||
Serial.print(F("MIFARE_Read() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
@@ -301,10 +301,10 @@ void formatValueBlock(byte blockAddr) {
|
||||
&& (buffer[12] == (byte)~buffer[13])
|
||||
&& (buffer[12] == buffer[14])
|
||||
&& (buffer[12] == (byte)~buffer[15])) {
|
||||
Serial.println("Block has correct Value Block format.");
|
||||
Serial.println(F("Block has correct Value Block format."));
|
||||
}
|
||||
else {
|
||||
Serial.println("Formatting as Value Block...");
|
||||
Serial.println(F("Formatting as Value Block..."));
|
||||
byte valueBlock[] = {
|
||||
0, 0, 0, 0,
|
||||
255, 255, 255, 255,
|
||||
@@ -312,7 +312,7 @@ void formatValueBlock(byte blockAddr) {
|
||||
blockAddr, ~blockAddr, blockAddr, ~blockAddr };
|
||||
status = mfrc522.MIFARE_Write(blockAddr, valueBlock, 16);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Write() failed: ");
|
||||
Serial.print(F("MIFARE_Write() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,12 +52,12 @@ void setup() {
|
||||
key.keyByte[i] = 0xFF;
|
||||
}
|
||||
|
||||
Serial.println("Scan a MIFARE Classic PICC to demonstrate read and write.");
|
||||
Serial.print("Using key (for A and B):");
|
||||
Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));
|
||||
Serial.print(F("Using key (for A and B):"));
|
||||
dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
|
||||
Serial.println();
|
||||
|
||||
Serial.println("BEWARE: Data will be written to the PICC, in sector #1");
|
||||
Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,10 +73,10 @@ void loop() {
|
||||
return;
|
||||
|
||||
// Show some details of the PICC (that is: the tag/card)
|
||||
Serial.print("Card UID:");
|
||||
Serial.print(F("Card UID:"));
|
||||
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
|
||||
Serial.println();
|
||||
Serial.print("PICC type: ");
|
||||
Serial.print(F("PICC type: "));
|
||||
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
|
||||
Serial.println(mfrc522.PICC_GetTypeName(piccType));
|
||||
|
||||
@@ -84,7 +84,7 @@ void loop() {
|
||||
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
|
||||
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
|
||||
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
|
||||
Serial.println("This sample only works with MIFARE Classic cards.");
|
||||
Serial.println(F("This sample only works with MIFARE Classic cards."));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -104,82 +104,82 @@ void loop() {
|
||||
byte size = sizeof(buffer);
|
||||
|
||||
// Authenticate using key A
|
||||
Serial.println("Authenticating using key A...");
|
||||
Serial.println(F("Authenticating using key A..."));
|
||||
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("PCD_Authenticate() failed: ");
|
||||
Serial.print(F("PCD_Authenticate() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
|
||||
// Show the whole sector as it currently is
|
||||
Serial.println("Current data in sector:");
|
||||
Serial.println(F("Current data in sector:"));
|
||||
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
|
||||
Serial.println();
|
||||
|
||||
// Read data from the block
|
||||
Serial.print("Reading data from block "); Serial.print(blockAddr);
|
||||
Serial.println(" ...");
|
||||
Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
|
||||
Serial.println(F(" ..."));
|
||||
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Read() failed: ");
|
||||
Serial.print(F("MIFARE_Read() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
}
|
||||
Serial.print("Data in block "); Serial.print(blockAddr); Serial.println(":");
|
||||
Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
|
||||
dump_byte_array(buffer, 16); Serial.println();
|
||||
Serial.println();
|
||||
|
||||
// Authenticate using key B
|
||||
Serial.println("Authenticating again using key B...");
|
||||
Serial.println(F("Authenticating again using key B..."));
|
||||
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("PCD_Authenticate() failed: ");
|
||||
Serial.print(F("PCD_Authenticate() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
|
||||
// Write data to the block
|
||||
Serial.print("Writing data into block "); Serial.print(blockAddr);
|
||||
Serial.println(" ...");
|
||||
Serial.print(F("Writing data into block ")); Serial.print(blockAddr);
|
||||
Serial.println(F(" ..."));
|
||||
dump_byte_array(dataBlock, 16); Serial.println();
|
||||
status = mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Write() failed: ");
|
||||
Serial.print(F("MIFARE_Write() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// Read data from the block (again, should now be what we have written)
|
||||
Serial.print("Reading data from block "); Serial.print(blockAddr);
|
||||
Serial.println(" ...");
|
||||
Serial.print(F("Reading data from block ")); Serial.print(blockAddr);
|
||||
Serial.println(F(" ..."));
|
||||
status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Read() failed: ");
|
||||
Serial.print(F("MIFARE_Read() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
}
|
||||
Serial.print("Data in block "); Serial.print(blockAddr); Serial.println(":");
|
||||
Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":"));
|
||||
dump_byte_array(buffer, 16); Serial.println();
|
||||
|
||||
// Check that data in block is what we have written
|
||||
// by counting the number of bytes that are equal
|
||||
Serial.println("Checking result...");
|
||||
Serial.println(F("Checking result..."));
|
||||
byte count = 0;
|
||||
for (byte i = 0; i < 16; i++) {
|
||||
// Compare buffer (= what we've read) with dataBlock (= what we've written)
|
||||
if (buffer[i] == dataBlock[i])
|
||||
count++;
|
||||
}
|
||||
Serial.print("Number of bytes that match = "); Serial.println(count);
|
||||
Serial.print(F("Number of bytes that match = ")); Serial.println(count);
|
||||
if (count == 16) {
|
||||
Serial.println("Success :-)");
|
||||
Serial.println(F("Success :-)"));
|
||||
} else {
|
||||
Serial.println("Failure, no match :-(");
|
||||
Serial.println(" perhaps the write didn't work properly...");
|
||||
Serial.println(F("Failure, no match :-("));
|
||||
Serial.println(F(" perhaps the write didn't work properly..."));
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// Dump the sector data
|
||||
Serial.println("Current data in sector:");
|
||||
Serial.println(F("Current data in sector:"));
|
||||
mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
|
||||
Serial.println();
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ void setup() {
|
||||
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
|
||||
SPI.begin(); // Init SPI bus
|
||||
mfrc522.PCD_Init(); // Init MFRC522 card
|
||||
Serial.println("Try the most used default keys to print block 0 of a MIFARE PICC.");
|
||||
Serial.println(F("Try the most used default keys to print block 0 of a MIFARE PICC."));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -82,10 +82,10 @@ boolean try_key(MFRC522::MIFARE_Key *key)
|
||||
byte block = 0;
|
||||
byte status;
|
||||
|
||||
// Serial.println("Authenticating using key A...");
|
||||
// Serial.println(F("Authenticating using key A..."));
|
||||
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
// Serial.print("PCD_Authenticate() failed: ");
|
||||
// Serial.print(F("PCD_Authenticate() failed: "));
|
||||
// Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return false;
|
||||
}
|
||||
@@ -94,17 +94,17 @@ boolean try_key(MFRC522::MIFARE_Key *key)
|
||||
byte byteCount = sizeof(buffer);
|
||||
status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
// Serial.print("MIFARE_Read() failed: ");
|
||||
// Serial.print(F("MIFARE_Read() failed: "));
|
||||
// Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
}
|
||||
else {
|
||||
// Successful read
|
||||
result = true;
|
||||
Serial.print("Success with key:");
|
||||
Serial.print(F("Success with key:"));
|
||||
dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
|
||||
Serial.println();
|
||||
// Dump block data
|
||||
Serial.print("Block "); Serial.print(block); Serial.print(":");
|
||||
Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
|
||||
dump_byte_array(buffer, 16);
|
||||
Serial.println();
|
||||
}
|
||||
@@ -128,10 +128,10 @@ void loop() {
|
||||
return;
|
||||
|
||||
// Show some details of the PICC (that is: the tag/card)
|
||||
Serial.print("Card UID:");
|
||||
Serial.print(F("Card UID:"));
|
||||
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
|
||||
Serial.println();
|
||||
Serial.print("PICC type: ");
|
||||
Serial.print(F("PICC type: "));
|
||||
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
|
||||
Serial.println(mfrc522.PICC_GetTypeName(piccType));
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ void setup() {
|
||||
Serial.begin(9600); // Initialize serial communications with the PC
|
||||
SPI.begin(); // Init SPI bus
|
||||
mfrc522.PCD_Init(); // Init MFRC522 card
|
||||
Serial.println("Write personal data on a MIFARE PICC ");
|
||||
Serial.println(F("Write personal data on a MIFARE PICC "));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -47,12 +47,12 @@ void loop() {
|
||||
// Select one of the cards
|
||||
if ( ! mfrc522.PICC_ReadCardSerial()) return;
|
||||
|
||||
Serial.print("Card UID:"); //Dump UID
|
||||
Serial.print(F("Card UID:")); //Dump UID
|
||||
for (byte i = 0; i < mfrc522.uid.size; i++) {
|
||||
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
|
||||
Serial.print(mfrc522.uid.uidByte[i], HEX);
|
||||
}
|
||||
Serial.print(" PICC type: "); // Dump PICC type
|
||||
Serial.print(F(" PICC type: ")); // Dump PICC type
|
||||
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
|
||||
Serial.println(mfrc522.PICC_GetTypeName(piccType));
|
||||
|
||||
@@ -62,33 +62,34 @@ void loop() {
|
||||
|
||||
Serial.setTimeout(20000L) ; // wait until 20 seconds for input from serial
|
||||
// Ask personal data: Family name
|
||||
Serial.println("Type Family name, ending with #");
|
||||
Serial.println(F("Type Family name, ending with #"));
|
||||
len=Serial.readBytesUntil('#', (char *) buffer, 30) ; // read family name from serial
|
||||
for (byte i = len; i < 30; i++) buffer[i] = '\s'; // pad with spaces
|
||||
|
||||
block = 1;
|
||||
//Serial.println("Authenticating using key A...");
|
||||
//Serial.println(F("Authenticating using key A..."));
|
||||
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("PCD_Authenticate() failed: ");
|
||||
Serial.print(F("PCD_Authenticate() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
else Serial.println(F("PCD_Authenticate() success: "));
|
||||
|
||||
// Write block
|
||||
status = mfrc522.MIFARE_Write(block, buffer, 16);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Write() failed: ");
|
||||
Serial.print(F("MIFARE_Write() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
else Serial.println("MIFARE_Write() success: ");
|
||||
else Serial.println(F("MIFARE_Write() success: "));
|
||||
|
||||
block = 2;
|
||||
//Serial.println("Authenticating using key A...");
|
||||
//Serial.println(F("Authenticating using key A..."));
|
||||
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("PCD_Authenticate() failed: ");
|
||||
Serial.print(F("PCD_Authenticate() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
@@ -96,22 +97,22 @@ void loop() {
|
||||
// Write block
|
||||
status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Write() failed: ");
|
||||
Serial.print(F("MIFARE_Write() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
else Serial.println("MIFARE_Write() success: ");
|
||||
else Serial.println(F("MIFARE_Write() success: "));
|
||||
|
||||
// Ask personal data: First name
|
||||
Serial.println("Type First name, ending with #");
|
||||
Serial.println(F("Type First name, ending with #"));
|
||||
len=Serial.readBytesUntil('#', (char *) buffer, 20) ; // read first name from serial
|
||||
for (byte i = len; i < 20; i++) buffer[i] = '\s'; // pad with spaces
|
||||
|
||||
block = 4;
|
||||
//Serial.println("Authenticating using key A...");
|
||||
//Serial.println(F("Authenticating using key A..."));
|
||||
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("PCD_Authenticate() failed: ");
|
||||
Serial.print(F("PCD_Authenticate() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
@@ -119,17 +120,17 @@ void loop() {
|
||||
// Write block
|
||||
status = mfrc522.MIFARE_Write(block, buffer, 16);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Write() failed: ");
|
||||
Serial.print(F("MIFARE_Write() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
else Serial.println("MIFARE_Write() success: ");
|
||||
else Serial.println(F("MIFARE_Write() success: "));
|
||||
|
||||
block = 5;
|
||||
//Serial.println("Authenticating using key A...");
|
||||
//Serial.println(F("Authenticating using key A..."));
|
||||
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("PCD_Authenticate() failed: ");
|
||||
Serial.print(F("PCD_Authenticate() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
@@ -137,11 +138,11 @@ void loop() {
|
||||
// Write block
|
||||
status = mfrc522.MIFARE_Write(block, &buffer[16], 16);
|
||||
if (status != MFRC522::STATUS_OK) {
|
||||
Serial.print("MIFARE_Write() failed: ");
|
||||
Serial.print(F("MIFARE_Write() failed: "));
|
||||
Serial.println(mfrc522.GetStatusCodeName(status));
|
||||
return;
|
||||
}
|
||||
else Serial.println("MIFARE_Write() success: ");
|
||||
else Serial.println(F("MIFARE_Write() success: "));
|
||||
|
||||
|
||||
Serial.println(" ");
|
||||
|
||||
Reference in New Issue
Block a user