RFID doesn't work, even when hardware is working and wiring is correct

Hello everyone! First of all thank you for the help i've been getting from this forum, it really was helpful
Now for the issue itself: The RFID doesn't work or i have to try multiple times in order for the tag to be read. Now, i've tested it the example codes and it works just fine. I really appreciate your help, thanks in advance

#include <Stepper.h> //Líbrería para el motorcito
#include <Keypad.h> //Librería para el Keypad
#include <LiquidCrystal_I2C.h> //Líbrería para la pantalla
#include <MFRC522.h> // Líbreria para el RFID
#include <EasyBuzzer.h> //buzzer 


//incluye las librerías necesarias



#define I2C_ADDR 0x27 //direccion hexadecimal i2c
#define LCD_COLUMNS 20 //columnas lcd
#define LCD_LINES 4 //Lineas del lcd
#define SS_PIN 53 //pin ss del Rfid
#define RST_PIN 48 //pin Reset del RFID
#define DEFAULT_AMOUNT 2
//
//
bool isBlocked;
bool menu;
int lastProduct;
int tiempo;
const int steps = 1000;
const int buzzerPin = 17;  // Define el pin  para el buzzer

unsigned int frequency = 1000;
unsigned int duration = 100;




Stepper motor(steps, A4, A3, A2, A1);
Stepper motor1(steps,A3, A3, A3, A3);


LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int creditos = 0;


MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class


MFRC522::MIFARE_Key key;


// Init array that will store new NUID
byte nuidPICC[4];

byte readCard[4];    
int successRead;

const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 9, 8, 7, 6 };
byte colPins[COLS] = { 5, 4, 3, 2 };


Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


void agregarTag(byte* uid){
 
}


void setup() {


  Serial.begin(9600); // Comienza la comunicación serial


  motor.setSpeed(60);
    motor1.setSpeed(60);


  lcd.init();
  lcd.backlight();
  SPI.begin();
  rfid.PCD_Init();
  Serial.println("Lector de RFID listo");


  pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP); // el eclipse no fue parcial y cegoooo nuestras miradas te vi q llorabas te vi q llorabas x el te para tres
pinMode(16, INPUT_PULLUP);
for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
 


  if ( ! rfid.PICC_IsNewCardPresent())
    return;


  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;
 


  EasyBuzzer.singleBeep(
    frequency,  // Frequency in hertz(HZ).  
    duration  // Duration of the beep in milliseconds(ms).
 
  );


}




void loop() {


  EasyBuzzer.update();


bool CreditosMas = digitalRead(14);
bool CreditosMenos = digitalRead(15);
bool EEPGONExd = digitalRead(16);


if(isBlocked == false){
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Bienvenido!");
  lcd.setCursor(0, 1);
  lcd.print("Sus creditos:");
  lcd.setCursor(14, 1);
  lcd.print(creditos);
  delay(20);
  menu = 1;
}

//Con mi balsa yo me ire a naufragaaaaaaaar

if(CreditosMas == 0){
  creditos++;
  delay(150);
}


if(CreditosMenos == 0){
creditos = creditos - 1;
  delay(150);
} // Y GRACIAS POR VENIIR





  char customKey = customKeypad.getKey();
  if (customKey == 'A') {
      EasyBuzzer.singleBeep(frequency,  duration);
    menu = 0;
    lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Usted eligio:");
        lcd.setCursor(0, 1);
    lcd.print("Producto A");
   
    isBlocked = 1;
    delay(2000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Confirmar con #");
        Serial.print("Cancelar con #");
    lcd.setCursor(0, 1);
    lcd.print("Cancelar con *");
    Serial.print("Cancelar con *");
    lastProduct = 1;
   


  } else if (customKey == 'B') {
      EasyBuzzer.singleBeep(frequency,  duration);
tiempo = millis();
  menu = 0;
    lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Usted eligio:");
        lcd.setCursor(0, 1);
    lcd.print("Producto B");
    isBlocked = 1;
    delay(2000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Confirmar con #");
    Serial.print("Confirmar con #");
    lcd.setCursor(0, 1);
    lcd.print("Cancelar con *");
    Serial.print("Cancelar con *");


    delay(2000);
        lastProduct = 2;
if(tiempo >= 30000){
  menu = 0;
  isBlocked = 0;
}
  }
  if(customKey == '#' && lastProduct == 1 && creditos >= 3){
      EasyBuzzer.singleBeep(frequency,  duration);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Compra aceptada");
    Serial.print("Compra aceptada");
    lcd.setCursor(0, 1);
    lcd.print("Producto 1");
    Serial.print("Producto 1");
      motor.step(10000);
     lastProduct == 0;
     creditos = creditos-3;


  }else if (customKey == '*' && lastProduct == 1 && menu == 0){
      EasyBuzzer.singleBeep(frequency,  duration);
    lcd.clear();
    lcd.print("Compra cancelada.");
    Serial.print("Compra cancelada.");
    delay(2000);
    isBlocked = 0;
     lastProduct == 0;
  }else if(customKey == '#' && lastProduct == 1 && creditos <= 3 && menu == 0){
      EasyBuzzer.singleBeep(frequency,  duration);
  lcd.clear();
      lcd.setCursor(0, 1);
  lcd.print("Creditos insuficientes");
      delay(2000);
     isBlocked = 0;
    lastProduct == 0;
   
  }


  if (customKey == '#' && lastProduct == 2 && creditos >= 3 && menu == 0) {
      EasyBuzzer.singleBeep(frequency,  duration);
    lcd.clear();
       lcd.setCursor(0, 0);
    lcd.print("Compra aceptada");
    Serial.print("Compra aceptada");
    lcd.setCursor(0, 1);
    lcd.print("Producto 2");
    Serial.print("Producto 2");
          motor1.step(steps);
creditos = creditos-3;
    lastProduct == 0;


  }else if(customKey == '#' && lastProduct == 2 && creditos <= 3 && menu == 0){
      EasyBuzzer.singleBeep(frequency,  duration);
  lcd.clear();
  lcd.print("Creditos insuficientes");
      delay(2000);
     isBlocked = 0;
    lastProduct == 0;
  }
if(customKey == '*' && lastProduct == 2 && menu == 0 && menu == 0) {
    EasyBuzzer.singleBeep(frequency,  duration);
    lcd.clear();
    lcd.print("Compra cancelada.");
    Serial.print("Compra cancelada.");
    delay(2000);
    isBlocked = 0;
    lastProduct == 0;
  }


 
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.


  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;



  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));


  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }


  if (rfid.uid.uidByte[0] != nuidPICC[0] ||
    rfid.uid.uidByte[1] != nuidPICC[1] ||
    rfid.uid.uidByte[2] != nuidPICC[2] ||
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    Serial.println(F("A new card has been detected."));
    creditos = creditos + 3;


    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   
    Serial.println(F("The NUID tag is:"));
    Serial.print(F("In hex: "));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
  }
  else Serial.println(F("Card read previously."));


  // Halt PICC
  rfid.PICC_HaltA();


  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();

}



void printHex(byte *buffer, byte bufferSize) { //imprime el UID en Hexadecimal
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

void printDec(byte *buffer, byte bufferSize) {//imprime el UID en Decimal
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

You have a lot of delay() calls in your code which prevents any RFID from happening. You also have

but you assign it from millis() so it needs to be unsigned long

After your program has been running for 30 seconds, millis() will always be greater than 30000. That is not the proper way to track elapsed time.

ive tried everything you told me but it doesnt work

Post your new version. Saying you tried something is not enough for anyone to see what you did and why it mightn't yet work.

a7

Updated version:

#include <Stepper.h> //Líbrería para el motorcito
#include <Keypad.h> //Librería para el Keypad
#include <LiquidCrystal_I2C.h> //Líbrería para la pantalla
#include <MFRC522.h> // Líbreria para el RFID
#include <EEPROM.h>
#include <EasyBuzzer.h> //buzzer 


//incluye las librerías necesarias



#define I2C_ADDR 0x27 //direccion hexadecimal i2c
#define LCD_COLUMNS 20 //columnas lcd
#define LCD_LINES 4 //Lineas del lcd
#define SS_PIN 53 //pin ss del Rfid
#define RST_PIN 48 //pin Reset del RFID
#define DEFAULT_AMOUNT 2
//
//
bool isBlocked;
bool menu;
int lastProduct;
const int steps = 1000;
const int buzzerPin = 17;  // Define el pin  para el buzzer
byte num_rfid = EEPROM.read(0);


unsigned int frequency = 1000;
unsigned int duration = 100;




Stepper motor(steps, A4, A3, A2, A1);
Stepper motor1(steps,A3, A3, A3, A3);


LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int creditos = 0;


MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class


MFRC522::MIFARE_Key key;


// Init array that will store new NUID
byte nuidPICC[4];

byte readCard[4];    
int successRead;

const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 9, 8, 7, 6 };
byte colPins[COLS] = { 5, 4, 3, 2 };


Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


void agregarTag(byte* uid){
 
}


void setup() {


  Serial.begin(9600); // Comienza la comunicación serial


  motor.setSpeed(60);
    motor1.setSpeed(60);


  lcd.init();
  lcd.backlight();
  SPI.begin();
  rfid.PCD_Init();
  Serial.println("Lector de RFID listo");


  pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP); // el eclipse no fue parcial y cegoooo nuestras miradas te vi q llorabas te vi q llorabas x el te para tres
pinMode(16, INPUT_PULLUP);
for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
 


  if ( ! rfid.PICC_IsNewCardPresent())
    return;


  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;
 


  EasyBuzzer.singleBeep(
    frequency,  // Frequency in hertz(HZ).  
    duration  // Duration of the beep in milliseconds(ms).
 
  );


}




void loop() {


  EasyBuzzer.update();


bool CreditosMas = digitalRead(14);
bool CreditosMenos = digitalRead(15);
bool EEPGONExd = digitalRead(16);


if(isBlocked == false){
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Bienvenido!");
  lcd.setCursor(0, 1);
  lcd.print("Sus creditos:");
  lcd.setCursor(14, 1);
  lcd.print(creditos);
  menu = 1;
}

//Con mi balsa yo me ire a naufragaaaaaaaar
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    int uidIndex = findUIDIndex();
    if (uidIndex != -1) {
      creditos++;
    }
  }
if(CreditosMas == 0){
  creditos++;
}


if(CreditosMenos == 0){
creditos = creditos - 1;
} // Y GRACIAS POR VENIIR


if(EEPGONExd == 0){
    for (int i = 0 ; i < EEPROM.length() ; i++) {
    EEPROM.write(i, 0);
  }


}


  char customKey = customKeypad.getKey();
  if (customKey == 'A') {
      EasyBuzzer.singleBeep(frequency,  duration);
    menu = 0;
    lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Usted eligio:");
        lcd.setCursor(0, 1);
    lcd.print("Producto A");
   
    isBlocked = 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Confirmar con #");
        Serial.print("Cancelar con #");
    lcd.setCursor(0, 1);
    lcd.print("Cancelar con *");
    Serial.print("Cancelar con *");
    lastProduct = 1;
   


  } else if (customKey == 'B') {
      EasyBuzzer.singleBeep(frequency,  duration);
  menu = 0;
    lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Usted eligio:");
        lcd.setCursor(0, 1);
    lcd.print("Producto B");
    isBlocked = 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Confirmar con #");
    Serial.print("Confirmar con #");
    lcd.setCursor(0, 1);
    lcd.print("Cancelar con *");
    Serial.print("Cancelar con *");


        lastProduct = 2;

  }
  if(customKey == '#' && lastProduct == 1 && creditos >= 3){
      EasyBuzzer.singleBeep(frequency,  duration);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Compra aceptada");
    Serial.print("Compra aceptada");
    lcd.setCursor(0, 1);
    lcd.print("Producto 1");
    Serial.print("Producto 1");
      motor.step(10000);
     lastProduct == 0;
     creditos = creditos-3;


  }else if (customKey == '*' && lastProduct == 1 && menu == 0){
      EasyBuzzer.singleBeep(frequency,  duration);
    lcd.clear();
    lcd.print("Compra cancelada.");
    Serial.print("Compra cancelada.");
    isBlocked = 0;
     lastProduct == 0;
  }else if(customKey == '#' && lastProduct == 1 && creditos <= 3 && menu == 0){
      EasyBuzzer.singleBeep(frequency,  duration);
  lcd.clear();
      lcd.setCursor(0, 1);
  lcd.print("Creditos insuficientes");
     isBlocked = 0;
    lastProduct == 0;
   
  }


  if (customKey == '#' && lastProduct == 2 && creditos >= 3 && menu == 0) {
      EasyBuzzer.singleBeep(frequency,  duration);
    lcd.clear();
       lcd.setCursor(0, 0);
    lcd.print("Compra aceptada");
    Serial.print("Compra aceptada");
    lcd.setCursor(0, 1);
    lcd.print("Producto 2");
    Serial.print("Producto 2");
          motor1.step(steps);
creditos = creditos-3;
    lastProduct == 0;


  }else if(customKey == '#' && lastProduct == 2 && creditos <= 3 && menu == 0){
      EasyBuzzer.singleBeep(frequency,  duration);
  lcd.clear();
  lcd.print("Creditos insuficientes");
     isBlocked = 0;
    lastProduct == 0;
  }
if(customKey == '*' && lastProduct == 2 && menu == 0 && menu == 0) {
    EasyBuzzer.singleBeep(frequency,  duration);
    lcd.clear();
    lcd.print("Compra cancelada.");
    Serial.print("Compra cancelada.");
    isBlocked = 0;
    lastProduct == 0;
  }


 
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.


  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;



  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));


  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }


  if (rfid.uid.uidByte[0] != nuidPICC[0] ||
    rfid.uid.uidByte[1] != nuidPICC[1] ||
    rfid.uid.uidByte[2] != nuidPICC[2] ||
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    Serial.println(F("A new card has been detected."));
    creditos = creditos + 3;


    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   
    Serial.println(F("The NUID tag is:"));
    Serial.print(F("In hex: "));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
  }
  else Serial.println(F("Card read previously."));


  // Halt PICC
  rfid.PICC_HaltA();


  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();

  for (int i = 0; i < rfid.uid.size; i++) {  //imprime eemprom cada vez que se lee rfid (por testeo principalmente)
    readCard[i] = rfid.uid.uidByte[i]; 
        EEPROM.write((rfid.uid.size*num_rfid), readCard[i] );       
    Serial.print(readCard[i], HEX);         
  }
  num_rfid++;  //añadimos 1 al contador en la EEPROM
EEPROM.write(0,num_rfid);   

}




int findUIDIndex() { //Funcion utilizada para buscar el UID en la EEPROM
  for (int i = 0; i < EEPROM.length(); i += 5) {
    if (EEPROM.read(i) == rfid.uid.uidByte[0] && EEPROM.read(i + 1) == rfid.uid.uidByte[1] && EEPROM.read(i + 2) == rfid.uid.uidByte[2] && EEPROM.read(i + 3) == rfid.uid.uidByte[3]) {
      return i;
    }
  }
  return -1;
}


void printHex(byte *buffer, byte bufferSize) { //imprime el UID en Hexadecimal
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

void printDec(byte *buffer, byte bufferSize) {//imprime el UID en Decimal
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

Changes:
deleted all delays(weren't really necesary) and that millis thing which i did not use

Hello Mtrix

I assume that you have written the programme by yourself, then it is quite easy to find the error.

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code as diagnostic prints to see the values of variables and determine whether they meet your expectations.

Have a nice day and enjoy coding in C++.

yeah your suggestion did work
thanks a lot for your help
have a nice day

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.