Skip to content

Instantly share code, notes, and snippets.

@prasant1010
Last active September 25, 2016 16:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save prasant1010/dd6e35c76b9b0990a0dece32c4645a14 to your computer and use it in GitHub Desktop.
#ifndef ButtonPress
#define ButtonPress
#include <avr/io.h>
char ButtonPressed(int buttonNumber, unsigned char pinOfButton, int portBit, int confidenceLevel);
char Pressed[numberOfButtons];
int Pressed_Confidence_Level[numberOfButtons]; //Measure button press cofidence
int Released_Confidence_Level[numberOfButtons]; //Measure button release confidence
char ButtonPressed(int buttonNumber, unsigned char pinOfButton,int portBit, int confidenceLevel)
{
if (bit_is_clear(pinOfButton, portBit))
{
Pressed_Confidence_Level[buttonNumber] ++; //Increase Pressed Conficence
Released_Confidence_Level[buttonNumber] = 0; //Reset released button confidence since there is a button press
if (Pressed_Confidence_Level[buttonNumber] > confidenceLevel) //Indicator of good button press
{
if (Pressed[buttonNumber] == 0)
{
Pressed[buttonNumber] = 1;
return 1;
}
//Zero it so a new pressed condition can be evaluated
Pressed_Confidence_Level[buttonNumber] = 0;
}
}
else
{
Released_Confidence_Level[buttonNumber] ++; //This works just like the pressed
Pressed_Confidence_Level[buttonNumber] = 0; //Reset pressed button confidence since the button is released
if (Released_Confidence_Level[buttonNumber] > confidenceLevel)
{
Pressed[buttonNumber] = 0;
Released_Confidence_Level[buttonNumber] = 0;
}
}
return 0;
}
#endif
/*written by prasant
this is simple voting machine if you cut off power all the data will be lost
so this project needs continuous power source
*/
#define F_CPU 1000000UL
#define numberOfButtons 8
#include <avr/io.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd.h"
#include "ButtonPress.h"
void candidate_selection();
int main(void)
{
unsigned char george_count=0,obama_count=0,clinton_count=0,trump_count=0;
char obama[16],george[16],clinton[16],trump[16];
DDRC=0x00;
PORTC=0xff;
lcd_init(LCD_DISP_ON_CURSOR_BLINK);
candidate_selection();
while(1)
{
if (ButtonPressed(0,PINC,0,100))
{
lcd_clrscr();
lcd_puts("you voted for ");
lcd_gotoxy(2,1);
lcd_puts("OBAMA");
obama_count++;
}
if (ButtonPressed(1,PINC,1,100))
{
lcd_clrscr();
lcd_puts("you voted for ");
lcd_gotoxy(2,1);
lcd_puts("GEORGE");
george_count++;
}
if (ButtonPressed(2,PINC,2,100))
{
lcd_clrscr();
lcd_puts("you voted for ");
lcd_gotoxy(2,1);
lcd_puts("CLINTON");
clinton_count++;
}
if (ButtonPressed(3,PINC,3,100))
{
lcd_clrscr();
lcd_puts("you voted for ");
lcd_gotoxy(2,1);
lcd_puts("TRUMP");
trump_count++;
}
if (ButtonPressed(4,PINC,4,100))
{
if ((obama_count)>(george_count)&&(obama_count)>(clinton_count)&&(obama_count)>(trump_count))
{
itoa(obama_count,obama,10);
lcd_clrscr();
lcd_puts(obama);
lcd_gotoxy(2,1);
lcd_puts("winner obama !");
lcd_gotoxy(1,3);
lcd_puts("votes");
}
else if ((george_count)>(obama_count)&&(george_count)>(clinton_count)&&(george_count)>(trump_count))
{
itoa(george_count,george,10);
lcd_clrscr();
lcd_puts(george);
lcd_gotoxy(2,1);
lcd_puts("winner george!");
}
else if ((clinton_count)>(obama_count)&&(clinton_count)>(george_count)&&(clinton_count)>(trump_count))
{
itoa(clinton_count,clinton,10);
lcd_clrscr();
lcd_puts(clinton);
lcd_gotoxy(2,1);
lcd_puts("winner clinton !");
}
else if ((trump_count)>(obama_count)&&(trump_count)>(george_count)&&(trump_count)>(clinton_count))
{
itoa(trump_count,trump,10);
lcd_clrscr();
lcd_puts(trump);
lcd_gotoxy(2,1);
lcd_puts("winner trump !");
}
else
{
lcd_clrscr();
lcd_puts("all equal vote !");
lcd_gotoxy(2,1);
lcd_puts("electronify win");
}
}
}
}
/*function definations */
void candidate_selection()
{
lcd_puts("please select");
lcd_gotoxy(2,1);
lcd_puts(" your candidate");
_delay_ms(1500);
lcd_clrscr();
lcd_puts("1obama 2george");
lcd_gotoxy(2,1);
lcd_puts("3clinton 4trump");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment