Funzionamento e Codice

Per svolgere il nostro progetto abbiamo utilizzato un codice abbastanza intuitivo

Ecco la seguente spiegazione del nostro codice:

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

#define SS_PIN 10
#define RST_PIN 9

// Correzione del formato UID (senza spazi e con caratteri esadecimali)
String UID = "3B71727"; // UID senza spazi
byte lock = 0;

Questo codice configura un lettore RFID MFRC522 con un display LCD I2C. Include le librerie necessarie per la comunicazione SPI con il lettore RFID e per controllare il display LCD. I pin 10 e 9 sono configurati rispettivamente come SS (selezione chip) e RST (reset) per il modulo RFID. L’UID di una tessera RFID è memorizzato come stringa “3B71727” e la variabile lock

Servo servo;  
LiquidCrystal_I2C lcd(0x27, 16, 2);  
MFRC522 rfid(SS_PIN, RST_PIN);  

void setup() {
  Serial.begin(9600);  

  servo.attach(3);  
  servo.write(70); 

  lcd.init();
  lcd.backlight(); 

  SPI.begin();
  rfid.PCD_Init();
}

void loop() {

  lcd.setCursor(4, 0);
  lcd.print("Welcome!");
  lcd.setCursor(1, 1);
  lcd.print("Put your card");

Il codice inizializza un servo, un display LCD e un modulo RFID. Nel setup(), viene configurata la comunicazione seriale, il servo viene collegato al pin 3 e impostato alla posizione di chiusura (70 gradi), il display LCD viene attivato e il modulo RFID viene inizializzato. Nel loop(), viene mostrato un messaggio di benvenuto sul display che invita l’utente ad avvicinare una tessera RFID.

  if (!rfid.PICC_IsNewCardPresent()) {
    return; 
  }

  if (!rfid.PICC_ReadCardSerial()) {
    return;  
  }

  lcd.clear();  
  lcd.setCursor(0, 0);
  lcd.print("Scanning");
  Serial.print("NUID tag is :");

  String scannedUID = "";  

  for (byte i = 0; i < rfid.uid.size; i++) {
    lcd.print(".");  

    scannedUID.concat(String(rfid.uid.uidByte[i], HEX));  

    delay(300);  

  }
  scannedUID.toUpperCase();  
  
  if (scannedUID == UID && lock == 0) {
    servo.write(160);  

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Door is open");
    delay(1500);  

    lcd.clear();
    lock = 1;  

  }
  else if (scannedUID == UID && lock == 1) {
    servo.write(70);  

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Door is locked");
    delay(1500);  

    lcd.clear();
    lock = 0;   

  }
  else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Wrong card!");  

    delay(1500);  
    lcd.clear();
  }
}

Il codice legge l’UID di una tessera RFID. Se non c’è una tessera o non riesce a leggere la seriale, esce dalla funzione. Se l’UID corrisponde a quello predefinito e il lucchetto è sbloccato, il servo apre la porta e il display mostra “Door is open”. Se il lucchetto è già bloccato, il servo chiude la porta e il display mostra “Door is lock”. Se l’UID non corrisponde, viene visualizzato “Carta sbagliata!”. Il lucchetto alterna tra stato sbloccato e bloccato ad ogni lettura corretta.

Ecco il codice completo che abbiamo utilizzato:

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

#define SS_PIN 10
#define RST_PIN 9

// Correzione del formato UID (senza spazi e con caratteri esadecimali)
String UID = "3B71727"; // UID senza spazi
byte lock = 0;

Servo servo;  // Creazione dell'oggetto servo
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Creazione dell'oggetto LCD
MFRC522 rfid(SS_PIN, RST_PIN);  // Creazione dell'oggetto RFID

void setup() {
  Serial.begin(9600);  // Inizializza la comunicazione seriale

  // Inizializzazione del servo
  servo.attach(3);  // Collega il servo al pin 3
  servo.write(70);  // Imposta la posizione iniziale del servo (porta chiusa)

  // Inizializzazione del display LCD
  lcd.init();
  lcd.backlight();  // Attiva la retroilluminazione del display

  // Inizializzazione del modulo RFID e della comunicazione SPI
  SPI.begin();
  rfid.PCD_Init();
}

void loop() {
  // Mostra il messaggio di benvenuto sul display LCD
  lcd.setCursor(4, 0);
  lcd.print("Welcome!");
  lcd.setCursor(1, 1);
  lcd.print("Put your card");

  // Controlla se è presente una nuova carta RFID
  if (!rfid.PICC_IsNewCardPresent()) {
    return;  // Esci dalla funzione se non c'è nessuna carta
  }

  // Legge il numero seriale della carta RFID
  if (!rfid.PICC_ReadCardSerial()) {
    return;  // Esci dalla funzione se non riesce a leggere il seriale
  }

  lcd.clear();  // Pulisce il display LCD
  lcd.setCursor(0, 0);
  lcd.print("Scanning");
  Serial.print("NUID tag is :");

  String scannedUID = "";  // Variabile per memorizzare l'UID letto dalla carta
  for (byte i = 0; i < rfid.uid.size; i++) {
    lcd.print(".");  // Mostra un punto per ogni byte letto
    scannedUID.concat(String(rfid.uid.uidByte[i], HEX));  // Aggiunge ogni byte all'UID
    delay(300);  // Aspetta un po' prima di leggere il prossimo byte
  }
  scannedUID.toUpperCase();  // Converte l'UID in maiuscolo per un confronto coerente

  // Confronta l'UID letto con quello predefinito
  if (scannedUID == UID && lock == 0) {
    servo.write(160);  // Sblocca la porta (servo apre la porta)
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Door is open");
    delay(1500);  // Aspetta 1.5 secondi prima di continuare
    lcd.clear();
    lock = 1;  // Imposta lo stato del lucchetto a "bloccato"
  }
  else if (scannedUID == UID && lock == 1) {
    servo.write(70);  // Blocca la porta (servo chiude la porta)
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Door is locked");
    delay(1500);  // Aspetta 1.5 secondi prima di continuare
    lcd.clear();
    lock = 0;  // Imposta lo stato del lucchetto a "sbloccato"
  }
  else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Wrong card!");  // Mostra un messaggio di errore se l'UID non corrisponde
    delay(1500);  // Aspetta 1.5 secondi prima di continuare
    lcd.clear();
  }
}

Translate »
We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners. View more
Cookies settings
Accept
Decline
Privacy & Cookie policy
Privacy & Cookies policy
Cookie name Active

Comments

When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection.

An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your comment, your profile picture is visible to the public in the context of your comment.

Media

If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.

Cookies

If you leave a comment on our site you may opt-in to saving your name, email address and website in cookies. These are for your convenience so that you do not have to fill in your details again when you leave another comment. These cookies will last for one year.

If you visit our login page, we will set a temporary cookie to determine if your browser accepts cookies. This cookie contains no personal data and is discarded when you close your browser.

When you log in, we will also set up several cookies to save your login information and your screen display choices. Login cookies last for two days, and screen options cookies last for a year. If you select "Remember Me", your login will persist for two weeks. If you log out of your account, the login cookies will be removed.

If you edit or publish an article, an additional cookie will be saved in your browser. This cookie includes no personal data and simply indicates the post ID of the article you just edited. It expires after 1 day.

Embedded content from other websites

Articles on this site may include embedded content (e.g. videos, images, articles, etc.). Embedded content from other websites behaves in the exact same way as if the visitor has visited the other website.

These websites may collect data about you, use cookies, embed additional third-party tracking, and monitor your interaction with that embedded content, including tracking your interaction with the embedded content if you have an account and are logged in to that website.

Who we share your data with

 If you request a password reset, your IP address will be included in the reset email.

How long we retain your data

If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognize and approve any follow-up comments automatically instead of holding them in a moderation queue.

For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.

What rights you have over your data

 If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.

Where your data is sent

 Visitor comments may be checked through an automated spam detection service.

Save settings
Cookies settings