I'm working on Fingerprint voting system using: LCD (with I2C), 3 buttons (2 buttons for selecting candidates/options, and 1 button for showing resaults), fingerprint sensor R307, and buzzer.
System should work on principe of selecting one of two buttons (voting), then put their finger on fingerprint in order to confirm their vote. It also would display you can't vote twice if you try voting with same finger again.
Problem is:
The system accepts ony the first fingerprint, and stores it. But when I try putting on another finger, it's not working and doesn't accept it.
I think the problem is in code and storing.
Here is the code:
#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Fingerprint.h>
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Fingerprint sensor setup (SoftwareSerial)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
// Button pins
const int redButtonPin = 11; // Candidate 1
const int yellowButtonPin = 12; // Candidate 2
const int whiteButtonPin = 10; // Results/Back button
const int buzzerPin = 13; // Buzzer
// Variables
int candidate1Votes = 0;
int candidate2Votes = 0;
int selectedCandidate = 0;
bool inResultsMode = false;
// Fingerprint IDs of registered voters (you'll need to enroll these separately)
uint8_t registeredVoters[] = {1, 2, 3, 4, 5}; // Example IDs
const int numVoters = sizeof(registeredVoters) / sizeof(registeredVoters[0]);
bool hasVoted[numVoters] = {false}; // Tracks who has voted
void saveVotes() {
EEPROM.put(0, candidate1Votes); // Address 0-3
EEPROM.put(4, candidate2Votes); // Address 4-7
}
void loadVotes() {
EEPROM.get(0, candidate1Votes);
EEPROM.get(4, candidate2Votes);
}
void setup() {
// Initialize components
pinMode(redButtonPin, INPUT_PULLUP);
pinMode(whiteButtonPin, INPUT_PULLUP);
pinMode(whiteButtonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize serial for debugging
Serial.begin(9600);
// Set the data rate for the fingerprint sensor
finger.begin(57600);
// Welcome message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Voting Machine");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000);
showCandidateSelection();
}
void loop() {
if (inResultsMode) {
// In results display mode
if (digitalRead(whiteButtonPin) == LOW) {
delay(200); // Debounce
inResultsMode = false;
showCandidateSelection();
}
} else {
// Normal voting mode
if (selectedCandidate == 0) {
// No candidate selected yet
if (digitalRead(redButtonPin) == LOW) {
selectCandidate(1);
} else if (digitalRead(whiteButtonPin) == LOW) {
selectCandidate(2);
} else if (digitalRead(whiteButtonPin) == LOW) {
delay(200); // Debounce
showResults();
}
} else {
// Candidate selected, waiting for fingerprint
checkFingerprint();
}
}
}
void showCandidateSelection() {
selectedCandidate = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please select");
lcd.setCursor(0, 1);
lcd.print("Candidate: 1 - 2");
}
void selectCandidate(int candidate) {
selectedCandidate = candidate;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Candidate");
lcd.setCursor(0, 1);
lcd.print("Selected");
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please put your");
lcd.setCursor(0, 1);
lcd.print("finger to vote.");
}
void checkFingerprint() {
// Get fingerprint
int fingerprintID = getFingerprintIDez();
if (fingerprintID > 0) {
bool isRegistered = false;
int voterIndex = -1;
// Check if fingerprint is registered
for (int i = 0; i < numVoters; i++) {
if (registeredVoters[i] == fingerprintID) {
isRegistered = true;
voterIndex = i;
break;
}
}
if (!isRegistered) {
// Fingerprint not registered
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Not registered!");
lcd.setCursor(0, 1);
lcd.print("Contact admin.");
tone(buzzerPin, 500, 1000);
delay(2000);
selectedCandidate = 0;
showCandidateSelection();
return;
}
// Handle registered voter
if (hasVoted[voterIndex]) {
// Already voted
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Already voted!");
lcd.setCursor(0, 1);
lcd.print("Please leave.");
// Sound alarm
for (int j = 0; j < 3; j++) {
tone(buzzerPin, 1000, 200);
delay(300);
}
delay(2000);
selectedCandidate = 0;
showCandidateSelection();
} else {
// First time voting
hasVoted[voterIndex] = true;
// Record vote
if (selectedCandidate == 1) {
candidate1Votes++;
} else {
candidate2Votes++;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Vote stored!");
lcd.setCursor(0, 1);
lcd.print("Thank you!");
// Success beep
tone(buzzerPin, 1500, 300);
delay(2000);
selectedCandidate = 0;
showCandidateSelection();
}
} else if (fingerprintID == 0) {
// Fingerprint not recognized (but finger detected)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Unrecognized");
lcd.setCursor(0, 1);
lcd.print("Try again...");
tone(buzzerPin, 300, 500);
delay(1000);
// Return to fingerprint prompt
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please put your");
lcd.setCursor(0, 1);
lcd.print("finger to vote.");
}
// If fingerprintID == -1 (no finger), do nothing
}
void showResults() {
inResultsMode = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Cnd1: ");
lcd.print(candidate1Votes);
lcd.setCursor(0, 1);
lcd.print("Cnd2: ");
lcd.print(candidate2Votes);
}
// Returns -1 if failed, 0 if no finger, or the fingerprint ID if detected
int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return 0;
// Found a match!
return finger.fingerID;
}