Introduction: Arduino Voting

Connect LCD and keypad (please note keypad pins are not the same)

Step 1: Upload Code to Arduino and You're Done

#include

// initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); int user_n; int num; int iii=0;

int n=1; int vote[6]={0,0,0,0,0,0}; int flaggg=0; int flagg=5; int flaggy=0; int flagggg=0; int pbutton2=0; int m; int nn=0; int nine=9; #include int key=15; int key2=14; int pbutton=0;//for f;lag const byte ROWS = 4; //four rows const byte COLS = 3; //three columns char keys[ROWS][COLS] = { {'A','B','C'}, {'D','E','F'}, {'G','H','I'}, {'K','L','M'} };

const byte ROWS2 = 4; //four rows const byte COLS2 = 3; //three columns char keys2[ROWS2][COLS2] = { {'N','O','P'}, {'Q','R','S'}, {'T','U','W'}, {'*','>','<'}, }; byte rowPins[ROWS] = {41, 43, 45, 47}; //connect to the row pinouts of the keypad byte colPins[COLS] = {49, 51, 53}; //connect to the column pinouts of the keypad

byte rowPins2[ROWS2] = {22, 24, 26, 28}; //connect to the row pinouts of the keypad byte colPins2[COLS2] = {30, 32, 34};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );//Main function to start keyboard Keypad keypad2 = Keypad( makeKeymap(keys2), rowPins2, colPins2, ROWS2, COLS2 );

void setup(){//runs only once pinMode(key,INPUT); pinMode(key2,INPUT); pinMode(nine, OUTPUT);//set contrast of lcd analogWrite(nine,370);

Serial.begin(9600); } char name[5][20];//double array for name of candidtes

void enter() { char key = keypad.getKey(); char key2 = keypad2.getKey();

if (key){ Serial.println(key); pbutton=5; } if(key2){ Serial.println(key2); pbutton=5; }

} void setupdisplay() { lcd.begin(8, 2); lcd.setCursor(2,0); lcd.print("Candidtes Not "); // lcd.begin(8, 2); lcd.setCursor(5,1); lcd.print("Added!");

delay(1000);// STOP INRO 1 lcd.begin(8, 2); lcd.setCursor(3,0); lcd.print("Press Any"); // lcd.begin(8, 2); lcd.setCursor(5,1); lcd.print("Button"); delay(1000);// STOP INRO 1 enter();

}

int numberofusers() { char keyb22='a'; int n=0; if(flaggg==0) { n=0; flaggg=1; } while(n!=1000) { lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("Enter Number of "); // lcd.begin(8, 2); lcd.setCursor(0,1); lcd.print("Candidates:"); lcd.setCursor(12,1); lcd.print(n); keyb22=keyb2(); m=n; num=n; if(keyb22=='*') {n=1000; } if(keyb22=='<' && n!=0) {n=n-1; } if(keyb22 =='>' && n!=5) {n=n+1; } delay(100);

}

lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("You have choosen "); // lcd.begin(8, 2); lcd.setCursor(0,1); lcd.print(m); lcd.setCursor(4,1); lcd.print("Candidates "); delay(1000); pbutton2=9;

return(m);

}

char keyb() {

char key = keypad.getKey(); if (key){ Serial.println(key); pbutton=5; } return(key);

}

char keyb2() { char key2 = keypad2.getKey();

if(key2){ Serial.println(key2); pbutton=5; } return(key2);

}

void error() {pbutton2=0; lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("Error!"); // lcd.begin(8, 2); lcd.setCursor(0,1); lcd.print("Restarting!"); delay(2000); setupdisplay();

}

void reset() { pbutton=0; pbutton2=0; setupdisplay();

}

void entername() { int n=0,m=0; if(user_n==0 && flagggg==0) { error(); } flagggg=5;

while(user_n!=0) { char k1='$',k2='$'; lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("Enter Name of "); // lcd.begin(8, 2); lcd.setCursor(0,1); lcd.print("Candidate:"); lcd.setCursor(12,1); lcd.print(user_n); delay(1000); while( k2!='*') { k1='$' ; k2='$'; k1=keyb(); k2=keyb2(); if(k1) { name[user_n][nn]=k1; if(flagg==5) { lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("Name"); lcd.setCursor(6,0); lcd.print(user_n); flagg=0;

} // lcd.begin(8, 2); //lcd.print("Candidate:"); lcd.setCursor(nn,1); lcd.print(name[user_n][nn]); //lcd.print(k1); nn=nn+1;

delay(100);

} else if(k2) {if(k2!='*') name[user_n][nn]=k2;

if(flagg==5) { lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("Name"); lcd.setCursor(6,0); lcd.print(user_n); flagg=0; }

// lcd.begin(8, 2); //lcd.print("Candidate:"); lcd.setCursor(nn,1); lcd.print(name[user_n][nn]); // lcd.print('A');

nn=nn+1; delay(100);

//delay(1000); } } lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("Name Saved!"); delay(1000); user_n=user_n-1; flagg=5; nn=0;

}//END WHILE LOOP FOR NUMBER OF USER ENTRY

systemready();

}

void systemready()

{

if(flaggy==0) { lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("Please choose"); lcd.setCursor(0,1); lcd.print("the candidate"); delay(1000); flaggy=5;} char k2=keyb2(); lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print(name[n][0]); lcd.print(name[n][1]); lcd.print(name[n][2]); lcd.print(name[n][3]); lcd.print(name[n][4]); lcd.print(name[n][5]); lcd.print(name[n][6]); lcd.print(name[n][7]); lcd.print(name[n][8]); lcd.print(name[n][9]); lcd.print(name[n][10]); lcd.print(name[n][11]); lcd.print(name[n][12]); lcd.print(name[n][13]); lcd.print(name[n][14]); lcd.print(name[n][15]);

lcd.print(name[n][16]); delay(100); while( analogRead(key)!=0) { lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("No Permission "); lcd.setCursor(0,1); lcd.print("No Key!!"); delay(1000);

} if(k2){ if(k2=='<' && n

lcd.print(name[n][16]); delay(1000); }

} if(k2=='>' && n>1) {n=n-1; lcd.begin(8, 2); lcd.setCursor(0,1); lcd.print("Press * to choose"); lcd.setCursor(0,0); lcd.print(name[n][0]); lcd.print(name[n][1]); lcd.print(name[n][2]); lcd.print(name[n][3]); lcd.print(name[n][4]); lcd.print(name[n][5]); lcd.print(name[n][6]); lcd.print(name[n][7]); lcd.print(name[n][8]); lcd.print(name[n][9]); lcd.print(name[n][10]); lcd.print(name[n][11]); lcd.print(name[n][12]); lcd.print(name[n][13]); lcd.print(name[n][14]); lcd.print(name[n][15]); lcd.print(name[n][16]); delay(1000); } if(k2=='*' && analogRead(key)==0) { vote[n]=vote[n]+1; lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("Thank you!"); lcd.setCursor(0,1); lcd.print("You have voted!"); delay(1000); systemready();

}

if(k2=='N') {int temp=0; int great; for(int i=0;i<=5;++i) { if(vote[i]>temp){ great=i; temp=vote[i]; }

}//end of for lcd.begin(8, 2); lcd.setCursor(0,0); lcd.print("Winner Votes:"); lcd.setCursor(13,0); lcd.print(temp);

lcd.setCursor(0,1); lcd.print(name[great][0]); lcd.print(name[great][1]); lcd.print(name[great][2]); lcd.print(name[great][3]); lcd.print(name[great][4]); lcd.print(name[great][5]); lcd.print(name[great][6]); lcd.print(name[great][7]); lcd.print(name[great][8]); lcd.print(name[great][9]); lcd.print(name[great][10]); lcd.print(name[great][11]); lcd.print(name[great][12]); lcd.print(name[great][13]); lcd.print(name[great][14]); lcd.print(name[great][15]); lcd.print(name[great][16]);

delay(10000); reset();

}

}

void loop(){ while(pbutton==0){ while(iii==19) {name[1][iii]=' '; iii=iii+1; }

iii=0;

while(iii==19) {name[2][iii]=' '; iii=iii+1; }

iii=0;

while(iii==19) {name[3][iii]=' '; iii=iii+1; }

iii=0; while(iii==19) {name[4][iii]=' '; iii=iii+1; } iii=0;

while(iii==19) {name[5][iii]=' '; iii=iii+1; } setupdisplay(); } while(pbutton2==0) { user_n=numberofusers(); } entername(); }