Reading and writing in RFID card

Hi guys new guy here.

i get error in this code "PCD_Authenticate() failed: Timeout in communication."

i've been working this for months now. can anyone help me please

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
//*****************************************************************************************//
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
for (byte i = 0; i < 6; i++) key.keyByte *= 0xFF;*
*}*
_//*****************************************************************************************//_
*//////////////////////////*
*//////// VARIABLES ///////*
*//////////////////////////*
*int block = 1;*
*byte blockcontent[18];*
*String puta;*
*byte readbackblock[18];*
*/////////////////////////////////////////////////////////////////////////////////////////////////*
*void loop() {*
* ///////////////////////////////////////////////////////////////////////////////////*
* // Look for new cards*
* if ( !mfrc522.PICC_IsNewCardPresent()) {*
* return;*
* }*
* if ( !mfrc522.PICC_ReadCardSerial()) {*
* return;*
* }*
* ////////////////////////////////*
* //////// PRINT THE ID //////////*
* ////////////////////////////////*
* readBlock(block, readbackblock);*
* mfrc522.PICC_HaltA(); // Halt PICC*
* mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD*
* mfrc522.PCD_Reset();*
* delay(100);*
* mfrc522.PCD_Init(); // Init MFRC522*
* for (int j = 0 ; j < 16 ; j++) //print the block contents*
* {*
* Serial.write (readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor*
* }*
* ///////////////////////////////////////////////////////////////////////////////////////////*
* // Check if Card was removed*
* bool cardRemoved = false;*
* int counter = 0;*
* bool current, previous;*
* previous = !mfrc522.PICC_IsNewCardPresent();*
* while (!cardRemoved) {*
* current = !mfrc522.PICC_IsNewCardPresent();*
* if (current && previous) counter++;*
* previous = current;*
* cardRemoved = (counter > 2);*
* delay(50);*
* int x = 0;*
* while (Serial.available()) {*
* String puta = Serial.readString();*
* puta.toCharArray(blockcontent, 14);*
* writeBlock(block, blockcontent);*
* }*
* }*
* Serial.println("Card was removed");*
* delay(500); //change value if you want to read cards faster*
* mfrc522.PICC_HaltA(); // Halt PICC*
* mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD*
* mfrc522.PCD_Reset();*
* delay(100);*
* mfrc522.PCD_Init(); // Init MFRC522*
*}*

*int writeBlock(int blockNumber, byte arrayAddress[])* 
*{*
* //this makes sure that we only write into data blocks. Every 4th block is a trailer block for the access/security info.*
_ int largestModulo4Number=blockNumber/4*4;_
* int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector*
* if (blockNumber > 2 && (blockNumber+1)%4 == 0){Serial.print(blockNumber);Serial.println(" is a trailer block:");return 2;}//block number is a trailer block (modulo 4); quit and send error code 2*

_ /*****************************************authentication of the desired block for access***********************************************************/_
* byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));*
<em> //byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);</em>
* //this method is used to authenticate a certain block for writing or reading*
* //command: See enumerations above -> PICC_CMD_MF_AUTH_KEY_A = 0x60 (=1100000), // this command performs authentication with Key A*
* //blockAddr is the number of the block from 0 to 15.*
<em> //MIFARE_Key *key is a pointer to the MIFARE_Key struct defined above, this struct needs to be defined for each block. New cards have all A/B= FF FF FF FF FF FF</em>
_ //Uid *uid is a pointer to the UID struct that contains the user ID of the card._
* if (status != MFRC522::STATUS_OK) {*
* Serial.println();*
* Serial.print("PCD_Authenticate() failed: ");*
* Serial.println(mfrc522.GetStatusCodeName(status));*
* return 3;//return "3" as error message*
* }*
* //it appears the authentication needs to be made before every block read/write within a specific sector.*
* //If a different sector is being authenticated access to the previous one is lost.*
_ /*****************************************writing the block***********************************************************/_

* status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);//valueBlockA is the block number, MIFARE_Write(block number (0-15), byte array containing 16 values, number of bytes in block (=16))*
* //status = mfrc522.MIFARE_Write(9, value1Block, 16);*
* if (status != MFRC522::STATUS_OK) {*
* Serial.print("MIFARE_Write() failed: ");*
* Serial.println(mfrc522.GetStatusCodeName(status));*
* return 4;//return "4" as error message*
* }*
* Serial.println("block was written");*
*}*
*int readBlock(int blockNumber, byte arrayAddress[])* 
*{*
_ int largestModulo4Number=blockNumber/4*4;_
* int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector*
_ /*****************************************authentication of the desired block for access***********************************************************/_
* byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));*
<em> //byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);</em>
* //this method is used to authenticate a certain block for writing or reading*
* //command: See enumerations above -> PICC_CMD_MF_AUTH_KEY_A = 0x60 (=1100000), // this command performs authentication with Key A*
* //blockAddr is the number of the block from 0 to 15.*
<em> //MIFARE_Key *key is a pointer to the MIFARE_Key struct defined above, this struct needs to be defined for each block. New cards have all A/B= FF FF FF FF FF FF</em>
_ //Uid *uid is a pointer to the UID struct that contains the user ID of the card._
* if (status != MFRC522::STATUS_OK) {*
* Serial.print("PCD_Authenticate() failed (read): ");*
* Serial.println(mfrc522.GetStatusCodeName(status));*
* return 3;//return "3" as error message*
* }*
* //it appears the authentication needs to be made before every block read/write within a specific sector.*
* //If a different sector is being authenticated access to the previous one is lost.*
_ /*****************************************reading a block***********************************************************/_

* byte buffersize = 18;//we need to define a variable with the read buffer size, since the MIFARE_Read method below needs a pointer to the variable that contains the size...* 
* status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize);//&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number*
* if (status != MFRC522::STATUS_OK) {*
* Serial.print("MIFARE_read() failed: ");*
* Serial.println(mfrc522.GetStatusCodeName(status));*
* return 4;//return "4" as error message*
* }*
* Serial.println("block was read");*
*}*
*````````*
*PLEASE HELP*

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

i get error in this code "PCD_Authenticate() failed: Timeout in communication."

Then this suggests that you are trying to access a card where the keys are not set to FFFFFFFFFFFFh or that you are trying to write over the first sector that contains the UID and other fixed data.

I can't tell because you code is a mess and the forum is mangling it and taking some of your code as an instruction to switch into italics. This is why we need you to post inside code tags to prevent this and also supply a scrolling window.