#include <Arduino.h>
#include <ESP32Servo.h>
HardwareSerial serialPort(2); // use UART2
Servo myservo; // create servo object to control a servo
// 16 servo objects can be created on the ESP32
int pos = 0; // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
int servoPin = 5;
uint8_t getFingerprintID();
const int trig=18;
const int echo=19;
float distance ,duration;
unsigned int count=0;// variable to count the number of visitors
int ir;// variable to store the value of the sensor
int vis;// variable that stores the number of visitors
#define irpin 21
void setup()
{pinMode(irpin, INPUT);
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 18 to the servo object
Serial.begin(9600);
while (!Serial)
; // For Yun/Leo/Micro/Zero/...
delay(100);
delay(5);
if (finger.verifyPassword())
{
Serial.println("Found fingerprint sensor!");
}
else
{
Serial.println("Did not find fingerprint sensor :(");
while (1)
{
delay(1);
}
}
Serial.println(F("Reading sensor parameters"));
finger.getParameters();
Serial.print(F("Status: 0x"));
Serial.println(finger.status_reg, HEX);
Serial.print(F("Sys ID: 0x"));
Serial.println(finger.system_id, HEX);
Serial.print(F("Capacity: "));
Serial.println(finger.capacity);
Serial.print(F("Security level: "));
Serial.println(finger.security_level);
Serial.print(F("Device address: "));
Serial.println(finger.device_addr, HEX);
Serial.print(F("Packet len: "));
Serial.println(finger.packet_len);
Serial.print(F("Baud rate: "));
Serial.println(finger.baud_rate);
finger.getTemplateCount();
if (finger.templateCount == 0)
{
Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");
}
else
{
Serial.println("Waiting for valid finger...");
Serial.print("Sensor contains ");
Serial.print(finger.templateCount);
Serial.println(" templates");
}
}
void loop()
{
getFingerprintID();
ir=digitalRead(irpin);// reading the output of the sensor
if(ir==LOW){// if the sensor detects the any reflected radiation
// digitalWrite(LED,HIGH);// turn on the LED
vis=count++;
Serial.print(vis);
// ThingSpeak.setField(2,vis);
delay(500); // don't ned to run this at full speed.
// Make sure to call update as fast as possible
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
duration=pulseIn(echo,HIGH);
distance=duration*0.034/2;
Serial.println(distance);
// reading the output of the sensor
delay(500); // don't ned to run this at full speed.
ThingSpeak.setField(1,distance);
getFingerprintID();
// OK success!
// found a match!
Serial.print("Found ID #");
Serial.print(finger.fingerID);
Serial.print(" with confidence of ");
Serial.println(finger.confidence);
myservo.write(0);
delay(5000);
return finger.fingerID;
}