#include "OneButton.h"
#include                 // ?????????? LCD
LiquidCrystal lcd(13, 12, 11, 10,  9,  8);
 

OneButton button1(0, true);
OneButton button2(1, true);
OneButton button3(2, true);    // ?????? ???????

#define  LED1  22                // ?????? ?????????? 1
#define  LED2  23
#define  LED3  24

#define  BUZZ  5                // ???????

int  P1 =  0;                    // ???? ??????? ??????
int  P2 =  0;                    // ???? ??????? ??????
int backLight = 3;

byte regim = 1;

void setup()
{
  button1.attachClick(Click1);
  button2.attachClick(Click2);
  button3.attachClick(Click3);
  button3.attachLongPressStart(LongPressStart3);  /// ?????????? ???????
  pinMode(backLight, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(BUZZ, OUTPUT);
  digitalWrite(backLight, HIGH);
  lcd.begin(16, 2);
  lcd.clear();                          //  ??????? ???????
  lcd.setCursor(0, 0);   
  lcd.print("R  P1");
  lcd.setCursor(3, 1);
  lcd.print("P2");

}

void loop()
{ 
  button1.tick();
  button2.tick();
  button3.tick();

  lcd.setCursor(6, 0);                      // ?????? ????? 1 ??????
  lcd.print(P1);
  lcd.print(" ");
  lcd.setCursor(0, 1);                    // ????? ?????? ?????
  lcd.print(regim);
  lcd.setCursor(6, 1);
  lcd.print(P2);                          // ?????? ????? 2 ??????
  lcd.print(" "); 
}

void Click1()
{
  P1 = P1 + 1;
  if(P1 >= 9 &&  regim == 2 &&  P1 > P2)
  {
    END();
  }
  if(P1 >= 21 &&  regim == 3 &&  P1 > P2)
  {
    END(); 
  }
}

void Click2()
{
  P2 = P2 + 1;
  if(P2 >= 9 &&  regim == 2 &&  P2 > P1)
  {
    END();
  }
  if(P2 >= 21 &&  regim == 3 &&  P2 > P1)
  {
    END(); 
  }
}

void Click3()
{
  regim++;     
  if(regim > 3)
  { 
    regim = 1;
  } 
}
void LongPressStart3()
{
  P1 =  0;
  P2 =  0;
}

void END()
{
  lcd.setCursor(10, 0);                   
  lcd.print("Game");
  lcd.setCursor(10, 1);
  lcd.print("Over");
  
  digitalWrite(LED1, HIGH);
  
  digitalWrite(BUZZ, HIGH);
  delay(1000);
  lcd.setCursor(10, 0);                   
  lcd.print("    ");
  lcd.setCursor(10, 1);
  lcd.print("    ");
  digitalWrite(LED1, LOW);
 
  digitalWrite(BUZZ, LOW);
  delay(1000);
  digitalWrite(LED2, HIGH);
  lcd.setCursor(10, 0);                   
  lcd.print("Game");
  lcd.setCursor(10, 1);
  lcd.print("Over");
  
  
  digitalWrite(LED2, LOW);
  digitalWrite(BUZZ, HIGH);
  delay(1000);
  lcd.setCursor(10, 0);                   
  lcd.print("    ");
  lcd.setCursor(10, 1);
  lcd.print("    ");
  digitalWrite(LED3, HIGH);
  
  digitalWrite(BUZZ, LOW);
  delay(1000);
  digitalWrite(LED3, LOW);
  lcd.setCursor(10, 0);                   
  lcd.print("Game");
  lcd.setCursor(10, 1);
  lcd.print("Over");
  
  digitalWrite(LED1, HIGH);
  digitalWrite(BUZZ, HIGH);
  delay(1000);
  lcd.setCursor(10, 0);                   
  lcd.print("    ");
  lcd.setCursor(10, 1);
  lcd.print("    ");
  digitalWrite(LED1, LOW);
  
  digitalWrite(BUZZ, LOW);
  P1 =  0;
  P2 =  0;
}

