/*The given code is a basic implementation of electronic voting machine .The design given below is for three teams but can be further extended for more teams
In this code very time a voter castes his vote the response is recored and a new session begins where another voter can cast his vote.
the whole thing ends when the final offical encharge presses the stop button for ending the voting procedure and displaying the winner
In this code few isr's have been used for button triggers*/
/* THE CIRCUIT:
==========================================
LCD pin Connect to
------------------------------------------
01 - GND GND, pot
02 - VCC +5V, pot
03 - Contrast Pot wiper
04 - RS Pin8 (P2.0)
05 - R/W GND
06 - EN Pin9 (P2.1)
07 - DB0 GND
08 - DB1 GND
09 - DB2 GND
10 - DB3 GND
11 - DB4 Pin10 (P2.2)
12 - DB5 Pin11 (P2.3)
13 - DB6 Pin12 (P2.4)
14 - DB7 Pin13 (P2.5)
15 - BL+ +5V
16 - BL- GND
===========================================
==============================================
RESISTOR Connect to
---------------------------------------------
R1 GND
R1 push_1(1)
R2 GND
R2 push_2(1)
R3 GND
R3 push_3(1)
R4 GND
R4 push_4(1)
================================================
================================================
PUSH BUTTON Connect to
------------------------------------------------
PushButton(pullup)-push_1(1) P1.0
PushButton(pullup)-push_2 (1) P1.1
PushButton(pullup)-push_3 (1) p1.4
PushButton(pullup)-push_4 (1) p1.5
PushButton(pullup)-push_1 (2) vcc
PushButton(pullup)-push_2 (2) vcc
PushButton(pullup)-push_3 (2) vcc
PushButton(pullup)-push_4 (2) vcc
================================================
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5);
volatile int flag1 = HIGH;//flag counter for stop button
volatile int flag2 = HIGH;// flag counter for vote
volatile int count[4] = {0,0,0,0};
int stop_button = 5;//p1.3
int party1_button = 7;//p1.5
int party2_button = 2;//p1.0
int party3_button = 6;//p1.4
int temp = 0;
volatile int index;
String party[3]={"party1" ,"party2","party3"};
int i=0;
int j=0,q;
char s[2];
int n=0;
volatile int c=0;
String result_f="party ";
void setup()
{ lcd.begin(16,2);
//All buttons acting pullup
pinMode(stop_button, INPUT_PULLUP);
pinMode(party1_button, INPUT_PULLUP);
pinMode(party2_button, INPUT_PULLUP);
pinMode(party3_button, INPUT_PULLUP);
//all button interrupt serivce routines initialsed on falling edges
attachInterrupt(stop_button, stop1, FALLING);
attachInterrupt(party1_button, poll_1, FALLING);
attachInterrupt(party2_button, poll_2, FALLING);
attachInterrupt(party3_button, poll_3, FALLING);
}
//the function itoi converts the values of integer to display on the lcd
void itoi(int n, char s[])
{
s[2]=' ';
do{
s[i++] = n%10 + '0';
}
while((n=n/10)>0);
char c;
for(q=0,j=i-1;q<j;q++,j--)
{
c= s[q];
s[q]=s[j];
s[j]=c;
}
}
//loop function iterrates itself throughout the code
void loop()
{
lcd.clear();
lcd.print("Welcome Voter");
delay(1000);
lcd.clear();
lcd.print("Cast your vote");
delay(1000);
lcd.clear();
//the condition for new voter
while((flag1==HIGH)&&(flag2==HIGH))
{ lcd.print("Waiting....");
delay(200);
lcd.clear();
}
//when the stop button is pressed
if(flag1==LOW)
result();
//when the voter has casted vote
else if((flag1==HIGH)&&(flag2==LOW))
{ //switch case which tells the voter which party he has opted for
switch(c)
{
case 1:
lcd.print("Thank you!");
delay(2000);
lcd.clear();
lcd.print("Voted for party1");
delay(5000);
lcd.clear();
break;
case 2:
/*s[2]=' ';
itoi(count[1],s);
lcd.print(s);
delay(2000);
lcd.clear();*/
lcd.print("Thank you!");
delay(1000);
lcd.clear();
lcd.print("Voted for party2");
delay(5000);
lcd.clear();
break;
case 3:
lcd.print("Thank you!");
delay(1000);
lcd.clear();
lcd.print("Voted for party3");
delay(5000);
lcd.clear();
break;
case 4:
lcd.print("Thank you!");
delay(1000);
lcd.clear();
lcd.print("Voted for party4");
delay(5000);
lcd.clear();
break;
case 0:
lcd.print("No Votes");
delay(1000);
lcd.clear();
}
flag2=HIGH;
}
}
//follwing fuctions of poll_x type save the response for x party and turn of a flag indicting the vote has been casted
void poll_1()
{ count[0]=count[0]+1;//recording response
flag2= LOW;//flag
c=1;}
void poll_2()
{ count[1]=count[1]+1;
flag2= LOW;
c=2;}
void poll_3()
{ count[2]=count[2]+1;
flag2= LOW;
c=3;}
void poll_4()
{ count[3]=count[3]+1;
flag2= LOW;
c=4;}
//the stop function linked with stop button isr
void stop1()
{flag1= LOW;}
//result function called when loop function encounters that stop was pressed
void result()
{ lcd.clear();
lcd.print("Results:");
delay(500);
lcd.clear();
//the calculation below finds the largest number ie winner
temp=count[0];
index=0;
for(i=1;i<3;i++)
{
if(count[i]>temp)
{temp= count[i];
index=i;}
}
index=index+1;
//display of results
lcd.print("*Winner*");
delay(3000);
lcd.clear();
s[2]=' ';
itoi(index,s);
result_f = result_f + s;
lcd.print(result_f);
delay(10000);
lcd.clear();
flag1=HIGH;
for(i=0;i<3;i++)
count[i] = 0;
s[2]=' ';
result_f="party";
}
Comments