RFID RC522 / lcd does not change

Hello, I have this code that verifies if the card that the sensor scans is registered or not and notifies it through an lcd and a led color. When I scan card 1 or 2, the green led turns on and the red one turns off as it should, but the display remains "esperando autorizacion..." any ideas? Thanks.

#include <MFRC522.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Keypad.h>

#define SS 10
#define RST 9
MFRC522 lector(SS, RST);
int led_abierto = A0;
int led_cerrado = A1;
unsigned long tiempoUltimaLectura = 0;
unsigned long tiempoEspera = 5000;
byte usuario1[4] = { 0x52, 0x81, 0x56, 0xD3 };
byte usuario2[4] = { 0xA2, 0x88, 0x68, 0x1B };
byte tarjeta_leida[4];
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void resetearValores() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Esperando");
  lcd.setCursor(0, 1);
  lcd.print("autorizacion...");
  digitalWrite(led_cerrado, HIGH);
  digitalWrite(led_abierto, LOW);
}

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  while (!Serial);
  SPI.begin();
  lector.PCD_Init();
  resetearValores();
}

void loop() {
  // Verificar si hay una nueva tarjeta presente
  if (lector.PICC_IsNewCardPresent()) {
    // Leer el UID de la tarjeta
    if (lector.PICC_ReadCardSerial()) {
      tiempoUltimaLectura = millis();  // Actualizar el tiempo de última lectura

      for (byte i = 0; i < lector.uid.size; i++) {
        tarjeta_leida[i] = lector.uid.uidByte[i];
      }
      if (compararUID(tarjeta_leida, usuario1)) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Bienvenido!");
        lcd.setCursor(0, 1);
        lcd.print("Usuario 1.");
        digitalWrite(led_abierto, HIGH);
        digitalWrite(led_cerrado, LOW);
      } else if (compararUID(tarjeta_leida, usuario2)) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Bienvenido!");
        lcd.setCursor(0, 1);
        lcd.print("Usuario 2.");
        digitalWrite(led_abierto, HIGH);
        digitalWrite(led_cerrado, LOW);
      } else {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Acceso denegado!");
        lcd.setCursor(0, 1);
        lcd.print("Tarjeta invalida.");
        digitalWrite(led_abierto, LOW);
        digitalWrite(led_cerrado, HIGH);
      }
    }
  }
  lector.PICC_HaltA();  // Detener comunicación con la tarjeta
  // Verificar si han pasado los 3 segundos desde la última lectura exitosa
  if (millis() - tiempoUltimaLectura >= tiempoEspera) {
    resetearValores();
    tiempoUltimaLectura = millis();  // Reiniciar el tiempo de última lectura
  }
  // Verificar si han pasado los 3 segundos desde la última lectura exitosa
}
boolean compararUID(byte* tarjeta1, byte* tarjeta2) {
  for (int i = 0; i < 4; i++) {
    if (tarjeta1[i] != tarjeta2[i]) {
      return false;
    }
  }
  return true;
}

You call resetearValores() at the end of every loop...

if (millis() - tiempoUltimaLectura >= tiempoEspera) {
    resetearValores();

Yes, but when I read a card the 5-second counter begins to call resetearValores() and simultaneously if the card read matches the one registered, either user1 / user2 enters. because irl the led changes color, but the display remains static...

If you make the LEDs in void resetearValores() be HIGH/HIGH or LOW/LOW, you will see when void resetearValores() is called, versus the main code (HIGH/LOW, LOW/HIGH).

When do you want void resetearValores() called, and when do you NOT want void resetearValores() called? It seems to be called around five seconds, regardless of pass/fail of the cards.

I want that after 5 seconds it enters the if(compareUID(card_read, user1)) / else if (compareUID(card_read, user2)) / else
is called when resetting values

> if (millis() - lastReadTime >= waitTime) {
>       resetValues();
>       lastReadTime = milliseconds();
>     }

but if I put it inside each if the function is never called

Is this what you mean by "if I put it inside each if the function is never called"?

if (compareUID(card_red, user1))...
.
(waitTime 5 seconds)
.
if (compareUID(card_read, user2))...

If both of these conditions fail, the card is neither user1 nor user2.

change the code to this:

#include <MFRC522.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Keypad.h>

#define SS 10
#define RST 9
MFRC522 lector(SS, RST);

int led_abierto = A0;
int led_cerrado = A1;

unsigned long tiempoUltimaLectura = 0;
unsigned long tiempoEspera = 5000;

byte usuario1[4] = { 0x52, 0x81, 0x56, 0xD3 };
byte usuario2[4] = { 0xA2, 0x88, 0x68, 0x1B };
byte tarjeta_leida[4];

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int posicion = 0;
int cursor = 5;
int clave = 0;
const byte Filas = 4;
const byte Cols = 4;
byte Pins_Filas[] = { 9, 8, 7, 6 };
byte Pins_Cols[] = { 5, 4, 3, 2 };
char Teclas[Filas][Cols] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
char contra[4] = { '2', '2', '5', '5' };
Keypad Teclado1 = Keypad(makeKeymap(Teclas), Pins_Filas, Pins_Cols, Filas, Cols);

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  while (!Serial);
  SPI.begin();
  lector.PCD_Init();
  digitalWrite(led_cerrado, HIGH);
  digitalWrite(led_abierto, LOW);
}

void loop() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Esperando");
  lcd.setCursor(0, 1);
  lcd.print("autorizacion...");

  if (lector.PICC_IsNewCardPresent()) {
  if (lector.PICC_ReadCardSerial()) {
      Serial.print("UID: ");
      for (byte i = 0; i < lector.uid.size; i++) {
        if (lector.uid.uidByte[i] < 0x10) {
          Serial.print(" 0");
        } else {
          Serial.print(" ");
        }
        Serial.print(lector.uid.uidByte[i], HEX);
        tarjeta_leida[i] = lector.uid.uidByte[i];
      }
      Serial.println();
    }
    
    if (compararUID(tarjeta_leida, usuario1)) {
      lcd.setCursor(0, 0);
      lcd.print("Bienvenido!");
      lcd.setCursor(0, 1);
      lcd.print("Usuario 1.");
      digitalWrite(led_abierto, HIGH);
      digitalWrite(led_cerrado, LOW);
      tiempoUltimaLectura = millis();
      Serial.print("TEST");
    } else if (compararUID(tarjeta_leida, usuario2)) {
      lcd.setCursor(0, 0);
      lcd.print("Bienvenido!");
      lcd.setCursor(0, 1);
      lcd.print("Usuario 2.");
      digitalWrite(led_abierto, HIGH);
      digitalWrite(led_cerrado, LOW);
      tiempoUltimaLectura = millis();
      Serial.print("TEST");
    } else {
      lcd.setCursor(0, 0);
      lcd.print("Acceso denegado!");
      lcd.setCursor(0, 1);
      lcd.print("Tarjeta invalida.");
      digitalWrite(led_abierto, LOW);
      digitalWrite(led_cerrado, HIGH);
      tiempoUltimaLectura = millis();
      Serial.print("TEST");
    }
  }
  lector.PICC_HaltA();
  
  if (millis() - tiempoUltimaLectura >= tiempoEspera) {
    loop();
    tiempoUltimaLectura = millis();
  }

  char pulsacion = Teclado1.getKey();
  if (pulsacion != 0) {
    if (pulsacion != '#' && pulsacion != '*' && clave == 0) {
      lcd.print(pulsacion);
      cursor++;
    }
  }

  if (pulsacion == '*') {
    posicion = 0;
    cursor = 5;
    clave = 0;
    posicion = 0;
    lcd.setCursor(0, 0);
    lcd.print("Introduzca clave");
    lcd.setCursor(5, 1);
    lcd.print(" ");
    lcd.setCursor(5, 1);
    digitalWrite(led_cerrado, HIGH);
    digitalWrite(led_abierto, LOW);
  }

  if (pulsacion == contra[posicion]) {
    posicion++;
  }
  
  if (posicion == 4) {
    lcd.setCursor(0, 0);
    lcd.print("Acceso autorizado!");
    lcd.setCursor(0, 1);
    lcd.print("Bienvenido");
    clave = 1;
    digitalWrite(led_abierto, HIGH);
    digitalWrite(led_cerrado, LOW);
  }
  
  if (cursor > 8) {
    cursor = 5;
    posicion = 0;
    lcd.setCursor(5, 1);
    lcd.print(" ");
    lcd.setCursor(5, 1);
    if (clave == 0) {
      digitalWrite(led_cerrado, HIGH);
      lcd.setCursor(0, 0);
      lcd.print("Clave incorrecta.");
    }
  }
}

boolean compararUID(byte* tarjeta1, byte* tarjeta2) {
  for (int i = 0; i < lector.uid.size; i++) {
    if (tarjeta1[i] != tarjeta2[i]) {
      return false;
    }
  }
  return true;
}

When the reader reads a card, it makes the if comparison and if the led changes color, IN THEORY THE LCD should change and print "TEST" on the serial port, in all 3 cases everything is true except for the lcd and then after 5 seconds the led returns to the initial color

Your LEDs only change when Tarjeta invalida

Is this correct? (or posicion) If the LCD cursor is in position 8 then "Clave incorrecta"?

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