-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject2.cpp
More file actions
186 lines (143 loc) · 4.22 KB
/
Copy pathproject2.cpp
File metadata and controls
186 lines (143 loc) · 4.22 KB
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
// Program libraries
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#define TX_Pin (14) //D5
#define RX_Pin (12) //D6
#define BAUD_RATE 57600
// Head section
String username;
SoftwareSerial mySerial;
mySerial.begin(BAUD_RATE, SWSERIAL_8N1, D5, D6, false, 95, 11);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
// Program Setup
finger.begin(BAUD_RATE);
// If fingerprint sensor is connected.
if (finger.verifyPassword()) {
Serial.println("Finger print sensor connected");
} else {
Serial.println("Can not detect the fingerprint sensor");
}
// Get the numbers of people enrolled
finger.getTemplateCount();
if (finger.templateCount == 0) {
Serial.print("Fingerprint databases contains no data");
Serial.println("Please enroll a user");
}
else {
Serial.println("Waiting for valid finger...");
}
//Program loop
currentFinger = finger.getImage();
if (currentFinger == FINGERPRINT_OK){ // Only triggers if there is a valid fingerprint on the sensor
Serial.println("Valid fingerprint detected");
}
else {
Serial.print("Place finger to scan")
}
//Program loop Ends
// Function to get the corresponding user with a specific ID
String getUsername(){
userID = getFingerprintID();
switch (userID){
case 1:
String username = "User One";
break;
case 2:
String username = "User Two";
break;
case 3:
String username = "User Three";
break;
case 4:
String username = "User Four";
break;
case 5:
String username = "User Five";
break;
case 6:
String username = "User Six";
break;
default:
String username = ""; // This condition may not be true
return username
}
}
//Function to get the current user ID
uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
/*Get current finger ID
Prepare the fingerprint for comparision
Ask the sensor to convert image to feature template.*/
p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return p;
default:
Serial.println("Unknown error");
return p;
}
// Will execute search if sensor converted image to feature template.
p = finger.fingerSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
return p;
} else {
Serial.println("Unknown error");
return p;
}
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
//Function to create file
void createFile(fs::FS &fs, const char* filePath, const char* dataLogged){
File data = fs.open(filePath, FILE_WRITE);
if(!data){
Serial.println("File creation failed")
}
}
// Function to Append to existing fil
void appendToFile(fs::FS &fs, const char* filePath, const char* dataLogged){
File data = fs.open(filePath, FILE_APPEND);
if (!data){
Serial.println("Writing to file failed")
}
}
// Function to get datetime
void getDateTime(){
timeClient.update();
if(!timeClient.update(){
timeClient.forceUpdate();
})
dateTime = timeClient.getFormattedDate();
}
// Function to log data to SD card
void logData(String userInfo, filePath){
File attendance = SD.open(filePath);
if (!attendance){ // If the attendance file is not available, create one
Serial.println("The attendance file doesn\'t exist")
createFile(SD, filePath userInfo);
}
else{
appendToFile(SD, filePath, userInfo);
}
}