-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharduino_code.cpp
230 lines (203 loc) · 5.9 KB
/
arduino_code.cpp
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
#include <Adafruit_Fingerprint.h>
#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)
// For UNO and others without hardware serial, we must use software serial...
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino (WHITE wire)
// Set up the serial port to use softwareserial..
SoftwareSerial mySerial(2, 3);
#else
// On Leonardo/M0/etc, others with hardware serial, use hardware serial!
// #0 is green wire, #1 is white
#define mySerial Serial1
#endif
// make finger object
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
// will hold the op choice
uint8_t op;
void setup() {
// set baude rate
Serial.begin(9600);
// wait till Serial has started
while(!Serial);
// to not overload cpu
delay(100);
// set the data rate for the sensor serial port
finger.begin(57600);
while(!finger.verifyPassword()){
Serial.println("Did not find sensor!");
delay(1);
}
Serial.println("sensor found and online!");
Serial.println(F("Reading sensor parameters"));
finger.getParameters();
Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);
Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);
Serial.print(F("Capacity: ")); Serial.println(finger.capacity);
Serial.print(F("Security level: ")); Serial.println(finger.security_level);
Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);
Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);
Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);
}
uint8_t readNumber(void){
uint8_t num = 0;
while (num == 0) {
while (! Serial.available());
num = Serial.parseInt();
}
return num;
}
uint8_t getFingerPrint(int slot = 1){
int p = -1;
while(1){
Serial.println("Place finger");
while (p != FINGERPRINT_OK){
p = finger.getImage();
switch (p) {
// fingerprin taken
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
// finger not found
case FINGERPRINT_NOFINGER:
Serial.println(".");
break;
// packet loss
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
break;
// error processing image
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
break;
// unknown error
default:
Serial.println("Unknown error");
break;
}
}
// convert image
p = finger.image2Tz(slot);
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
return p;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
default:
Serial.println("Unknown error");
}
}
}
void addFinger(void){
// get fingerprint ID
Serial.println("Enter finger print id from 1 to 127");
uint8_t id = readNumber();
// initialize variable for holding temp values
uint8_t p;
// id cannot be 0
if(id == 0){
Serial.print("Id cannot be 0");
return;
}
while(1){
while(1){
// get fingerprint
getFingerPrint(1);
// take fingerprint again
Serial.println("Remove finger");
delay(2000);
getFingerPrint(2);
// create model and match prints
p = finger.createModel();
if (p == FINGERPRINT_OK) {
Serial.println("Prints matched!");
break;
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
} else if (p == FINGERPRINT_ENROLLMISMATCH) {
Serial.println("Fingerprints did not match");
} else {
Serial.println("Unknown error");
}
}
// store model
p = finger.storeModel(id);
if (p == FINGERPRINT_OK) {
Serial.println("Stored!");
return;
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
} else if (p == FINGERPRINT_BADLOCATION) {
Serial.println("Could not store in that location");
} else if (p == FINGERPRINT_FLASHERR) {
Serial.println("Error writing to flash");
} else {
Serial.println("Unknown error");
}
}
}
void removeFinger(void){
Serial.println("Enter Id of the fingerprint that is to be deleted: ");
uint8_t id = readNumber();
if(id == 0){
return;
}
Serial.print("Deleteing ID #"); Serial.println(id);
uint8_t p = -1;
p = finger.deleteModel(id);
if (p == FINGERPRINT_OK) {
Serial.println("Deleted!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
} else if (p == FINGERPRINT_BADLOCATION) {
Serial.println("Could not delete in that location");
} else if (p == FINGERPRINT_FLASHERR) {
Serial.println("Error writing to flash");
} else {
Serial.print("Unknown error: 0x"); Serial.println(p, HEX);
}
}
void scanFinger(void){
uint8_t p;
while(1){
getFingerPrint(1);
p = finger.fingerSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
break;
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
} else {
Serial.println("Unknown error");
}
}
Serial.print("Found ID #"); Serial.println(finger.fingerID);
Serial.print("with confidence of "); Serial.println(finger.confidence);
}
void loop() {
Serial.println("Enter choice: ");
op = readNumber();
Serial.println(op);
switch(op){
case 1:
// add finger
addFinger();
break;
case 2:
// remove finger
removeFinger();
break;
case 3:
// scan for finger
scanFinger();
break;
};
}