Reaction Game

by 685274 in Circuits > Arduino

1231 Views, 7 Favorites, 0 Comments

Reaction Game

Screenshot 2023-06-19 at 1.52.33 PM.png

The circuit I made is a reaction game that involves 2 sectors of computer engineering, hardware and software. The circuits intent is to test your reaction skills. The way it works is that you push 1 of 2 buttons to start the game and the code combined with the LCD screen to prepare the user for the game, once the green LED turns on, the user pushed the button and the buzzer will go off indicating you pushed the button. if you fail it will count down from 3 and prints a message saying you failed. if you win, it will skip the countdown and print a message saying congratulations and the time it took you to react

Supplies

Screenshot 2023-06-19 at 2.03.33 PM.png
Screenshot 2023-06-19 at 2.03.46 PM.png

Above is a list for all my materials (Note this list excludes wires and the Arduino itself)

Wiring

Screenshot 2023-06-19 at 2.15.04 PM.png
Screenshot 2023-06-19 at 2.14.41 PM.png
Screenshot 2023-06-19 at 2.14.22 PM.png

Step 1: The first step is to collect all the parts, organizing them and getting started with the 555 timer and the 2.2 K resistor, capacitor and the decade counter


Step 2: The next steps were to start with the buzzer, LEDs, and the buttons, which I wired in the breadboard and directly to the Arduino


Step 3:The next task was with the LCD screen which I wired to a separate breadboard and directly to the Arduino.


Step 4: The last step to the circuit was the code, I posted the code below.

Code

This is the code in c++ that I ran in the Arduino software:

// 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();

  
}