#include <LiquidCrystal.h>
#include<EEPROM.h>
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾//
//------------DEFEND_MODE------------//
//___________________________________//
//-----------------------------------//
//settings
//-----------------------------------//
int prepare = 3;
int minute = 15;
//-----------------------------------//



const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int stop = A1;
const int buzz = 9;
int Min = 0;
int sec = 0;
unsigned int check_val = 50;
bool PREP = true;
bool RUN = true;
bool min_flag = true;



void setup()
{
  lcd.begin(16, 2);
  pinMode(stop, INPUT_PULLUP);
  pinMode(buzz, OUTPUT); 
  digitalWrite(buzz, LOW);

  Min = prepare;


  delay(1500);
  INIT();
  delay(1000);
  prep();
}

void loop()
{

}


void prep()
{
    lcd.clear();
    delay(250);
    PREP = true;
    while (PREP)
    {
      sec = sec - 1;
      delay(1000);
      if (sec == -1)
      {
        sec = 59;
        Min = Min - 1;
      }
      lcd.setCursor(2, 0);
      lcd.print("PREPARATION");
      lcd.setCursor(5, 1);
      //lcd.print(':');
      if (Min <= 9)
      {
        lcd.print('0');
      }
      lcd.print(Min);
      lcd.print(':');
      if (sec <= 9)
      {
        lcd.print('0');
      }
      lcd.print(sec);
      if (Min == 0 && sec == 0)
      {
        lcd.setCursor(4, 0);
        PREP = false;
        for (int i = 0; i < 20; i++)
        {
          digitalWrite(buzz, HIGH);
          delay(1000);
          digitalWrite(buzz, LOW);
          delay(1000);
          Min = minute;
          timer();
        }
      }
    }
}

void timer()
{
    lcd.clear();
    delay(250);
    RUN = true;
    while (RUN)
    {
      if (digitalRead(stop) == LOW)
      {
        RUN = false;
        win();
      }
      
      sec = sec - 1;
      delay(1000);
      if (sec == -1)
      {
        sec = 59;
        Min = Min - 1;
      }
      lcd.setCursor(5, 0);
      lcd.print("DEFUSE");
      lcd.setCursor(5, 1);
      if (Min <= 9)
      {
        lcd.print('0');
      }
      lcd.print(Min);
      lcd.print(':');
      if (sec <= 9)
      {
        lcd.print('0');
      }
      lcd.print(sec);
      if (Min == 0 && sec == 0)
      {
        lcd.setCursor(4, 0);
        RUN = false;
        for (int i = 0; i < 20; i++)
        {
          /*digitalWrite(buzz, HIGH);
          delay(100);
          digitalWrite(buzz, LOW);
          delay(100);*/

        boom();
        }
      }
    }
}




void INIT()
{
  Min = Min;
  sec = 0;
  lcd.clear();
  lcd.setCursor(5, 0);
  lcd.print("READY?");
  lcd.setCursor(5, 1);
  if (Min <= 9)
  {
    lcd.print('0');
  }
  lcd.print(Min);
  lcd.print(':');
  if (sec <= 9)
  {
    lcd.print('0');
  }
  lcd.print(sec);
  min_flag = true;
  delay(500);
}


void boom()
{
  digitalWrite(buzz, HIGH); 
  delay(10000);
  digitalWrite(buzz, LOW);
  delay(250);
}

void win()
{
  delay(250);
  lcd.clear();
          lcd.setCursor(6, 0);
          lcd.print("WIN");
          lcd.setCursor(0, 1);
          lcd.print("****************");
  digitalWrite(buzz, HIGH); 
  delay(250);
  digitalWrite(buzz, LOW);
  delay(250);
  win();
}