Skip to content

Instantly share code, notes, and snippets.

@Ajak58a
Created March 2, 2024 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ajak58a/c7ecdb74344884afe22070170bb44613 to your computer and use it in GitHub Desktop.
Save Ajak58a/c7ecdb74344884afe22070170bb44613 to your computer and use it in GitHub Desktop.
Arduino based Biometric Voting Machine voting system code
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include "TouchScreen.h"
#include<EEPROM.h>
#include <Adafruit_Fingerprint.h>
#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)
SoftwareSerial mySerial(2, 3);
#else
#define mySerial Serial1
#endif
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
int vote1, vote2, vote3;
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
MCUFRIEND_kbv tft;
#define MINPRESSURE 10
#define MAXPRESSURE 1000
const int XP=6,XM=A2,YP=A1,YM=7; //320x480 ID=0x9486
const int TS_LEFT=931,TS_RT=168,TS_TOP=452,TS_BOT=191;
#define TS_MINX 125
#define TS_MINY 85
#define TS_MAXX 965
#define TS_MAXY 905
TouchScreen ts(XP, YP, XM, YM, 300); //re-initialised after diagnose
TSPoint tp; //global point
void setup(void) {
tft.reset();
tft.begin(tft.readID());
tft.setRotation(2);
Serial.begin(9600);
Serial.println();
Serial.print("reading id...");
delay(500);
Serial.println(tft.readID(), HEX);
finger.begin(57600);
delay(5);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
}
else {
Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
tft.fillScreen(BLACK);
Home();
pinMode(13, OUTPUT);
result();
}
bool fingerprintMatch(){
screen1();
delay(5000);
uint8_t p = finger.getImage();
if (p == FINGERPRINT_NOFINGER) goto NoFinger;
else if (p == FINGERPRINT_OK) {
p = finger.image2Tz();
if (p != FINGERPRINT_OK) goto NoMatch;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) goto NoMatch;
Serial.print("Verified ID:");
Serial.println(finger.fingerID);
delay(1500);
return true;
}
NoMatch:
{
return false;
}
NoFinger:
{
Serial.println("No finger detected");
delay(1500);
return false;
}
}
void Home()
{
tft.fillScreen(BLACK);
tft.drawRoundRect(0, 0, 320, 480, 8, WHITE); //Page border
tft.setCursor(105, 5);
tft.setTextSize(3);
tft.setTextColor(CYAN);
tft.print("Voting");
tft.setCursor(97, 29);
tft.print("Machine");
tft.drawRoundRect(35, 70, 250, 50, 8, WHITE); //Vote
tft.fillRoundRect(35, 70, 250, 50, 8, YELLOW);
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.setCursor(60, 82);
tft.print("Candidate 1");
tft.drawRoundRect(35, 160, 250, 50, 8, WHITE); //Enroll
tft.fillRoundRect(35, 160, 250, 50, 8, YELLOW);
tft.setCursor(60, 172);
tft.print("Candidate 2");
tft.drawRoundRect(35, 250, 250, 50, 8, WHITE);
tft.fillRoundRect(35, 250, 250, 50, 8, YELLOW); //Result
tft.setCursor(60, 262);
tft.print("Candidate 3");
}
void screen1()
{
tft.fillScreen(BLACK);
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.setCursor(70, 190);
tft.print("Please Scan");
tft.setCursor(70, 240);
tft.print("Your Finger");
Serial.println("Please press finger");
}
void screen2()
{
Serial.println("Finger not found");
tft.fillScreen(BLACK);
tft.setTextSize(3);
tft.setCursor(70, 190);
tft.print("Sorry You");
tft.setCursor(70, 240);
tft.print("Can't Vote");
delay(3000);
Home();
}
void loop(){
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
TSPoint p = ts.getPoint();
// if sharing pins, you'll need to fix the directions of the touchscreen pins
//pinMode(XP, OUTPUT);
//pinMode(YM, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > ts.pressureThreshhold)
{
p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
p.y = map(p.y, TS_MAXY, TS_MINY, 0, 240);
//Serial.print("X:"); // I used this to get the accurate touch points for X and Y axis
//Serial.print(p.x);
//Serial.print(" ");
//Serial.print("Y:");
//Serial.println(p.y);
if (p.x > 50 && p.x < 280 && p.y > 160 && p.y < 190 && p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
Serial.println("Candidate 1");
if(fingerprintMatch())
{
vote1++;
EEPROM.write(0, vote1);
tft.fillScreen(BLACK);
tft.setCursor(70, 220);
tft.print("Thank You");
delay(3000);
Home();
}
else
{
screen2();
}
}
if (p.x > 50 && p.x < 280 && p.y > 110 && p.y < 140)
{
Serial.println("Candidate 2");
if(fingerprintMatch())
{
vote2++;
EEPROM.write(0, vote2);
tft.fillScreen(BLACK);
tft.setCursor(70, 220);
tft.print("Thank You");
delay(3000);
Home();
}
else
{
screen2();
}
}
if (p.x > 50 && p.x < 280 && p.y > 60 && p.y < 90)
{
Serial.println("Candidate 3");
if(fingerprintMatch())
{
vote3++;
EEPROM.write(0, vote3);
tft.fillScreen(BLACK);
tft.setCursor(70, 220);
tft.print("Thank You");
delay(3000);
Home();
}
else
{
screen2();
}
}
}
}
void result()
{
vote1=EEPROM.read(0);
vote2=EEPROM.read(1);
vote3=EEPROM.read(2);
int vote=vote1+vote2+vote3;
Serial.println(vote1);
Serial.println(vote2);
Serial.println(vote3);
if(vote)
{
if((vote1 > vote2 && vote1 > vote3))
{
Serial.print("Candidate 1 Wins");
delay(2000);
}
else if(vote2 > vote1 && vote2 > vote3)
{
Serial.print("Candidate 2 Wins");
delay(2000);
}
else if((vote3 > vote1 && vote3 > vote2))
{
Serial.print("Candidate 3 Wins");
delay(2000);
}
else
{
Serial.print("Tie Up Or");
Serial.print("No Result");
delay(1000);
}
}
else
{
Serial.print("No Voting....");
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment