#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
// LCD Initialization
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust the address if necessary
// Pin Definitions
const int ballotButtonPin = 32;
const int resetButtonPin = 33;
const int partyButtonPins[4] = {25, 26, 27, 14};
const int ledPins[5] = {12, 13, 15, 16, 17};
const int buzzerPin = 18;
// Variables
int voteCount[4] = {0, 0, 0, 0}; // Vote counters for 4 parties
bool votingEnabled = false;
unsigned long resetPressStartTime = 0;
String voteLog = "";
int voteSequenceNumber = 1; // Initialize sequence number
void setup() {
// Initialize Serial, LCD, and EEPROM
Serial.begin(115200);
Wire.begin(21, 22); // Initialize I2C with SDA and SCL pins
lcd.init(); // Initialize the LCD
lcd.begin(20, 4); // Corrected to include columns and rows
lcd.backlight();
EEPROM.begin(512);
// Initialize Pins
pinMode(ballotButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
for (int i = 0; i < 4; i++) pinMode(partyButtonPins[i], INPUT_PULLUP);
for (int i = 0; i < 5; i++) pinMode(ledPins[i], OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Load Vote Count from EEPROM
for (int i = 0; i < 4; i++) {
voteCount[i] = EEPROM.read(i);
if (voteCount[i] == 255) {
voteCount[i] = 0; // Initialize to 0 if EEPROM is uninitialized
}
}
// Display Startup Banner
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome to EVM");
lcd.setCursor(0, 1);
lcd.print("Futuristic System");
delay(3000); // Reduced delay for testing purposes
updateVoteCountDisplay();
}
void loop() {
if (digitalRead(ballotButtonPin) == LOW) {
enableVoting();
}
if (votingEnabled) {
handleVoting();
}
handleReset();
handleSerialInput();
}
// Function to Enable Voting
void enableVoting() {
digitalWrite(ledPins[0], HIGH);
tone(buzzerPin, 1000, 100);
votingEnabled = true;
lcd.clear();
delay(500); // Debounce
}
// Function to Handle Voting
void handleVoting() {
for (int i = 0; i < 4; i++) {
if (digitalRead(partyButtonPins[i]) == LOW) {
voteCount[i]++;
digitalWrite(ledPins[i + 1], HIGH);
tone(buzzerPin, 1000, 200);
delay(200);
tone(buzzerPin, 1000, 200);
lcd.clear();
lcd.print("You voted for:");
lcd.setCursor(0, 1);
lcd.print("Party ");
lcd.print((char)('A' + i));
logVote(i);
delay(5000);
updateVoteCountDisplay();
votingEnabled = false;
delay(500); // Debounce
// Turn off all LEDs after voting
for (int j = 0; j < 5; j++) {
digitalWrite(ledPins[j], LOW);
}
break;
}
}
}
// Function to Update Vote Count on LCD
void updateVoteCountDisplay() {
lcd.clear();
for (int i = 0; i < 4; i++) {
lcd.setCursor(0, i);
lcd.print("Party ");
lcd.print((char)('A' + i));
lcd.print(": ");
lcd.print(voteCount[i]);
}
}
// Function to Log Votes
void logVote(int partyIndex) {
voteLog += String(voteSequenceNumber) + ": Party " + (char)('A' + partyIndex) + "\n";
voteSequenceNumber++; // Increment sequence number
Serial.println("Vote Logs:");
Serial.print(voteLog);
// Save Vote Count to EEPROM
for (int i = 0; i < 4; i++) {
EEPROM.write(i, voteCount[i]);
}
EEPROM.commit();
}
// Function to Handle System Reset
void handleReset() {
if (digitalRead(resetButtonPin) == LOW) {
if (resetPressStartTime == 0) {
resetPressStartTime = millis(); // Start timing
Serial.println("Reset button pressed, starting timer...");
} else if (millis() - resetPressStartTime >= 30000) {
Serial.println("Reset button pressed for 30 seconds, resetting system...");
resetSystem();
}
} else {
if (resetPressStartTime != 0) {
Serial.println("Reset button released before 30 seconds, resetting timer...");
}
resetPressStartTime = 0; // Reset the timer if the button is released
}
}
// Function to Reset System
void resetSystem() {
for (int i = 0; i < 4; i++) {
voteCount[i] = 0;
EEPROM.write(i, voteCount[i]);
}
EEPROM.commit();
voteLog = "";
voteSequenceNumber = 1; // Reset sequence number
lcd.clear();
lcd.print("System Reset");
delay(2000);
updateVoteCountDisplay();
digitalWrite(ledPins[0], LOW);
for (int i = 1; i < 5; i++) {
digitalWrite(ledPins[i], LOW);
}
}
// Function to Handle Serial Input
void handleSerialInput() {
if (Serial.available() > 0) {
char input = Serial.read();
if (input == 'l') {
Serial.println("Vote Logs:");
Serial.print(voteLog);
}
}
}