// C++ code
//
#include <LiquidCrystal.h>

//LCD Pin declaration

const int rs = 7, en = 6, d4 = 8, d5 = 9, d6 = 10, d7 = 11;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//Reaction Game Components
int buzzer =12;
int led=13;
int startButton = 3;
int reactButton=2;

//Other variables

int time = 0;
int buttonTime;
int reactTime;

void setup()
{
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  
  //setting pin Modes
  pinMode(led, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(reactButton, INPUT);
  pinMode(startButton, INPUT);
}

void introScreen()//presents the intro screen on LCD
{
  delay(1000);
  lcd.print("Reaction Game");
  lcd.setCursor(0, 1);
  lcd.print("-Press the Start Button to Continue-");
  delay(1000);

}


void loseScreen()//presents the lose screen on LCD
{
  delay(1000);
  lcd.setCursor(0,0);
  lcd.print("You lose..");
  lcd.setCursor(0,1);
  lcd.print("Took too long! ");
  delay(4000);
  lcd.clear();
  
}


void winScreen()//presents the win screen on the LCD
{
  delay(1000);
  lcd.setCursor(0,0);
  lcd.print("Congratulations!");
  lcd.setCursor(0,1);
  lcd.print("Time: ");
  lcd.setCursor(5,1);
  lcd.print(reactTime + 1);
  lcd.print(" seconds.");
  int startButtonState = digitalRead(startButton);  
  delay(4000);
  lcd.clear();
  
}



void timer()///keeps track of time
{
  Serial.print("Time: ");
  Serial.println(time);
  Serial.print("Time for LED to turn ON: ");
  int reactButtonState= digitalRead(reactButton);
  
  buttonTime=random(2, 8);//picks a random time from 2 to 8 seconds
  Serial.println(buttonTime);
  while(time <= buttonTime && reactButtonState == 0)
  {
    delay(1000);//delay 1 second

    if(time == buttonTime)//if the button time is reached
    {
      digitalWrite(led, HIGH);//make the LED blink
      delay(500);
      time += 0.5;
      digitalWrite(led, LOW);
      delay(100);
      
      int count=0;
      lcd.clear();
      lcd.print("You lose in: ");//start the countdown on the LCD monitor
      lcd.setCursor(0,1);
      lcd.print("Count:");
      lcd.setCursor(6,1);
      lcd.print(4-(count));

      while(count < 3)
      {
      	count++;
        Serial.print("count: ");
        lcd.setCursor(6,1);
        Serial.println(count);
        lcd.print(4-(count));
        reactButtonState= digitalRead(reactButton);
        digitalWrite(buzzer, HIGH);
      if(reactButtonState == 1)
      {
        Serial.println("You win!");
        lcd.clear();
        winScreen();
        break;
      }
        delay(1000);
        digitalWrite(buzzer, LOW);

        reactTime++;
      
      }
      

      
      if(count ==3 && reactButtonState==0)
      {
        lcd.clear();
        loseScreen();
        delay(1000);
      }
      
  }
    time++;
}
}        

void waitScreen()// this message will show on the LCD monitor screen
{//this will allow the user to know THAT their start button input was recieved.
  delay(1000);
  lcd.clear();
  lcd.print("Get ready....");

}

void resetAll()//resets all the variables
{
  time=0;
  reactTime=0;
  buttonTime=0;
  digitalWrite(buzzer, LOW);
  
}
                
void loop()
{
  introScreen();
  int startButtonState = digitalRead(startButton);
  while(startButtonState!= 1)//makes sure start button is pressed before continuing
  {
    startButtonState = digitalRead(startButton);
  }
  waitScreen();
  timer();
  resetAll();
	
  
}