Skip to content

Commit fcc703c

Browse files
Update arduino_code.ino
1 parent 66f683d commit fcc703c

File tree

1 file changed

+121
-95
lines changed

1 file changed

+121
-95
lines changed
Lines changed: 121 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,98 @@
1-
#include <RCSwitch.h>
2-
#include <MFRC522.h>
3-
#include <Keypad.h>
41
#include <SPI.h>
5-
#include <LiquidCrystal.h>
6-
7-
// RFID
8-
#define SS_PIN 6
9-
#define RST_PIN 5
10-
#define solenoidPin 4
11-
MFRC522 rfid(SS_PIN, RST_PIN);
12-
13-
const int rs = 12; // Digital Pin 12
14-
const int en = 11; // Digital Pin 11
15-
const int d6 = 10; // Digital Pin 10
16-
const int d4 = 9; // Digital Pin 9
17-
const int d5 = 8; // Digital Pin 8
18-
const int d7 = 7; // Digital Pin 7
19-
20-
// Keypad
21-
const byte ROWS = 4; // Four rows
22-
const byte COLS = 3; // Three columns
23-
char keys[ROWS][COLS] = {
24-
{'1', '2', '3'},
25-
{'4', '5', '6'},
26-
{'7', '8', '9'},
27-
{'*', '0', '#'}
28-
};
29-
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
30-
byte colPins[COLS] = {5, 4, 3}; // Connect to the column pinouts of the keypad
31-
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
32-
33-
// Users RFID PASSWORD NAME
34-
String validCards[] = {"CARD1_UID", "CARD2_UID"};
35-
String userPasswords[] = {"1234", "5678"};
36-
String userNames[] = {"X", "Y"};
2+
#include <Wire.h>
3+
#include <LiquidCrystal_I2C.h>
4+
#include <Servo.h>
5+
6+
#define RelayPin 2
7+
8+
LiquidCrystal_I2C lcd(0x27, 16, 2);
9+
Servo garageServo;
3710

38-
// LCD
39-
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
11+
const int buttonPins[] = {13, 12, 11, 10, 9, 8, 7, 6, 5};
12+
const int buttonNumbers[] = {7, 4, 1, 8, 5, 2, 9, 6, 3};
13+
14+
String userPasswords[] = {"1234", "5678"};
15+
String userNames[] = {"User1", "User2"};
4016

4117
String enteredPassword = "";
18+
String lastResult = ""; // To store the face recognition result
4219

4320
void setup() {
4421
Serial.begin(115200);
45-
SPI.begin();
46-
rfid.PCD_Init();
47-
lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
22+
Serial.println("Serial connection started. Type 'OPEN_DOOR' to test.");
23+
24+
lcd.init();
25+
lcd.backlight();
26+
lcd.setCursor(0, 0);
27+
lcd.print(" HVZ House ");
28+
lcd.setCursor(0, 1);
29+
lcd.print(" Hosgeldiniz! ");
30+
31+
pinMode(RelayPin, OUTPUT);
32+
digitalWrite(RelayPin, HIGH);
33+
34+
for (int i = 0; i < 9; i++) {
35+
pinMode(buttonPins[i], INPUT_PULLUP);
36+
}
4837
}
4938

5039
void loop() {
51-
// process incoming commands from serial port
5240
if (Serial.available() > 0) {
5341
String command = Serial.readStringUntil('\n');
42+
command.trim();
5443
if (command == "OPEN_DOOR") {
5544
openDoor();
56-
}
57-
}
58-
59-
// RFID Read
60-
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
61-
String cardUID = "";
62-
for (byte i = 0; i < rfid.uid.size; i++) {
63-
cardUID += String(rfid.uid.uidByte[i], HEX);
64-
}
65-
if (isValidCard(cardUID)) {
66-
String user = getUserByCard(cardUID);
45+
} else if (command == "VALID_PASSWORD") {
6746
openDoor();
68-
displayGreeting(user, cardUID);
69-
Serial.print("Hello ");
70-
Serial.println(user);
71-
sendResultToJetson("Hello " + user);
47+
} else if (command == "OPEN_GARAGE") {
48+
openGarage();
49+
} else if (command == "FACE_RECOGNITION") {
50+
Serial.println("Facial Recognition Started...");
51+
} else if (command.startsWith("IDENTIFIED:")) {
52+
String name = command.substring(11); // Remove the "IDENTIFIED:" part
53+
displayFaceRecognitionGreeting(name);
7254
}
73-
rfid.PICC_HaltA();
7455
}
7556

76-
// Keypad Read
77-
char key = keypad.getKey();
78-
if (key) {
79-
if (key == '#') {
80-
if (isValidPassword(enteredPassword)) {
81-
String user = getUserByPassword(enteredPassword);
82-
openDoor();
83-
displayGreeting(user, "PASSWORD");
84-
Serial.print("Hello ");
85-
Serial.println(user);
86-
sendResultToJetson("Hello " + user);
87-
}
88-
enteredPassword = "";
89-
} else {
57+
for (int i = 0; i < 9; i++) {
58+
if (digitalRead(buttonPins[i]) == LOW) {
59+
String key = String(buttonNumbers[i]);
9060
enteredPassword += key;
61+
Serial.print("Entered: ");
62+
Serial.println(enteredPassword);
63+
64+
lcd.clear();
65+
lcd.setCursor(0, 0);
66+
lcd.print(" HVZ House ");
67+
lcd.setCursor((16 - enteredPassword.length()) / 2, 1);
68+
lcd.print(enteredPassword);
69+
70+
if (enteredPassword.length() >= 4) {
71+
if (isValidPassword(enteredPassword)) {
72+
String user = getUserByPassword(enteredPassword);
73+
delay(1000);
74+
displayGreeting(user, "Sifre");
75+
Serial.print("Hello ");
76+
Serial.println(user);
77+
sendResultToJetson("Hello " + user);
78+
openDoor();
79+
} else {
80+
Serial.println("Invalid Password!");
81+
lcd.clear();
82+
lcd.setCursor(0, 0);
83+
lcd.print(" Hatali Sifre ");
84+
delay(2000);
85+
lcd.clear();
86+
lcd.setCursor(0, 0);
87+
lcd.print(" HVZ House ");
88+
}
89+
enteredPassword = "";
90+
}
91+
delay(200);
9192
}
9293
}
9394
}
9495

95-
bool isValidCard(String uid) {
96-
for (String validCard : validCards) {
97-
if (validCard == uid) {
98-
return true;
99-
}
100-
}
101-
return false;
102-
}
103-
10496
bool isValidPassword(String password) {
10597
for (String userPassword : userPasswords) {
10698
if (userPassword == password) {
@@ -110,17 +102,8 @@ bool isValidPassword(String password) {
110102
return false;
111103
}
112104

113-
String getUserByCard(String uid) {
114-
for (int i = 0; i < sizeof(validCards) / sizeof(validCards[0]); i++) {
115-
if (validCards[i] == uid) {
116-
return userNames[i];
117-
}
118-
}
119-
return "Unknown";
120-
}
121-
122105
String getUserByPassword(String password) {
123-
for (int i = 0; i < sizeof(userPasswords) / sizeof(userPasswords[0]); i++) {
106+
for (int i = 0; i < (sizeof(userPasswords) / sizeof(userPasswords[0])); i++) {
124107
if (userPasswords[i] == password) {
125108
return userNames[i];
126109
}
@@ -129,9 +112,24 @@ String getUserByPassword(String password) {
129112
}
130113

131114
void openDoor() {
132-
digitalWrite(solenoidPin, HIGH);
133-
delay(1000); // 1 second delay
134-
digitalWrite(solenoidPin, LOW);
115+
Serial.println("openDoor() called");
116+
digitalWrite(RelayPin, LOW);
117+
delay(5000);
118+
digitalWrite(RelayPin, HIGH);
119+
Serial.println("DOOR OPENED");
120+
}
121+
122+
void openGarage() {
123+
Serial.println("openGarage() called");
124+
garageServo.attach(4); //Connect (activate) the servo
125+
garageServo.write(180);
126+
Serial.println("GARAGE OPENED");
127+
delay(1000);
128+
garageServo.write(30);
129+
Serial.println("Servo Up");
130+
delay(500);
131+
garageServo.detach(); // Disable servo
132+
135133
}
136134

137135
void sendResultToJetson(String message) {
@@ -141,9 +139,37 @@ void sendResultToJetson(String message) {
141139

142140
void displayGreeting(String user, String identifier) {
143141
lcd.clear();
144-
lcd.setCursor(0, 0);
142+
lcd.setCursor((16 - (8 + user.length())) / 2, 0); // 'Merhaba' is 8 characters long.
145143
lcd.print("Merhaba ");
146144
lcd.print(user);
145+
146+
String fullText = "Giris: " + identifier;
147+
lcd.setCursor((16 - fullText.length()) / 2, 1);
148+
lcd.print(fullText);
149+
150+
delay(2000);
151+
lcd.clear();
152+
lcd.setCursor(0, 0);
153+
lcd.print(" HVZ House ");
154+
lcd.setCursor(0, 1);
155+
lcd.print(" Hosgeldiniz! ");
156+
}
157+
158+
void displayFaceRecognitionGreeting(String name) {
159+
lcd.clear();
160+
lcd.setCursor((16 - (8 + name.length())) / 2, 0); // 'Merhaba' is 8 characters long.
161+
lcd.print("Merhaba ");
162+
lcd.print(name);
163+
164+
lcd.setCursor((16 - 22) / 2, 0); //to center the phrase "the homeowner has been informed"
165+
lcd.print("H3RUM0'e bilgi");
166+
lcd.setCursor((16 - 22) / 2, 1); // Ekranda ikinci satır
167+
lcd.print("verildi");
168+
169+
delay(2000);
170+
lcd.clear();
171+
lcd.setCursor(0, 0);
172+
lcd.print(" HVZ House ");
147173
lcd.setCursor(0, 1);
148-
lcd.print(identifier);
174+
lcd.print(" Hosgeldiniz! ");
149175
}

0 commit comments

Comments
 (0)