Vi riposto il codice:
- Codice: Seleziona tutto
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define SS_PIN 10
#define RST_PIN 9
#define LED_DENIED_PIN 7
#define LED_ACCESS_PIN 6
#define LED_AUTORIZED_PIN 4
#define rele 2
#define MAXSCHEDE 8
LiquidCrystal_I2C lcd(0x27,16,2);
MFRC522 mfrc522(SS_PIN, RST_PIN);
int code[MAXSCHEDE][7] = {{xx,xx,xx,xx},
{xx,xx,xx,xx},
{xx,xx,xx,xx},
{xx,xx,xx,xx},
{xx,xx,xx,xx},
{xx,xx,xx,xx},
{xx,xx,xx,xx},
{xx,xx,xx,xx}};
int codeRead = 0;
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Arduino RFID reading UID");
pinMode( LED_DENIED_PIN , OUTPUT);
pinMode( LED_ACCESS_PIN , OUTPUT);
pinMode( LED_AUTORIZED_PIN , OUTPUT);
digitalWrite (rele, HIGH);
pinMode(rele, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("IT Benvenuto");
Serial.println("IT Benvenuto");
lcd.setCursor(0,1);
lcd.print("ES Bienvenidos");
Serial.println("ES Bienvenidos");
}
void loop() {
if ( mfrc522.PICC_IsNewCardPresent())
{
if ( mfrc522.PICC_ReadCardSerial())
{
lcd.clear();
Serial.print("Tag UID:");
lcd.setCursor(0,0);
lcd.print("Tag UID:");
lcd.setCursor(0,1);
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x27 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
lcd.print(mfrc522.uid.uidByte[i] < 0x27 ? " 0" : " ");
lcd.print(mfrc522.uid.uidByte[i], HEX);
// lcd.print(" ");
}
int i = 0;
boolean match = false;
for(int sch=0;sch<MAXSCHEDE&&!match;sch++)
{
int i=0;
match = true;
while(i<mfrc522.uid.size)
{
if(!(int(mfrc522.uid.uidByte[i]) == int(code[sch][i])))
{
match = false;
}
i++;
}
}
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
if(match)
{
digitalWrite( LED_ACCESS_PIN , HIGH);
lcd.print("Accesso Valido");
lcd.setCursor(0,1);
lcd.print("Porta Aperta");
Serial.println("\Accesso Valido");
digitalWrite( LED_AUTORIZED_PIN, HIGH);
digitalWrite (rele, LOW);
delay (1000);
digitalWrite (rele, HIGH);
digitalWrite( LED_AUTORIZED_PIN, LOW);
}else{
digitalWrite( LED_DENIED_PIN , HIGH);
lcd.print("Carta Errata");
lcd.setCursor(0,1);
lcd.print("Accesso Negato");
Serial.println("\Carta Errata");
}
Serial.println("============================");
mfrc522.PICC_HaltA();
delay(1000);
reset_state();
}
}
}
void reset_state()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("IT Benvenuto");
Serial.println("IT Benvenuto");
lcd.setCursor(0,1);
lcd.print("ES Bienvenidos");
Serial.println("ES Bienvenidos");
digitalWrite( LED_ACCESS_PIN , LOW);
digitalWrite( LED_DENIED_PIN , LOW);
}