#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <ezButton.h>
#define screen_width 128
#define screen_height 64
Adafruit_SSD1306 OLED(screen_width, screen_height, &Wire, -1);
ezButton b1(34);
ezButton b2(35);
ezButton b3(32);
ezButton b4(33);
ezButton b5(25);
int vote1 = 0, vote2 = 0, vote3 = 0, vote4 = 0;
int flag = 0;
int voting_completed = 0;
void setup() {
Serial.begin(9600);
b1.setDebounceTime(25);
b2.setDebounceTime(25);
b3.setDebounceTime(25);
b4.setDebounceTime(25);
b5.setDebounceTime(25);
if (!OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
};
delay(2000);
OLED.clearDisplay();
OLED.setTextSize(2);
OLED.setTextColor(WHITE);
OLED.setCursor(2,5);
OLED.println("Start");
OLED.setCursor(2,21);
OLED.println("Voting");
OLED.display();
delay(2000);
OLED.clearDisplay();
OLED.setTextSize(2);
OLED.setTextColor(WHITE);
OLED.setCursor(2,0);
OLED.println("RED- A");
OLED.setCursor(2,16);
OLED.println("YELLOW- B");
OLED.setCursor(2,32);
OLED.println("BLUE- C");
OLED.setCursor(2,48);
OLED.println("GREEN- D");
OLED.display();
flag = 1;
}
void loop() {
b1.loop();
b2.loop();
b3.loop();
b4.loop();
b5.loop();
if (voting_completed == 0 && flag == 1){
if (b1.isPressed()){
vote1++;
}
else if(b2.isPressed()){
vote2++;
}
else if(b3.isPressed()){
vote3++;
}
else if(b4.isPressed()){
vote4++;
}
else if(b5.isPressed()){
voting_completed = 1;
vote();
Serial.println("THANK YOU FOR VOTING!");
}
}
else{determine_winner();}
delay(10);
}
void vote(){
OLED.clearDisplay();
OLED.setTextSize(2);
OLED.setTextColor(WHITE);
OLED.setCursor(1,0);
OLED.println("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();
delay(2000);
}
void determine_winner() {
OLED.clearDisplay();
OLED.setTextSize(1);
OLED.setTextColor(WHITE);
OLED.setCursor(1, 10);
if ((vote1 > vote2 && vote1 > vote3 && vote1 > vote4))
OLED.print("A is the winner");
else if (vote2 > vote1 && vote2 > vote3 && vote2 > vote4)
OLED.print("B is the winner");
else if (vote3 > vote1 && vote3 > vote2 && vote3 > vote4)
OLED.print("C is the winner");
else if(vote4 > vote1 && vote4 > vote2 && vote4 > vote3)
OLED.print("D is the winner");
else OLED.print("There was a tie. Vote again!");
OLED.display();
}