#include <LiquidCrystal.h>
#include <mechButton.h>


LiquidCrystal lcd(12,11,10,9,8,7,6);

mechButton btn1(2);
mechButton btn2(3);
mechButton btn3(4);
mechButton btn4(5);
mechButton btnClr(13);

int choiceA;
int choiceB;
int choiceC;
int choiceD;
bool  change;

void setup() {

  lcd.begin(16,2);
  lcd.print("Click to vote.");
  btn1.setCallback(btn1Clk);
  btn2.setCallback(btn2Clk);
  btn3.setCallback(btn3Clk);
  btn4.setCallback(btn4Clk);
  btnClr.setCallback(btnClrClk);
  clearVotes();
  change = true;
}


void btn1Clk(void) {
  
  if (!btn1.trueFalse()) {
    choiceA++;
    change = true;
  }
}


void btn2Clk(void) {
  
  if (!btn2.trueFalse()) {
    choiceB++; 
    change = true;
  }
}


void btn3Clk(void) {
  
  if (!btn3.trueFalse()) {
    choiceC++;
    change = true;
  }
}


void btn4Clk(void) {
  
  if (!btn4.trueFalse()) {
      choiceD++;
      change = true;
  }
}


void btnClrClk(void) {
  
  if (!btnClr.trueFalse()) {
      clearVotes();
      change = true;
  }
}


void clearVotes(void) {

  choiceA = 0;
  choiceB = 0;
  choiceC = 0;
  choiceD = 0;
}

void printResultLine(void) {

  lcd.setCursor(0, 1);
  lcd.print("                ");
  lcd.setCursor(0, 1);
  
  lcd.print("A");
  lcd.print(choiceA);

  lcd.print(" B");
  lcd.print(choiceB);

  lcd.print(" C");
  lcd.print(choiceC);

  lcd.print(" D");
  lcd.print(choiceD);
  change = false;
}

void loop() { 

  idle();
  if (change) {
    printResultLine();
  }
 }