Advertisement
safwan092

Untitled

May 12th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. /***************************************************
  2. This is an example sketch for our optical Fingerprint sensor
  3.  
  4. Designed specifically to work with the Adafruit BMP085 Breakout
  5. ----> http://www.adafruit.com/products/751
  6.  
  7. These displays use TTL Serial to communicate, 2 pins are required to
  8. interface
  9. Adafruit invests time and resources providing this open source code,
  10. please support Adafruit and open-source hardware by purchasing
  11. products from Adafruit!
  12.  
  13. Written by Limor Fried/Ladyada for Adafruit Industries.
  14. BSD license, all text above must be included in any redistribution
  15. ****************************************************/
  16.  
  17. #include <Adafruit_Fingerprint.h>
  18.  
  19.  
  20. #if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)
  21. // For UNO and others without hardware serial, we must use software serial...
  22. // pin #2 is IN from sensor (GREEN wire)
  23. // pin #3 is OUT from arduino (WHITE wire)
  24. // Set up the serial port to use softwareserial..
  25. SoftwareSerial mySerial(2, 3);
  26.  
  27. #else
  28. // On Leonardo/M0/etc, others with hardware serial, use hardware serial!
  29. // #0 is green wire, #1 is white
  30. #define mySerial Serial1
  31.  
  32. #endif
  33.  
  34.  
  35. Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
  36.  
  37. uint8_t id;
  38.  
  39. void setup()
  40. {
  41. Serial.begin(9600);
  42. while (!Serial); // For Yun/Leo/Micro/Zero/...
  43. delay(100);
  44. Serial.println("\n\nAdafruit Fingerprint sensor enrollment");
  45.  
  46. // set the data rate for the sensor serial port
  47. finger.begin(57600);
  48.  
  49. if (finger.verifyPassword()) {
  50. Serial.println("Found fingerprint sensor!");
  51. } else {
  52. Serial.println("Did not find fingerprint sensor :(");
  53. while (1) { delay(1); }
  54. }
  55.  
  56. Serial.println(F("Reading sensor parameters"));
  57. finger.getParameters();
  58. Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);
  59. Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);
  60. Serial.print(F("Capacity: ")); Serial.println(finger.capacity);
  61. Serial.print(F("Security level: ")); Serial.println(finger.security_level);
  62. Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);
  63. Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);
  64. Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);
  65. }
  66.  
  67. uint8_t readnumber(void) {
  68. uint8_t num = 0;
  69.  
  70. while (num == 0) {
  71. while (! Serial.available());
  72. num = Serial.parseInt();
  73. }
  74. return num;
  75. }
  76.  
  77. void loop() // run over and over again
  78. {
  79. Serial.println("Ready to enroll a fingerprint!");
  80. Serial.println("Please type in the ID # (from 1 to 127) you want to save this finger as...");
  81. id = readnumber();
  82. if (id == 0) {// ID #0 not allowed, try again!
  83. return;
  84. }
  85. Serial.print("Enrolling ID #");
  86. Serial.println(id);
  87.  
  88. while (! getFingerprintEnroll() );
  89. }
  90.  
  91. uint8_t getFingerprintEnroll() {
  92.  
  93. int p = -1;
  94. Serial.print("Waiting for valid finger to enroll as #"); Serial.println(id);
  95. while (p != FINGERPRINT_OK) {
  96. p = finger.getImage();
  97. switch (p) {
  98. case FINGERPRINT_OK:
  99. Serial.println("Image taken");
  100. break;
  101. case FINGERPRINT_NOFINGER:
  102. Serial.println(".");
  103. break;
  104. case FINGERPRINT_PACKETRECIEVEERR:
  105. Serial.println("Communication error");
  106. break;
  107. case FINGERPRINT_IMAGEFAIL:
  108. Serial.println("Imaging error");
  109. break;
  110. default:
  111. Serial.println("Unknown error");
  112. break;
  113. }
  114. }
  115.  
  116. // OK success!
  117.  
  118. p = finger.image2Tz(1);
  119. switch (p) {
  120. case FINGERPRINT_OK:
  121. Serial.println("Image converted");
  122. break;
  123. case FINGERPRINT_IMAGEMESS:
  124. Serial.println("Image too messy");
  125. return p;
  126. case FINGERPRINT_PACKETRECIEVEERR:
  127. Serial.println("Communication error");
  128. return p;
  129. case FINGERPRINT_FEATUREFAIL:
  130. Serial.println("Could not find fingerprint features");
  131. return p;
  132. case FINGERPRINT_INVALIDIMAGE:
  133. Serial.println("Could not find fingerprint features");
  134. return p;
  135. default:
  136. Serial.println("Unknown error");
  137. return p;
  138. }
  139.  
  140. Serial.println("Remove finger");
  141. delay(2000);
  142. p = 0;
  143. while (p != FINGERPRINT_NOFINGER) {
  144. p = finger.getImage();
  145. }
  146. Serial.print("ID "); Serial.println(id);
  147. p = -1;
  148. Serial.println("Place same finger again");
  149. while (p != FINGERPRINT_OK) {
  150. p = finger.getImage();
  151. switch (p) {
  152. case FINGERPRINT_OK:
  153. Serial.println("Image taken");
  154. break;
  155. case FINGERPRINT_NOFINGER:
  156. Serial.print(".");
  157. break;
  158. case FINGERPRINT_PACKETRECIEVEERR:
  159. Serial.println("Communication error");
  160. break;
  161. case FINGERPRINT_IMAGEFAIL:
  162. Serial.println("Imaging error");
  163. break;
  164. default:
  165. Serial.println("Unknown error");
  166. break;
  167. }
  168. }
  169.  
  170. // OK success!
  171.  
  172. p = finger.image2Tz(2);
  173. switch (p) {
  174. case FINGERPRINT_OK:
  175. Serial.println("Image converted");
  176. break;
  177. case FINGERPRINT_IMAGEMESS:
  178. Serial.println("Image too messy");
  179. return p;
  180. case FINGERPRINT_PACKETRECIEVEERR:
  181. Serial.println("Communication error");
  182. return p;
  183. case FINGERPRINT_FEATUREFAIL:
  184. Serial.println("Could not find fingerprint features");
  185. return p;
  186. case FINGERPRINT_INVALIDIMAGE:
  187. Serial.println("Could not find fingerprint features");
  188. return p;
  189. default:
  190. Serial.println("Unknown error");
  191. return p;
  192. }
  193.  
  194. // OK converted!
  195. Serial.print("Creating model for #"); Serial.println(id);
  196.  
  197. p = finger.createModel();
  198. if (p == FINGERPRINT_OK) {
  199. Serial.println("Prints matched!");
  200. } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  201. Serial.println("Communication error");
  202. return p;
  203. } else if (p == FINGERPRINT_ENROLLMISMATCH) {
  204. Serial.println("Fingerprints did not match");
  205. return p;
  206. } else {
  207. Serial.println("Unknown error");
  208. return p;
  209. }
  210.  
  211. Serial.print("ID "); Serial.println(id);
  212. p = finger.storeModel(id);
  213. if (p == FINGERPRINT_OK) {
  214. Serial.println("Stored!");
  215. } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  216. Serial.println("Communication error");
  217. return p;
  218. } else if (p == FINGERPRINT_BADLOCATION) {
  219. Serial.println("Could not store in that location");
  220. return p;
  221. } else if (p == FINGERPRINT_FLASHERR) {
  222. Serial.println("Error writing to flash");
  223. return p;
  224. } else {
  225. Serial.println("Unknown error");
  226. return p;
  227. }
  228.  
  229. return true;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement