Advertisement
mikroavr

finger print motor

Mar 31st, 2019
2,394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_Fingerprint.h>
  2. #include <SoftwareSerial.h>
  3. SoftwareSerial mySerial(2, 3); // rx, tx
  4.  
  5. Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
  6.  
  7. const byte R_ON = 16;
  8. const byte R_ALARM = 17;
  9.  
  10. int control = 0;
  11. int count = 0;
  12. void relay_control();
  13.  
  14. #define LIMIT 3
  15. #define BENAR 1
  16. #define SALAH 2
  17.  
  18. void setup()  
  19. {
  20.  
  21.   pinMode(R_ON, OUTPUT);
  22.   pinMode(R_ALARM, OUTPUT);
  23.  
  24.   digitalWrite(R_ON, LOW); // mematikan relay
  25.   digitalWrite(R_ALARM, LOW); // mematikan ALARM
  26.  
  27.   Serial.begin(9600); // setting kecepatan transfer komunikasi serial
  28.   while (!Serial);
  29.   delay(100); //tunda waktu selama 1/10 detik
  30.   Serial.println("\n\nAdafruit finger detect test");
  31.  
  32.   // set the data rate for the sensor serial port
  33.   finger.begin(57600);
  34.  
  35.   if (finger.verifyPassword()) { // jika
  36.     Serial.println("Found fingerprint sensor!");
  37.   } else {
  38.     Serial.println("Did not find fingerprint sensor :(");
  39.     while (1) { delay(1); }
  40.   }
  41.  
  42.   finger.getTemplateCount();
  43.   Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  44.   Serial.println("Waiting for valid finger...");
  45. }
  46.  
  47. void loop()                     // run over and over again
  48. {
  49.  
  50.   //getFingerprintIDez();
  51.   getFingerprintID();
  52.   relay_control(); // CEK control relay
  53.   delay(100);            //don't ned to run this at full speed.
  54.  
  55. }
  56.  
  57. uint8_t getFingerprintID() {
  58.   uint8_t p = finger.getImage();
  59.   switch (p) {
  60.     case FINGERPRINT_OK:
  61.       Serial.println("Image taken"); // jika di tempel sidik jari
  62.       break;
  63.     case FINGERPRINT_NOFINGER:
  64.       Serial.println("No finger detected"); // tidak di tempel sidik jari
  65.       return p;
  66.     case FINGERPRINT_PACKETRECIEVEERR:
  67.       Serial.println("Communication error");
  68.       return p;
  69.     case FINGERPRINT_IMAGEFAIL:
  70.       Serial.println("Imaging error");
  71.       return p;
  72.     default:
  73.       Serial.println("Unknown error");
  74.       return p;
  75.   }
  76.  
  77.   // OK success!
  78.  
  79.   p = finger.image2Tz();
  80.   switch (p) {
  81.     case FINGERPRINT_OK:
  82.       Serial.println("Image converted");
  83.       break;
  84.     case FINGERPRINT_IMAGEMESS:
  85.       Serial.println("Image too messy");
  86.       return p;
  87.     case FINGERPRINT_PACKETRECIEVEERR:
  88.       //Serial.println("Communication error");
  89.       return p;
  90.     case FINGERPRINT_FEATUREFAIL:
  91.       //Serial.println("Could not find fingerprint features");
  92.       return p;
  93.     case FINGERPRINT_INVALIDIMAGE:
  94.       //Serial.println("Could not find fingerprint features");
  95.       return p;
  96.     default:
  97.       Serial.println("Unknown error");
  98.       return p;
  99.   }
  100.  
  101.   // OK converted!
  102.   p = finger.fingerFastSearch();
  103.   if (p == FINGERPRINT_OK) {
  104.     Serial.println("Found a print match!"); // jika di tempel sidika jari dan sidik jari terdaftar
  105.     control = 1;
  106.    
  107.   } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  108.     //Serial.println("Communication error");
  109.     return p;
  110.   } else if (p == FINGERPRINT_NOTFOUND) {
  111.     Serial.println("Did not find a match"); // di tempel tapi tidak ada terdaftar sidik jari
  112.     control = 2;
  113.     count = count + 1;
  114.     Serial.println(control);
  115.    
  116.     return p;
  117.   } else {
  118.     Serial.println("Unknown error");
  119.     return p;
  120.   }  
  121.  
  122.   // found a match!
  123.   Serial.print("Found ID #"); Serial.print(finger.fingerID);
  124.   Serial.print(" with confidence of "); Serial.println(finger.confidence);
  125.  
  126.   return finger.fingerID;
  127. }
  128.  
  129. // returns -1 if failed, otherwise returns ID #
  130. int getFingerprintIDez() {
  131.   uint8_t p = finger.getImage();
  132.   if (p != FINGERPRINT_OK)  return -1;
  133.  
  134.   p = finger.image2Tz();
  135.   if (p != FINGERPRINT_OK)  return -1;
  136.  
  137.   p = finger.fingerFastSearch();
  138.   if (p != FINGERPRINT_OK)  return -1;
  139.  
  140.   // found a match!
  141.   Serial.print("Found ID #"); Serial.print(finger.fingerID);
  142.   Serial.print(" with confidence of "); Serial.println(finger.confidence);
  143.   return finger.fingerID;
  144. }
  145.  
  146. void relay_control(){
  147.   if ( control == BENAR ){
  148.     digitalWrite(R_ON, HIGH); // relay di on kan --> MOTOR HIDUP
  149.     digitalWrite(R_ALARM, LOW); // --> ALARM MATI
  150.   }
  151.  
  152.   else if ( control == SALAH ){
  153.     digitalWrite(R_ON, LOW); // ---> MOTOR MATI
  154.     Serial.println(count);
  155.       if ( count == LIMIT){
  156.         digitalWrite(R_ALARM, HIGH); //---> ALARM HIDUP SETELAH LIMIT
  157.       }
  158.       else{
  159.         digitalWrite(R_ALARM, LOW);
  160.       }
  161.   }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement