#include <Keypad.h>
#include <LiquidCrystal.h>

// Define pin numbers for components
const int rgbLedR = 9;      // Red pin of the RGB LED
const int rgbLedG = 10;     // Green pin of the RGB LED
const int rgbLedB = 11;     // Blue pin of the RGB LED
const int redLed = 12;      // Red LED pin
const int buttonPin = 2;    // Button for casting vote
const int buzzerPin = 3;    // Buzzer pin
const int party1Button = 4; // Button for Party 1
const int party2Button = 5; // Button for Party 2
// Add more party buttons as needed
const int passwordButton = 6; // Button for entering passwords
const int passwordLED = 7;     // LED to indicate password entry

// Define LCD screen
LiquidCrystal lcd(8, 7, 6, 5, 4, 3);

// Define keypad
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
Keypad keypad = Keypad( makeKeymap(keys), ROWS, COLS );

// Define variables for vote count
int party1Votes = 0;
int party2Votes = 0;
// Add more variables for other parties as needed

// Flag to indicate if the results are displayed
bool displayResults = false;

void setup() {
  // Initialize components
  pinMode(rgbLedR, OUTPUT);
  pinMode(rgbLedG, OUTPUT);
  pinMode(rgbLedB, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(party1Button, INPUT);
  pinMode(party2Button, INPUT);
  // Initialize more party buttons as needed
  pinMode(passwordButton, INPUT);
  pinMode(passwordLED, OUTPUT);

  // Initialize LCD
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Election Results");

  // Set RGB LED to green (ready for voting)
  digitalWrite(rgbLedR, LOW);
  digitalWrite(rgbLedG, HIGH);
  digitalWrite(rgbLedB, LOW);
}

void loop() {
  // Check if the EVM is active (password entered)
  if (checkPassword()) {
    // Allow voting
    if (digitalRead(buttonPin) == HIGH) {
      castVote();
    }

    // Check for party button presses and increment vote count
    if (digitalRead(party1Button) == HIGH) {
      party1Votes++;
      castVote(); // Activate buzzer and LED for Party 1 vote
    }
    if (digitalRead(party2Button) == HIGH) {
      party2Votes++;
      castVote(); // Activate buzzer and LED for Party 2 vote
    }
    // Add more party buttons as needed
  }

  // Check if password for displaying results is entered
  if (displayResults && displayVotes()) {
    // Display results and prevent further votes
    while (true) {
      // Code to keep results displayed and prevent further voting
    }
  }
}

void castVote() {
  // Turn on the red LED and play a sound
  digitalWrite(redLed, HIGH);
  tone(buzzerPin, 1000, 200); // Play a 1000Hz tone for 200ms
  delay(1000); // Wait for a moment
  digitalWrite(redLed, LOW);
}

bool displayVotes() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Party 1 Votes: ");
  lcd.print(party1Votes);
  lcd.setCursor(0, 1);
  lcd.print("Party 2 Votes: ");
  lcd.print(party2Votes);
  // Add more displays for other parties as needed
  return true;
}

bool checkPassword() {
  // Check if the password is entered correctly
  char password[5] = "1234"; // Change this to your password
  char enteredPassword[5] = "";
  int passwordIndex = 0;

  digitalWrite(passwordLED, LOW); // Turn off password LED

  while (passwordIndex < 4) {
    char key = keypad.getKey();
    if (key) {
      enteredPassword[passwordIndex] = key;
      passwordIndex++;
    }
  }

  if (strcmp(password, enteredPassword) == 0) {
    // Password is correct
    digitalWrite(passwordLED, HIGH); // Turn on password LED
    displayResults = true;
    return true;
  } else {
    // Password is incorrect
    return false;
  }
}
$abcdeabcde151015202530354045505560fghijfghij
$abcdeabcde151015202530354045505560fghijfghij