#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD I2C address
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 rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int voteA = 0;
int voteB = 0;
int voteC = 0;
int voteD = 0;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Voting Machine");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1.Candidates");
lcd.setCursor(0, 1);
lcd.print("2.Vote 3.Results");
}
void loop() {
char customKey = customKeypad.getKey();
if (customKey) {
lcd.clear();
switch (customKey) {
case '1':
showCandidates();
break;
case '2':
castVote();
break;
case '3':
showResults();
break;
}
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1.Candidates");
lcd.setCursor(0, 1);
lcd.print("2.Vote 3.Results");
}
}
void showCandidates() {
lcd.setCursor(0, 0);
lcd.print("A. Saleh");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("B. Hasnat");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("C. Abu Bakr");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D. Umar");
delay(2000);
}
void castVote() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Vote (A/B/C/D):");
char voteKey = customKeypad.waitForKey();
if (voteKey) {
switch (voteKey) {
case 'A':
voteA++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Vote A: ");
lcd.print(voteA);
break;
case 'B':
voteB++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Vote B: ");
lcd.print(voteB);
break;
case 'C':
voteC++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Vote C: ");
lcd.print(voteC);
break;
case 'D':
voteD++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Vote D: ");
lcd.print(voteD);
break;
default:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Vote");
break;
}
delay(2000);
}
}
void showResults() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("A:");
lcd.print(voteA);
lcd.setCursor(8, 0);
lcd.print("B:");
lcd.print(voteB);
lcd.setCursor(0, 1);
lcd.print("C:");
lcd.print(voteC);
lcd.setCursor(8, 1);
lcd.print("D:");
lcd.print(voteD);
delay(5000);
}