microrobotics

R503_LED_Control_Enroll_Finger_with_I2C_LCD_1602

Aug 6th, 2021
1,401
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_Fingerprint.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <Wire.h>
  4.  
  5. #define WAKE_PIN 13
  6.  
  7. // On Leonardo/Micro or others with hardware serial, use those!
  8. // uncomment this line:
  9. // #define mySerial Serial1
  10.  
  11. // For UNO and others without hardware serial, we must use software serial...
  12. // pin #2 is IN from sensor
  13. // pin #3 is OUT from arduino
  14. // comment these two lines if using hardware serial
  15. //Open Serial Monitor
  16.  
  17. SoftwareSerial mySerial(2, 3);
  18.  
  19. LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  20.  
  21. Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
  22.  
  23. uint8_t id;
  24.  
  25. void setup()
  26. {
  27.   pinMode(WAKE_PIN, INPUT );
  28.  
  29.   lcd.begin(); // initialize the lcd
  30.   lcd.clear();
  31.   lcd.backlight(); // Turn backlight On
  32.  
  33.   Serial.begin(9600);
  34.   while (!Serial);  // For Yun/Leo/Micro/Zero/...
  35.   delay(100);
  36.   Serial.println("\n\nAdafruit Fingerprint sensor enrollment");
  37.  
  38.   // set the data rate for the sensor serial port
  39.   finger.begin(57600);
  40.  
  41.   if (finger.verifyPassword()) {
  42.     Serial.println("Found fingerprint sensor!");
  43.   } else {
  44.     Serial.println("Did not find fingerprint sensor :(");
  45.     while (1) {
  46.       delay(1);
  47.     }
  48.   }
  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.     delay(2000);
  64.     finger.LEDcontrol(FINGERPRINT_LED_GRADUAL_ON, 200, FINGERPRINT_LED_PURPLE);
  65.     delay(2000);
  66.     finger.LEDcontrol(FINGERPRINT_LED_GRADUAL_OFF, 200, FINGERPRINT_LED_PURPLE);
  67.     delay(2000);
  68.  
  69.   lcd.setCursor(0, 0);
  70.   lcd.print("Fingerprint");
  71.   lcd.setCursor(0, 1);
  72.   lcd.print("Enrollment");
  73.   delay(2000);
  74.   lcd.clear();
  75.  
  76.   Serial.println("Ready to enroll a fingerprint!");
  77.   Serial.println("Please type in the ID # (from 1 to 127) you want to save this finger as...");
  78.   lcd.setCursor(0, 0);
  79.   lcd.print("Enter finger ID");
  80.   delay(2000);
  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.  
  89.  
  90.   char buffer[16];
  91.   sprintf(buffer, "Enrolling ID #%d", id);
  92.   lcd.setCursor(0, 0);
  93.   lcd.print(buffer);
  94.   lcd.setCursor(0, 1);
  95.   lcd.print("Place finger");
  96.  
  97.   while (!  getFingerprintEnroll() );
  98. }
  99.  
  100. uint8_t getFingerprintEnroll() {
  101.  
  102.   int p = -1;
  103.   Serial.print("Waiting for valid finger to enroll as #"); Serial.println(id);
  104.    finger.LEDcontrol(FINGERPRINT_LED_BREATHING, 100, FINGERPRINT_LED_BLUE);
  105.   while (p != FINGERPRINT_OK) {
  106.     p = finger.getImage();
  107.     switch (p) {
  108.       case FINGERPRINT_OK:
  109.  
  110.         finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_BLUE);
  111.         Serial.println("Image taken");
  112.         lcd.setCursor(0, 1);
  113.         lcd.print("Image taken");
  114.         delay(1000);
  115.         lcd.clear();
  116.         break;
  117.       case FINGERPRINT_NOFINGER:
  118.         Serial.println(".");
  119.         break;
  120.       case FINGERPRINT_PACKETRECIEVEERR:
  121.         Serial.println("Communication error");
  122.         break;
  123.       case FINGERPRINT_IMAGEFAIL:
  124.         Serial.println("Imaging error");
  125.         break;
  126.       default:
  127.         Serial.println("Unknown error");
  128.         break;
  129.     }
  130.   }
  131.  
  132.   // OK success!
  133.  
  134.   p = finger.image2Tz(1);
  135.   switch (p) {
  136.     case FINGERPRINT_OK:
  137.       Serial.println("Image converted");
  138.       break;
  139.     case FINGERPRINT_IMAGEMESS:
  140.       Serial.println("Image too messy");
  141.       return p;
  142.     case FINGERPRINT_PACKETRECIEVEERR:
  143.       Serial.println("Communication error");
  144.       return p;
  145.     case FINGERPRINT_FEATUREFAIL:
  146.       Serial.println("Could not find fingerprint features");
  147.       return p;
  148.     case FINGERPRINT_INVALIDIMAGE:
  149.       Serial.println("Could not find fingerprint features");
  150.       return p;
  151.     default:
  152.       Serial.println("Unknown error");
  153.       return p;
  154.   }
  155.  
  156.   Serial.println("Remove finger");
  157.   lcd.setCursor(0, 0);
  158.   lcd.print("Remove finger");
  159.   delay(2000);
  160.   lcd.clear();
  161.   p = 0;
  162.   while (p != FINGERPRINT_NOFINGER) {
  163.     p = finger.getImage();
  164.   }
  165.   finger.LEDcontrol(FINGERPRINT_LED_BREATHING, 100, FINGERPRINT_LED_BLUE);
  166.   Serial.print("ID "); Serial.println(id);
  167.   p = -1;
  168.   Serial.println("Place same finger again");
  169.   lcd.setCursor(0, 0);
  170.   lcd.print("Place finger");
  171.   lcd.setCursor(0, 1);
  172.   lcd.print("again");
  173.   delay(2000);
  174.   lcd.clear();
  175.   while (p != FINGERPRINT_OK) {
  176.     p = finger.getImage();
  177.     switch (p) {
  178.       case FINGERPRINT_OK:
  179.         Serial.println("Image taken");
  180.         break;
  181.       case FINGERPRINT_NOFINGER:
  182.         Serial.print(".");
  183.         break;
  184.       case FINGERPRINT_PACKETRECIEVEERR:
  185.         Serial.println("Communication error");
  186.         break;
  187.       case FINGERPRINT_IMAGEFAIL:
  188.         Serial.println("Imaging error");
  189.         break;
  190.       default:
  191.         Serial.println("Unknown error");
  192.         break;
  193.     }
  194.   }
  195.  
  196.   // OK success!
  197.  
  198.   p = finger.image2Tz(2);
  199.   switch (p) {
  200.     case FINGERPRINT_OK:
  201.       Serial.println("Image converted");
  202.       break;
  203.     case FINGERPRINT_IMAGEMESS:
  204.       Serial.println("Image too messy");
  205.       return p;
  206.     case FINGERPRINT_PACKETRECIEVEERR:
  207.       Serial.println("Communication error");
  208.       return p;
  209.     case FINGERPRINT_FEATUREFAIL:
  210.       Serial.println("Could not find fingerprint features");
  211.       return p;
  212.     case FINGERPRINT_INVALIDIMAGE:
  213.       Serial.println("Could not find fingerprint features");
  214.       return p;
  215.     default:
  216.       Serial.println("Unknown error");
  217.       return p;
  218.   }
  219.  
  220.   // OK converted!
  221.   Serial.print("Creating model for #");  Serial.println(id);
  222.  
  223.   p = finger.createModel();
  224.   if (p == FINGERPRINT_OK) {
  225.     Serial.println("Prints matched!");
  226.   } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  227.     Serial.println("Communication error");
  228.     return p;
  229.   } else if (p == FINGERPRINT_ENROLLMISMATCH) {
  230.     finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_RED, 10);
  231.     Serial.println("Fingerprints did not match");
  232.     lcd.setCursor(0, 0);
  233.     lcd.print("Fingerprints did not match");
  234.     lcd.setCursor(0, 1);
  235.     lcd.print("not match");
  236.     delay(2000);
  237.     lcd.clear();
  238.    
  239.  
  240.     return p;
  241.   } else {
  242.     Serial.println("Unknown error");
  243.     return p;
  244.   }
  245.  
  246.   Serial.print("ID "); Serial.println(id);
  247.   p = finger.storeModel(id);
  248.   if (p == FINGERPRINT_OK) {
  249.     Serial.println("Stored!");
  250.     lcd.setCursor(0, 0);
  251.     lcd.print("Stored");
  252.  
  253.   } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  254.     Serial.println("Communication error");
  255.     return p;
  256.   } else if (p == FINGERPRINT_BADLOCATION) {
  257.     Serial.println("Could not store in that location");
  258.     return p;
  259.   } else if (p == FINGERPRINT_FLASHERR) {
  260.     Serial.println("Error writing to flash");
  261.     return p;
  262.   } else {
  263.     Serial.println("Unknown error");
  264.     return p;
  265.   }
  266. }
RAW Paste Data