Untitled

 avatar
unknown
plain_text
5 months ago
2.7 kB
6
Indexable
#include <Adafruit_Fingerprint.h>
#include <Servo.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


Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
Servo myServo;

// Servo control values for continuous rotation
int pos = 0;
int servoStop = 90;       // Neutral signal (stops the servo)
int servoOpen = 120;      // Rotates in one direction (e.g., to "open")
int servoClose = 60;      // Rotates in the other direction (e.g., to "close")

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  while (!Serial) delay(1000); // Wait for Serial monitor to open

  Serial.println("Initializing Fingerprint Sensor...");
  
  // Initialize fingerprint sensor
  finger.begin(57600);
  if (finger.verifyPassword()) {
    Serial.println("Fingerprint sensor initialized successfully!");
  } else {
    Serial.println("Failed to initialize fingerprint sensor. Please check connections.");
    while (1);
  }

  // Attach the servo motor and explicitly set it to the closed position
  myServo.attach(8); // Connect the servo to pin 9
  delay(2000); // Allow time for servo to stabilize
  Serial.println("Servo initialized.");
}

void loop() {
  Serial.println("Place your finger on the sensor...");
// Wait for a fingerprint
  if (getFingerprintID() == 1) {
    Serial.println("Fingerprint verified!");
    
  //  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
  //   // in steps of 1 degree
  //   myServo.write(pos);              // tell servo to go to position in variable 'pos'
  //   delay(15);                       // waits 15 ms for the servo to reach the position
  // }
  myServo.attach(8);
  myServo.write(180);
  delay(5000);
  } else {
    myServo.detach();
    Serial.println("Fingerprint not recognized. Try again.");
  }

  delay(1000); // Small delay between attempts
}

// Function to get fingerprint ID
int getFingerprintID() {
  int result = finger.getImage();
  if (result != FINGERPRINT_OK) return -1;

  result = finger.image2Tz();
  if (result != FINGERPRINT_OK) return -1;

  result = finger.fingerFastSearch();
  if (result != FINGERPRINT_OK) return -1;

  return finger.fingerID;
}
Editor is loading...
Leave a Comment