safwan092

fingerprint code working - Enroll

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