Arduino Basic Game: Over or Under?

Introduction: Arduino Basic Game: Over or Under?

After figuring out the basics of using the LCD screen with arduino the next natural step was to add some inputs and make some sort of game. The game is really basic where the LCD screen will give a prompt asking "Over or Under?" along with a number and the user has to guess if the next number is over or under the number shown. The game uses a few basic concepts such as the LEDs, LCDs and push buttons along with the arduino uno.

Supplies

(1) Arduino Uno

(1) LCD screen (1602A)

(1) 10k Potentiometer

(2) LEDs, 1 Green and 1 Red although color is optional

(2) 330 Ohm Resistors

(2) Buttons

(25) M-M Wires

Step 1: Connect Components

Connect the components as shown in the figure above.

The connections shown are:

LCD to Arduino

(K) to (GND)

(A) to (5V)

(D7) to (2)

(D6) to (3)

(D5) to (4)

(D4) to (5)

(E) to (11)

(RW) to (GND)

(RS) to (12)

(Vo) to (potentiometer)

(VDD) to (5v)

(VSS) to (GND)

All (5V) and (GND) refer to connections to the bread board. The arduino will be connected to the breadboard using the 5V and GND to the red and black rows onto the breadboard.

LEDs to Arduino

Red LED to (10)

Green LED to (9)

Buttons to Arduino

Green to (7)

Red to (6)

Step 2: Code - Set Up

This code requires the liquidCrystal library.

The first part of the code initializes all the variables needed as shown:

// include the library code:
#include <liquidCrystal.h
// Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Button set ups
const int buttonG = 7;
const int buttonR = 6;
int redState;
int greenState;
//red or green variable
int rG;
//LED set ups
const int ledG = 9;
const int ledR = 10;
//game variable
int score = 5;
long rNum=0;
long lNum = rNum;

The next part of the code is a function that, when called, waits for a button to be pressed then returns a number which indicates the button that was pressed.

//Function to wait for the button to be pressed, returns 1 or 2 for the button pressed
int buttonPressed(){ int tmp; while(greenState == HIGH && redState == HIGH){ greenState = digitalRead(buttonG); redState= digitalRead(buttonR); } if(greenState == LOW){ tmp = 1; } if(redState == LOW){ tmp = 2; } return tmp; }

The "void setup()" sets the leds as outputs, the buttons as Input_Pullups as well as the initial button states. The LCD is cleared the game title is shown and the initial number is determined using random function, for 'true' random the seed is set by reading the analog 0.

void setup() 
{ //LED set up pinMode(ledG,OUTPUT); pinMode(ledR,OUTPUT);
  //Button set up
  pinMode(buttonG, INPUT_PULLUP);
  pinMode(buttonR, INPUT_PULLUP);
  greenState = digitalRead(buttonG);
  redState = digitalRead(buttonR);
  
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Clears the LCD screen, says name of game
  lcd.print("Over/Under Game");
  delay(3000);
  lcd.clear();
  delay(2000);
  randomSeed(analogRead(0));
  lNum = random(101);
}

Step 3: Main Code

The main part of the code starts by setting up the LCD for the game portion; the code then calls the buttonPressed() function, remember this function is set up to wait for a button to be pressed. When a value is returned it will go into an if_else statement which checks the input and if it is correct or incorrect. If the input is correct, then the green LED will momentarily turn on and one point will be added to the score. If the input is incorrect, then the red LED will momentarily turn on and one point will be subtracted from the score. The code is shown below:

void loop() <br>{
  lcd.setCursor(0, 0);
  lcd.print("Over or Under?");
  lcd.setCursor(0, 1);
  lcd.print(lNum);
  rNum = random(101);
  
  greenState = digitalRead(buttonG);
  redState= digitalRead(buttonR);
  
  rG = buttonPressed();
 
 if(rG == 1){
  if(lNum < rNum){
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Correct");
    score = score +1;
    lcd.setCursor(0, 1);
    lcd.print("Score: "); lcd.print(score);
    digitalWrite(ledG,HIGH);
    delay(1000);
    digitalWrite(ledG,LOW);
    lcd.clear();
  }
  else{
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Incorrect");
    score = score - 1;
    lcd.setCursor(0, 1);
    lcd.print("Score: "); lcd.print(score);
    digitalWrite(ledR,HIGH);
    delay(1000);
    digitalWrite(ledR,LOW);
    lcd.clear();
  }
 }
if(rG == 2){
  if(lNum > rNum){
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Correct");
    score = score +1;
    lcd.setCursor(0, 1);
    lcd.print("Score: "); lcd.print(score);
    digitalWrite(ledG,HIGH);
    delay(1000);
    digitalWrite(ledG,LOW);
    lcd.clear();
  }
  else{
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Incorrect");
    score = score - 1;
    lcd.setCursor(0, 1);
    lcd.print("Score: "); lcd.print(score);
    digitalWrite(ledR,HIGH);
    delay(1000);
    digitalWrite(ledR,LOW);
    lcd.clear();
  }
 }
 lNum = rNum;
}

Step 4: Upload Sketch and Play

Attached is the arduino code and the fritzing file for the schematic.

Obviously with the code there is most likely a better/more optimal way to write it, there is room to improve.

The score starts at 5, initially the game was intended to end when hitting 0 points, that can be an addition to the code in the future.

Also, attaching a 9V battery with adapter to the arduino makes it mobile so you can show off your project to friends and family.

Be the First to Share

    Recommendations

    • Rice & Grains Challenge

      Rice & Grains Challenge
    • CNC and 3D Printing Contest

      CNC and 3D Printing Contest
    • Puzzles Challenge

      Puzzles Challenge

    Comments