Hi,
I made the code for the program of the safe lock with the help of people from this forum. But I want to add the information, which display during the skanning the tag or closing the door. So, I try to do on my own.
However, when I start the program everything is ok, the LCD screen displays “Hello! SCAN Your Tag!”, but when I scan the tags there is something like “Hel &**%#( SCAN Your Tag!” instead of “Authorized access”. Why this is happening? How I should implement these function on the bottom of the code.
Thank You for Help.
Here is the code:
#include <LiquidCrystal.h>
const int rs = 5, en = 4, d4 = 3, d5 = 2, d6 = 1, d7 = 0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//LiBRARES
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
// DEFINED PINS
#define SS_PIN 10
#define RST_PIN 9
#define trigPin 7
#define echoPin 6
//ASSIGNING THE VARIABLE VALUES OF THE FUNCTION
boolean doorLocked = true;
boolean tagRead = false;
String content= "";
MFRC522 mfrc522(SS_PIN, RST_PIN); //
Servo myServo; //
void setup(){
lcd.begin(16, 2);
lcd.print(" Hello!");
lcd.setCursor(0, 1);
lcd.print(" SCAN Your Tag!");
Serial.begin(9600); //
SPI.begin(); //
mfrc522.PCD_Init(); //
myServo.attach(8); // Servo attached to PIN 8
lockDoor(); // Function, which locks the door, when the system starts
pinMode(trigPin, OUTPUT); // Mode of TrigPIN of ultrasonic sesnor
pinMode(echoPin, INPUT); // Mode of EchoPIN of ultrasonic sesnor
}
void loop()
{
int doorClosedDistance = 4; //Border distance 4cm
int distanceToDoor = getDistance(); // disranceToDoor is equal to the function getDistance
readTag(); //Function, which reads the tag
if(distanceToDoor < doorClosedDistance) // If distance to door is less than doorClosedDistance, then the door are locked
{
if(!doorLocked)
{
lockDoor();
doorLocked = true; //Variable, which is set after locking the door
}
}
if(tagRead) // If tag is read, then there is a function, which check this tag and If the tag is proper, the function opens the door
{
if(checkTag())
{
openDoor();
doorLocked = false; //Variable, which is set after opening the door
}
tagRead = false; //Variable, which is set after this whole action above, to reset
}
}
int getDistance() //Function, which read, what is the distance from ultrasonic sensor
{
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
unsigned long duration = pulseIn(echoPin, HIGH);
int cm = (duration/2) / 29.1;
return cm;
}
void openDoor() //Function, which opens the door
{
myServo.write(180);
delay(5000);
}
void lockDoor() //Function, which closes the door
{
delay(1000);
myServo.write(0);
}
bool checkTag() //Function, which checks the tag
{
boolean goodTag = false;
if (content.substring(1) == "F2 00 7C 1B")
{
printAuthorizedAccess();
goodTag = true;
}
else
{
printAccessDenied();
goodTag = false;
}
content="";
tagRead = false;
return goodTag;
printNormalMessage();
}
void readTag() // Function, which read the tag
{
if (mfrc522.PICC_IsNewCardPresent())
{
if (mfrc522.PICC_ReadCardSerial())
{
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
tagRead=true;
}
}
}
content.toUpperCase();
}
void printAuthorizedAccess() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Authorized access");
}
void printAccessDenied() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access denied");
}
void printNormalMessage() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("-Lock System On-");
lcd.setCursor(0, 1);
lcd.print(" SCAN NOW");
}
Model of LCD screen:
2x16 LCD display with blue backlight
Model 1170
Driver S6A0069