#include <EEPROM.h>
const int addButtonPin = 15; // Define the "Add" button pin
const int lookupButtonPin = 2; // Define the "Lookup" button pin
const int deleteButtonPin = 4; // Define the "Delete" button pin
boolean addButtonState = false; // Variable to store the "Add" button state
boolean lookupButtonState = false; // Variable to store the "Lookup" button state
boolean deleteButtonState = false; // Variable to store the "Delete" button state
boolean addButtonPrevState = false; // Variable to store the previous "Add" button state
boolean lookupButtonPrevState = false; // Variable to store the previous "Lookup" button state
boolean deleteButtonPrevState = false; // Variable to store the previous "Delete" button state
boolean dataInputInProgress = false; // Flag to track data input
boolean deleteInProgress = false; // Flag to track delete process
int lastId = 0; // Variable to store the last used ID
struct Entry {
int id;
String name;
String phoneNumber;
boolean deleted;
};
const int maxEntries = 100; // Define the maximum number of entries
Entry entries[maxEntries]; // Array to store entries
const int entrySize = sizeof(Entry); // Calculate the size of one Entry structure
void setup() {
Serial.begin(9600); // Set the baud rate to match the one in the serial monitor
pinMode(addButtonPin, INPUT_PULLUP); // Set the "Add" button pin as input with pull-up resistor
pinMode(lookupButtonPin, INPUT_PULLUP); // Set the "Lookup" button pin as input with pull-up resistor
pinMode(deleteButtonPin, INPUT_PULLUP); // Set the "Delete" button pin as input with pull-up resistor
EEPROM.begin(maxEntries * entrySize); // Initialize EEPROM with enough space for all entries
loadEntriesFromEEPROM(); // Load existing data from EEPROM
}
void loop() {
// Read the "Add" button state
addButtonState = digitalRead(addButtonPin);
// Check if the "Add" button is pressed and data input is not in progress
if (addButtonState == LOW && addButtonPrevState == HIGH && !dataInputInProgress) {
int newId = findNextAvailableId(); // Get the next available ID
if (newId != -1) {
Serial.println("ID: " + String(newId));
Serial.println("Please enter your name:");
while (Serial.available() == 0); // Wait for user input
entries[newId - 1].name = Serial.readStringUntil('\n');
Serial.println("Please enter your phone number:");
while (Serial.available() == 0); // Wait for user input
entries[newId - 1].phoneNumber = Serial.readStringUntil('\n');
entries[newId - 1].deleted = false; // Entry is not deleted
lastId = newId; // Update the last used ID
Serial.println("Name: " + entries[newId - 1].name);
Serial.println("Phone Number: " + entries[newId - 1].phoneNumber);
Serial.println("Credentials saved!"); // Display "Credentials saved" message
saveEntryToEEPROM(newId - 1, entries[newId - 1]); // Save the new entry to EEPROM
dataInputInProgress = false; // Reset the flag to indicate data input is complete
} else {
Serial.println("No available IDs. Please delete an entry to free up an ID.");
}
}
//with fingerprint
/*
if (addButtonState == LOW && addButtonPrevState == HIGH && !dataInputInProgress) {
int newId = findNextAvailableId(); // Get the next available ID
if (newId != -1) {
// Perform fingerprint verification here
int fingerprintId = verifyFingerprint(); // Get the fingerprint ID
if (fingerprintId != -1) { // Check if fingerprint is verified
Serial.println("Fingerprint verified.");
Serial.println("ID: " + String(newId));
Serial.println("Fingerprint ID: " + String(fingerprintId)); // Display fingerprint ID
Serial.println("Please enter your name:");
while (Serial.available() == 0); // Wait for user input
entries[newId - 1].name = Serial.readStringUntil('\n');
Serial.println("Please enter your phone number:");
while (Serial.available() == 0); // Wait for user input
entries[newId - 1].phoneNumber = Serial.readStringUntil('\n');
entries[newId - 1].deleted = false; // Entry is not deleted
lastId = newId; // Update the last used ID
Serial.println("Name: " + entries[newId - 1].name);
Serial.println("Phone Number: " + entries[newId - 1].phoneNumber);
Serial.println("Credentials saved!"); // Display "Credentials saved" message
saveEntryToEEPROM(newId - 1, entries[newId - 1]); // Save the new entry to EEPROM
dataInputInProgress = false; // Reset the flag to indicate data input is complete
} else {
Serial.println("Fingerprint not verified. Access denied.");
}
} else {
Serial.println("No available IDs. Please delete an entry to free up an ID.");
}
}
*/
// Read the "Lookup" button state
lookupButtonState = digitalRead(lookupButtonPin);
// Check if the "Lookup" button is pressed
if (lookupButtonState == LOW && lookupButtonPrevState == HIGH && !deleteInProgress) {
Serial.println("Please enter the ID you want to lookup:");
while (Serial.available() == 0); // Wait for user input
int lookupId = Serial.parseInt();
if (lookupId > 0 && lookupId <= maxEntries && !entries[lookupId - 1].deleted && entries[lookupId - 1].name != "" && entries[lookupId - 1].phoneNumber != "") {
Serial.println("ID: " + String(lookupId));
Serial.println("Name: " + entries[lookupId - 1].name);
Serial.println("Phone Number: " + entries[lookupId - 1].phoneNumber);
} else {
Serial.println("No data found for the specified ID.");
}
}
// Read the "Delete" button state
deleteButtonState = digitalRead(deleteButtonPin);
// Check if the "Delete" button is pressed
if (deleteButtonState == LOW && deleteButtonPrevState == HIGH && !dataInputInProgress) {
Serial.println("Please enter the ID you want to delete:");
while (Serial.available() == 0); // Wait for user input
int deleteId = Serial.parseInt();
if (deleteId > 0 && deleteId <= maxEntries) {
if (!entries[deleteId - 1].deleted && entries[deleteId - 1].name != "" && entries[deleteId - 1].phoneNumber != "") {
Serial.println("ID: " + String(deleteId));
Serial.println("Name: " + entries[deleteId - 1].name);
Serial.println("Phone Number: " + entries[deleteId - 1].phoneNumber);
Serial.println("Are you sure you want to delete this entry? (Y/N)");
char confirm;
do {
while (Serial.available() == 0); // Wait for user input
confirm = Serial.read();
} while (confirm != 'Y' && confirm != 'y' && confirm != 'N' && confirm != 'n');
if (confirm == 'Y' || confirm == 'y') {
entries[deleteId - 1].deleted = true; // Mark entry as deleted
Serial.println("Entry deleted.");
saveEntryToEEPROM(deleteId - 1, entries[deleteId - 1]); // Save the updated entry to EEPROM
dataInputInProgress = false; // Reset the flag to allow further data input
} else {
Serial.println("Deletion canceled.");
dataInputInProgress = false; // Reset the flag to allow further data input
}
} else {
Serial.println("Nothing to delete here.");
dataInputInProgress = false; // Reset the flag to allow further data input
}
} else {
Serial.println("ID not found.");
dataInputInProgress = false; // Reset the flag to allow further data input
}
}
/*
if (deleteButtonState == LOW && deleteButtonPrevState == HIGH && !dataInputInProgress) {
Serial.println("Please enter the ID you want to delete:");
while (Serial.available() == 0); // Wait for user input
int deleteId = Serial.parseInt();
if (deleteId > 0 && deleteId <= maxEntries) {
if (!entries[deleteId - 1].deleted && entries[deleteId - 1].name != "" && entries[deleteId - 1].phoneNumber != "") {
Serial.println("ID: " + String(deleteId));
Serial.println("Name: " + entries[deleteId - 1].name);
Serial.println("Phone Number: " + entries[deleteId - 1].phoneNumber);
Serial.println("Are you sure you want to delete this entry? (Y/N)");
char confirm;
do {
while (Serial.available() == 0); // Wait for user input
confirm = Serial.read();
} while (confirm != 'Y' && confirm != 'y' && confirm != 'N' && confirm != 'n');
if (confirm == 'Y' || confirm == 'y') {
// Retrieve the associated fingerprint ID
int fingerprintId = getFingerprintIdForEntry(deleteId);
// Delete the entry data
entries[deleteId - 1].deleted = true; // Mark entry as deleted
Serial.println("Entry deleted.");
saveEntryToEEPROM(deleteId - 1, entries[deleteId - 1]); // Save the updated entry to EEPROM
// Delete the fingerprint template associated with the fingerprint ID
deleteFingerprintTemplate(fingerprintId);
dataInputInProgress = false; // Reset the flag to allow further data input
} else {
Serial.println("Deletion canceled.");
dataInputInProgress = false; // Reset the flag to allow further data input
}
} else {
Serial.println("Nothing to delete here.");
dataInputInProgress = false; // Reset the flag to allow further data input
}
} else {
Serial.println("ID not found.");
dataInputInProgress = false; // Reset the flag to allow further data input
}
}
*/
addButtonPrevState = addButtonState; // Store the current "Add" button state for comparison
lookupButtonPrevState = lookupButtonState; // Store the current "Lookup" button state for comparison
deleteButtonPrevState = deleteButtonState; // Store the current "Delete" button state for comparison
}
// Function to find the next available ID
int findNextAvailableId() {
for (int i = 0; i < maxEntries; i++) {
if (entries[i].deleted || entries[i].name == "" || entries[i].phoneNumber == "") {
return i + 1; // Return the first deleted or empty ID
}
}
return -1; // No available IDs
}
// Function to save an entry to EEPROM
void saveEntryToEEPROM(int index, Entry entry) {
int addr = index * entrySize;
EEPROM.put(addr, entry);
EEPROM.commit();
}
// Function to load entries from EEPROM
void loadEntriesFromEEPROM() {
for (int i = 0; i < maxEntries; i++) {
Entry entry;
int addr = i * entrySize;
EEPROM.get(addr, entry);
entries[i] = entry;
}
}