Untitled
unknown
plain_text
2 years ago
5.6 kB
5
Indexable
#include <Servo.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Adafruit_Fingerprint.h>
// fingerprint setup
#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);
uint8_t id;
// servo
Servo servo;
// lcd
int row = 0;
int col = 0;
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
// keypad
int step = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
byte pin_rows[ROWS] = {A3, A2, A1, A0};
byte pin_cols[COLS] = {13, 12, 11, 10};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_cols, ROWS, COLS);
// button A,B states
int a = 0;
int b = 0;
// store password
int password = 0;
int correctPassword = 12345;
// door state
int doorOpen = 0;
void setup()
{
Serial.begin(9600);
servo.attach(A4);
lcd.begin(16, 2);
Serial.println("Start");
}
void loop()
{
// variables
char key = keypad.getKey();
// start
if (doorOpen == 1)
{
servo.write(135);
}
if (key == '*')
{
doorOpen = 0;
step = 0;
password = 0;
servo.write(0);
}
if (step == 0)
{
lcd.setCursor(row, col);
lcd.print("Start ");
lcd.setCursor(row, col + 1);
lcd.print("press D to continue");
if (key == 'D')
{
Serial.println("----------------------------------------------------------------");
Serial.println("A: Password");
Serial.println("B: Fingerprint");
Serial.println("----------------------------------------------------------------");
Serial.println("Fingerprint ");
Serial.println("----------------------------------------------------------------");
Serial.println("Press A to create a new fingerprint");
Serial.println("Press B to save a fingerprint");
Serial.println("Press C to open by fingerprinting");
Serial.println("Press D to delete a fingerprint");
step = 1;
}
}
// run
if (step == 1)
{
lcd.setCursor(row, col);
lcd.println("A: Password ");
lcd.setCursor(row, col + 1);
lcd.println("B: Fingerprint ");
if (key)
{
switch (key)
{
case 'A':
a = 1;
step = 2;
break;
case 'B':
b = 1;
step = 2;
break;
default:
break;
}
}
}
// password
if (a == 1)
{
lcd.setCursor(row, col);
lcd.print("Enter Password ");
lcd.setCursor(row, col + 1);
lcd.print(" ");
password = getPassword();
doorOpen = checkPassword(password, correctPassword);
step = 1;
a = 0;
}
// fingerprint
if (b == 1)
{
lcd.setCursor(row, col);
lcd.println("Fingerprint ");
lcd.setCursor(row, col + 1);
lcd.println("Press A,C,D");
// press keypad
if (key)
{
switch (key)
{
case 'A':
lcd.setCursor(row, col);
lcd.println("Add a fingerprint");
lcd.setCursor(row, col + 1);
lcd.println(" ");
//code them van tay
break;
case 'C':
lcd.setCursor(row, col);
lcd.println("Put your finger into the fingerprint");
lcd.setCursor(row, col + 1);
lcd.println(" " );
//code nhan van tay
break;
case 'D':
lcd.setCursor(row, col);
lcd.println("delete a fingerprint");
lcd.setCursor(row, col + 1);
lcd.println("Input finger id: ");
//code xoa van tay
break;
default:
break;
}
}
step = 1;
b = 0;
}
} // end loop
// functions
int getPassword()
{
String enteredPassword = "";
char key;
int i = 0;
while (true)
{
key = keypad.getKey();
if (key)
{
lcd.setCursor(row + i, col + 1);
lcd.println("*");
i++;
// Check if the entered password is complete
if (key == 'D')
{
break;
}
else if (key == '*')
{
// Clear the entered password if the asterisk key is pressed
enteredPassword = "";
}
else
{
// Append the entered digit to the password
enteredPassword += key;
}
}
}
// Convert the entered password from String to integer
int password = enteredPassword.toInt();
return password;
}
int checkPassword(int password, int correctPassword)
{
if (password == correctPassword)
{
lcd.setCursor(row, col);
lcd.print("Password Correct ");
lcd.setCursor(row, col + 1);
lcd.print(" ");
delay(1000);
return 1; // Password is correct
}
else
{
lcd.setCursor(row, col);
lcd.print("Password Incorrect");
lcd.setCursor(row, col + 1);
lcd.print(" ");
delay(1000);
return 0; // Password is incorrect
}
}
Editor is loading...