Project showcase
Combo Masher

Combo Masher © GPL3+

A small game in which the player must match the sequence to earn points. The game ends when the time runs out.

  • 1,789 views
  • 0 comments
  • 3 respects

Components and supplies

Necessary tools and machines

hands

Apps and online services

About this project

This is a game utilizing an Arduino kit and the Liquid Crystal LCD display. The goal of the game is to copy as many combos as possible before time runs out.

Code

ButtonMasherC/C++
This program uses pins 3 - 13 on an Arduino Uno. I used 2 breadboards for space convenience.
#include <LiquidCrystal.h>

// Controls the game button inputs
const int btn1 = 10;
const int btn2 = 11;
const int btn3 = 12;
const int btn4 = 13;
// variable for reading the pushbutton status
int bs1 = 0;
int bs2 = 0;
int bs3 = 0;
int bs4 = 0;

// Sound
const int buzz = 3;

// Values to detect combination
int combo[] = {0, 0, 0, 0};
int input[] = {0, 0, 0, 0};
int index = 0;//which index of 'input'

// Controls LCD Values
const int rs = 4, en = 5, d4 = 6, d5 = 7, d6 = 8, d7 = 9;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//Game Variables
int score = 0;
float timer = 60.0;
bool isGame = true;



void setup() 
{   
  isGame = true;
  
  pinMode(btn1, INPUT);
  pinMode(btn2, INPUT);
  pinMode(btn3, INPUT);
  pinMode(btn4, INPUT);

  // Set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // Let's make it more random
  // Uses an empty analog pin to get a floating number
  randomSeed(analogRead(A0));
  // Generate a random 4-digit sequence
  MakeCombo();
}



void loop() 
{
  if(isGame) 
  {
      // Prints at the beginning of the screen
      ClearScreen();
      PrintInfo();
    
      // Read the input values from the buttons
      bs1 = digitalRead(btn1);
      bs2 = digitalRead(btn2);
      bs3 = digitalRead(btn3);
      bs4 = digitalRead(btn4);
    
      if(index >= 4)
        ResetInput();
    
      // Check if the buttons states are HIGH and print the output on the display
      if(bs1 == HIGH) {
        tone(buzz, 3000);
        input[index++] = 1;
        delay(100); // feel free to adjust this for button length
        
      } else if(bs2 == HIGH) {
        input[index++] = 2;
        tone(buzz, 3000);
        delay(100); // feel free to adjust this for button length
        
      } else if(bs3 == HIGH) {
        input[index++] = 3;
        tone(buzz, 3000);
        delay(100); // feel free to adjust this for button length
        
      } else if(bs4 == HIGH) {
        input[index++] = 4;
        tone(buzz, 3000);
        delay(100); // feel free to adjust this for button length
        
      } else
        noTone(buzz);
    
        // Check if the combo was a match
        if(CheckCombo())
        {
            score++;
            MakeCombo();
            ResetInput();
        
            //tone(buzz, 5000, 250);
            delay(100);
        }
        
      //Checks if the game is over (when time runs out)
      if(timer > 0)
        timer -= 0.1f;
      else
        GameOver();
  }
    
  delay(100); // Make sure to adjust accordingly with the 'timer'
}



void PrintInfo()
{
  // This prints on the first line of the 16x2 display
  lcd.print("S: ");
  lcd.print(score);
  if(score < 10)
    lcd.print("       ");
   else
    lcd.print("      ");
  lcd.print("T: ");
  lcd.print(static_cast<int>(timer));
  
  lcd.setCursor(0, 1);
  for(int i = 0; i < 4; i++)
    lcd.print(combo[i]);
    lcd.print("        ");
  for(int i = 0; i < 4; i++)
    lcd.print(input[i]);
}



void ClearScreen() 
{
    //Iterates through each row and prints a blank
    lcd.setCursor(0, 0);
    for(int col = 0; col < 16; col++)
        lcd.print(" ");
        
    lcd.setCursor(0, 1);
    for(int col = 0; col < 16; col++)
    lcd.print(" ");

    lcd.setCursor(0, 0);
}



void MakeCombo()
{
    // Fill array with random values assigned to button numbers
    for(int i = 0; i < 4; i++)
        combo[i] = random(1, 5);
}



bool CheckCombo()
{
    // Check if the input == combo
    for(int i = 0; i < 4; i++)
    {
        if(combo[i] != input[i])
            return false;
    }
    return true;
}



void ResetInput()
{
    for(int i = 0; i < 4; i++)
        input[i] = 0;
    index = 0;
}



void GameOver()
{
    ClearScreen();
    
    lcd.print("GameOver");
    lcd.setCursor(0, 1);
    lcd.print("Score: ");
    lcd.print(score);
    
    isGame = false;
}

Schematics

Combo Masher Schematic
Follow the wires carefully, you should be able to drag the code in and run it.
Buttonmasherschematic bb etcqgcweq2

Comments

Similar projects you might like

Using 16x2 LCD with Arduino

by Techmirtz

  • 19,426 views
  • 3 comments
  • 34 respects

Light Sequence Creator

Project tutorial by Reid Paulhus

  • 9,085 views
  • 2 comments
  • 20 respects

Arduino Game By LCD

Project tutorial by Mohammed Magdy

  • 32,874 views
  • 31 comments
  • 108 respects

Arduino Temperature - Humidity - Rain Sensor

Project showcase by Rick_Findus

  • 7,412 views
  • 6 comments
  • 13 respects

Start/Stop Chronometer

Project showcase by Gabriele Scordamaglia

  • 3,667 views
  • 0 comments
  • 5 respects

AI LCD friend!

Project tutorial by aip06

  • 2,988 views
  • 8 comments
  • 7 respects
Add projectSign up / Login