#include <Wire.h>
#include <SPI.h>
#include <MFRC522.h>
// ==========================================
// CONFIGURACIÓN LCD AiP31068 (De tu archivo)
// ==========================================
#define LCD_I2C_ADDR 0x3E
#define LCD_MODE_COMMAND 0x00
#define LCD_MODE_DATA 0x40
#define LCD_CLEARDISPLAY 0x01
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_FUNCTIONSET 0x20
#define LCD_SETDDRAMADDR 0x80
#define LCD_2LINE 0x08
#define LCD_5x8DOTS 0x00
#define LCD_DISPLAYON 0x04
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTDEC 0x00
// ==========================================
// CONFIGURACIÓN RFID MFRC522 (Para Arduino Mega)
// ==========================================
#define RST_PIN 5 // Pin de reset para el RFID
#define SS_PIN 53 // Pin de selección (SDA/SS) en Arduino Mega
MFRC522 mfrc522(SS_PIN, RST_PIN); // Instancia del lector RFID
// ==========================================
// FUNCIONES DEL LCD (Tu archivo de referencia)
// ==========================================
void lcdSend(unsigned char value, unsigned char mode){
Wire.beginTransmission(LCD_I2C_ADDR);
Wire.write(mode); // Control byte: determina si es comando o dato
Wire.write(value); // El valor real
Wire.endTransmission();
delayMicroseconds(50); // Pequeña pausa para que el LCD procese
}
void lcdSetup(){
Wire.begin(); // Inicializa bus I2C (Pines 20 y 21 en Mega por defecto)
// 2 líneas, fuente 5x8
lcdSend(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS, LCD_MODE_COMMAND);
// Encender pantalla
lcdSend(LCD_DISPLAYCONTROL | LCD_DISPLAYON, LCD_MODE_COMMAND);
// Limpiar pantalla
lcdClear();
// Modo de entrada: escribir de izquierda a derecha
lcdSend(LCD_ENTRYMODESET | LCD_ENTRYLEFT | LCD_ENTRYSHIFTDEC, LCD_MODE_COMMAND);
}
void lcdClear(){
lcdSend(LCD_CLEARDISPLAY, LCD_MODE_COMMAND);
}
void lcdSetCursor(int x, int y){
int offsets[] = {0x00, 0x40, 0x14, 0x54};
lcdSend(LCD_SETDDRAMADDR | (x + offsets[y]), LCD_MODE_COMMAND);
}
void lcdPrint(const char * str){
while (*str) {
lcdSend(*str++, LCD_MODE_DATA);
}
}
// En lugar de enviar un string completo, esta función nos ayuda
// a imprimir números en formato Hexadecimal en tu LCD
void lcdPrintHex(byte valor) {
char buf[3];
sprintf(buf, "%02X", valor);
lcdPrint(buf);
}
// ==========================================
// FLUJO PRINCIPAL DE ARDUINO
// ==========================================
void setup() {
Serial.begin(115200); // Mantenemos tu velocidad de serial
lcdSetup(); // Inicializa tu pantalla AiP31068
SPI.begin(); // Inicializa el bus SPI para el RFID
mfrc522.PCD_Init(); // Inicializa el lector RFID MFRC522
// Mensaje inicial en la pantalla
mostrarMensajeInicio();
}
void loop() {
// 1. Busca si hay tarjetas nuevas frente al lector
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// 2. Selecciona la tarjeta para leer su contenido
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// --- ¡TARJETA DETECTADA! ---
lcdClear();
lcdSetCursor(0, 0);
lcdPrint(" Tarjeta Leida!");
lcdSetCursor(0, 1);
lcdPrint("ID: ");
// Imprime el UID (Identificador Único) en tu pantalla usando formato HEX
for (byte i = 0; i < mfrc522.uid.size; i++) {
lcdPrintHex(mfrc522.uid.uidByte[i]);
if(i < mfrc522.uid.size - 1) {
lcdPrint(" "); // Espacio entre los bytes del ID
}
}
// Termina la lectura actual
mfrc522.PICC_HaltA();
// Muestra el mensaje por 3 segundos antes de volver a la pantalla de espera
delay(3000);
mostrarMensajeInicio();
}
// Función auxiliar para no repetir código de la pantalla de inicio
void mostrarMensajeInicio() {
lcdClear();
lcdSetCursor(0, 0);
lcdPrint(" Lector Listo ");
lcdSetCursor(0, 1);
lcdPrint("Acerque Tarjeta ");
}Loading
mfrc522
mfrc522