#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD, set the I2C address to 0x27 for a 16x2 display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define LED pins
const int led1Pin = 25;
const int led2Pin = 26;
const int led3Pin = 27;
// Define Button pins
const int button1Pin = 32;
const int button2Pin = 33;
const int button3Pin = 34;
// Variables to store vote counts
int modi = 0;
int kejriwal = 0;
int rahul = 0;
// Debounce variables
unsigned long lastDebounceTime[3] = {0, 0, 0}; // For 3 buttons
const unsigned long debounceDelay = 10; // milliseconds
bool buttonState[3] = {HIGH, HIGH, HIGH}; // Last state of buttons
bool lastButtonState[3] = {HIGH, HIGH, HIGH}; // Previous state of buttons
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize LCD
lcd.init();
lcd.backlight();
// Print a message on the LCD
lcd.setCursor(0, 0);
lcd.print("Voting Machine");
// Set LED pins as outputs
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
// Set Button pins as inputs
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
}
void loop() {
// Read the button states
int reading1 = digitalRead(button1Pin);
int reading2 = digitalRead(button2Pin);
int reading3 = digitalRead(button3Pin);
// Check Button 1
if (reading1 != lastButtonState[0]) {
lastDebounceTime[0] = millis(); // reset the debounce timer
}
if ((millis() - lastDebounceTime[0]) > debounceDelay) {
if (reading1 == LOW && buttonState[0] == HIGH) {
modi++;
lcd.setCursor(0, 1);
lcd.print("Mode 1: ");
lcd.print(modi);
digitalWrite(led1Pin, HIGH);
delay(500);
digitalWrite(led1Pin, LOW);
}
buttonState[0] = reading1; // Update the button state
}
// Check Button 2
if (reading2 != lastButtonState[1]) {
lastDebounceTime[1] = millis(); // reset the debounce timer
}
if ((millis() - lastDebounceTime[1]) > debounceDelay) {
if (reading2 == LOW && buttonState[1] == HIGH) {
kejriwal++;
lcd.setCursor(0, 1);
lcd.print("Mode 2: ");
lcd.print(kejriwal);
digitalWrite(led2Pin, HIGH);
delay(500);
digitalWrite(led2Pin, LOW);
}
buttonState[1] = reading2; // Update the button state
}
// Check Button 3
if (reading3 != lastButtonState[2]) {
lastDebounceTime[2] = millis(); // reset the debounce timer
}
if ((millis() - lastDebounceTime[2]) > debounceDelay) {
if (reading3 == LOW && buttonState[2] == HIGH) {
rahul++;
lcd.setCursor(0, 1);
lcd.print("Mode 3: ");
lcd.print(rahul);
digitalWrite(led3Pin, HIGH);
delay(500);
digitalWrite(led3Pin, LOW);
}
buttonState[2] = reading3; // Update the button state
}
// Update the last button states
lastButtonState[0] = reading1;
lastButtonState[1] = reading2;
lastButtonState[2] = reading3;
// Add a small delay to avoid rapid loop execution
delay(100);
}