#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <ezButton.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH,SCREEN_HEIGHT,&Wire,-1);
// button objects
ezButton button1(13);
ezButton button2(33);
ezButton button3(14);
ezButton button4(27);
ezButton button5(26);
int vote1 = 0;
int vote2 = 0;
int vote3 = 0;
int vote4 = 0;
int voting_completed = 0;
void setup() {
Serial.begin(9600);
button1.setDebounceTime(25);
button2.setDebounceTime(25);
button3.setDebounceTime(25);
button4.setDebounceTime(25);
button5.setDebounceTime(25);
// Initlasize oled display with i2c address ox3c
if(!oled.begin(SSD1306_SWITCHCAPVCC,0x3c)){
Serial.println(F("SSD1306 allocarion fialed"));
for(;;);
}
delay(2000);
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(2,3);
oled.println("Start Voting...");
oled.setCursor(2,15);
oled.println("A - Green");
oled.setCursor(2,27);
oled.println("B - Yellow");
oled.setCursor(2,39);
oled.println("C - Red");
oled.setCursor(2,51);
oled.println("D - Blue");
oled.display();
delay(5000);
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(2,3);
oled.println("Begin...");
oled.display();
}
void loop() {
button1.loop();
button2.loop();
button3.loop();
button4.loop();
button5.loop();
if (voting_completed == 0) {
if (button1.isPressed()) {
vote1++;
}
else if (button2.isPressed()) {
vote2++;
}
else if (button3.isPressed()) {
vote3++;
}
else if (button4.isPressed()) {
vote4++;
}
else if (button5.isPressed()) {
voting_completed = 1;
vote_count();
delay(3000);
Serial.println("voting completed");
}
} else {
determine_winner();
delay(2000);
}
delay(100);
}
void vote_count(){
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
// oled.setCursor(1,0);
// oled.println("A");
// oled.setCursor(1,30);
// oled.println(vote1);
// oled.setCursor(35,0);
// oled.println("B");
// oled.setCursor(35,30);
// oled.println(vote2);
// oled.setCursor(70,0);
// oled.println("C");
// oled.setCursor(70,30);
// oled.println(vote3);
// oled.setCursor(105,0);
// oled.println("D");
// oled.setCursor(105,30);
// oled.println(vote4);
oled.setCursor(1, 0);
oled.print("A B C D");
oled.setCursor(1, 20);
oled.print(vote1);
oled.setCursor(35, 20);
oled.print(vote2);
oled.setCursor(65, 20);
oled.print(vote3);
oled.setCursor(100, 20);
oled.print(vote4);
oled.display();
}
void determine_winner(){
delay(100);
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(1,10);
if(vote1>vote2 && vote1>vote3 && vote1>vote4){
oled.println("A is the winner");
}
else if(vote2>vote1 && vote2>vote3 && vote2>vote4){
oled.println("B is the winner");
}
else if(vote3>vote1 && vote3>vote2 && vote3>vote4){
oled.println("C is the winner");
}
else if(vote4>vote1 && vote4>vote2 && vote4>vote3){
oled.println("D is the winner");
}
else{
oled.println("There was a tie vote again");
}
oled.display();
}