I am building a safe using an arduino uno with a keypad and finger print reader. The safe always opens even though no finger is placed on the reader. Any idea how to fix this?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Fingerprint.h>
#include <Keypad.h>
#include <Servo.h>
#include <SoftwareSerial.h>
// Define the pins
const byte ROW_NUM = 4; // four rows
const byte COLUMN_NUM = 4; // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
SoftwareSerial mySerial(11, 12); // RX, TX for SoftwareSerial
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
Servo safeServo;
// Define variables
const int correctPin = 1234; // Change this to your desired PIN
bool safeOpen = false;
// LED and buzzer pins
const int redLedPin = A1;
const int greenLedPin = A2;
const int buzzerPin = 13;
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize fingerprint sensor
finger.begin(57600); // Change the argument to match your Serial object
finger.setSecurityLevel(5);
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Enter PIN:");
// Attach servo
safeServo.attach(10); // Use pin 10 for the servo
// Set initial LED states
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
}
void loop() {
int enteredPin = readKeypad();
if (enteredPin == correctPin) {
lcd.clear();
lcd.print("Place finger...");
delay(5000);
if (validateFingerprint()) {
openSafe();
delay(5000);
closeSafe();
} else {
buzzAndDelay();
}
} else {
buzzAndDelay();
}
}
int readKeypad() {
lcd.clear();
lcd.print("Enter PIN:");
char enteredChars[5];
int i = 0;
while (true) {
char key = keypad.getKey();
if (key) {
if (key == '#') {
enteredChars[i] = '\0';
lcd.clear();
break;
} else {
lcd.setCursor(i, 1);
lcd.print('*');
enteredChars[i] = key;
i++;
}
}
}
return atoi(enteredChars);
}
bool validateFingerprint() {
lcd.clear();
lcd.print("Place finger...");
while (!finger.getImage());
int fingerprintID = finger.fingerFastSearch();
if (fingerprintID == -1 || finger.confidence < 50) {
lcd.clear();
lcd.print("Fingerprint");
lcd.setCursor(0, 1);
lcd.print("recognized");
delay(2000);
return true;
} else {
lcd.clear();
lcd.print("Fingerprint not");
lcd.setCursor(0, 1);
lcd.print("recognized");
delay(1000);
return false;
}
}
void openSafe() {
lcd.clear();
lcd.print("Safe open");
safeServo.write(0); // Open the safe
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
safeOpen = true;
}
void closeSafe() {
if (safeOpen) {
lcd.clear();
lcd.print("Press * to close");
while (true) {
char key = keypad.getKey();
if (key == '*') {
lcd.clear();
lcd.print("Safe closed");
safeServo.write(90);
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
safeOpen = false;
break;
}
}
}
}
void buzzAndDelay() {
lcd.clear();
lcd.print("Access denied");
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
delay(1000);
}