-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_coffre_fort.ino
246 lines (196 loc) · 4.57 KB
/
project_coffre_fort.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <Keypad.h>
#define SS_PIN 53
#define RST_PIN 50
#define SERVO_PIN 3
#define MAX_ATTEMPTS 3
#define ACCESS_DELAY 2000
#define DENIED_DELAY 1000
Servo myservo;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int R=6;
int V=7;
int buzzer=4;
int wrongAttempts=0;
int rightAttempts=0;
int wrongpsw=0;
char tab[3];
//Pad
char* password = "123A"; // change the password here, just pick any 3 numbers
int position = 0;
int error = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = { 21, 20, 19, 18 };
byte colPins[COLS] = { 17, 16, 15, 14 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
myservo.attach(SERVO_PIN);
Serial.println("Put your card to the reader Or enter your password");
Serial.println();
pinMode(V,OUTPUT);
pinMode(R,OUTPUT);
pinMode(buzzer,OUTPUT);
LockedPosition(true);
}
void loop() {
char key = keypad.getKey();
if( key!=NO_KEY)
{
if (key == '*' || key == '#')
{
position = 0;
LockedPosition(true);
}
if (key == password[position])
{
position ++;
}
else
{
position++;
error++;
}
if (position == 4 && error == 0)
{
LockedPosition(false);
position = 0;
}
else if(position == 4 && error != 0){
IncorrectKey();
position = 0;
error=0;
}
}
delay(100);
digitalWrite(buzzer,HIGH);
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
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));
}
Serial.println();
Serial.print("Message : ");
content.toUpperCase();
if (content.substring(1) == "FA 39 27 28") //change here the UID of the card
{
UnlockDoor();
}
else
{
LockDoor();
}
}
//fonctions
void UnlockDoor(){
rightAttempts++;
Serial.println("Access autorise");
digitalWrite(V,HIGH);
Serial.println();
delay(500);
digitalWrite(V,LOW);
myservo.write( 0 );
Serial.println("the door is opened");
delay(500);
if (rightAttempts % 2==0)
{
myservo.write( 180 );
Serial.println("the door is closed");
}
}
//fonction pour fem
void LockDoor(){
Serial.println(" Access non autorise");
wrongAttempts++;
digitalWrite(R,HIGH);
for(int i=0;i<MAX_ATTEMPTS;i++)
{
digitalWrite(R,HIGH);
delay(400);
digitalWrite(R,LOW);
if (wrongAttempts>= MAX_ATTEMPTS)
{
digitalWrite(R,HIGH) ;
digitalWrite(buzzer,LOW);//l3aks
delay(3000);
digitalWrite(buzzer,HIGH);
digitalWrite(R,LOW);
wrongAttempts=0;
}
}
delay(DENIED_DELAY);
}
void LockedPosition(int locked){
if (locked)
{
// digitalWrite(RedpinLock, HIGH);
//digitalWrite(GreenpinUnlock, LOW);
myservo.write(180);
digitalWrite(V,HIGH);
digitalWrite(buzzer,LOW);
delay(300);
digitalWrite(buzzer,HIGH);
digitalWrite(V,LOW);
}
else
{
if(password=="123A"){
//digitalWrite(RedpinLock, LOW);
//digitalWrite(GreenpinUnlock, HIGH);
myservo.write(0);
digitalWrite(V,HIGH);
digitalWrite(buzzer,LOW);
delay(300);
digitalWrite(buzzer,HIGH);
digitalWrite(V,LOW);
}
else {
LockDoor();
IncorrectKey (); }
}
}
void IncorrectKey() {
wrongpsw++;
digitalWrite(R, HIGH); // Turn on the red LED
delay(400);
if(wrongpsw>=3)
{
while(true)
{
digitalWrite(buzzer, LOW); // Activate the buzzer
delay(3000); // Delay for 1 second
digitalWrite(buzzer, HIGH); // Deactivate the buzzer
wrongpsw=0;
break;
}
}
digitalWrite(R, LOW); // Turn off the red LED
}