Fun Arduino Number Guessing Game With LCD Display – Beginner-Friendly
by _Stemonkey in Circuits > Arduino
126 Views, 1 Favorites, 0 Comments
Fun Arduino Number Guessing Game With LCD Display – Beginner-Friendly
Introduction
Have you ever wanted to bring a classic number-guessing game to life using electronics? This project allows you to do just that using an Arduino, a 4x4 keypad, and an I2C LCD. The game is simple: guess the number selected by the Arduino, and you'll receive feedback on whether your guess is too high, too low, or correct. You get a limited number of attempts to guess the number, and if you run out, the correct answer will be revealed.
This project is perfect for beginners looking to improve their skills with Arduino, user input, and LCDs. It’s also a fun way to create an interactive game that you can play with friends and family.
Downloads
Supplies
Materials Needed
- Arduino Uno: The main controller for the project.
- 4x4 Matrix Keypad: For user input (guessing the number).
- I2C 16x2 LCD Display: To display the game prompts and feedback.
- Buzzer: (Optional) For sound feedback on correct and incorrect guesses.
- Breadboard and Jumper Wires: For connecting components. (additional)
Circuit Diagram
- Connect the Keypad:
- The 4x4 keypad has 8 pins, which connect to the Arduino’s digital pins. (check the image)
- Connect the LCD:
- The I2C LCD connects to the Arduino’s SDA and SCL pins (typically A4 and A5).
- Connect the Buzzer (Optional):
- Connect the positive pin of the buzzer to a digital pin (e.g., pin 11) and the negative pin to the ground.
Code Overview
Here’s a breakdown of how the code works:
1. Setup and Initialization
- The LCD and keypad are initialized in the setup() function. The LCD is configured to display prompts, and the keypad is used to take input from the user.
2. Starting a New Game
- The startNewGame() function sets up a new round by generating a random number within the selected range (0-9 or 0-99). It also displays the initial prompt for the user to enter their guess.
3. User Input and Validation
- The user enters their guess via the keypad. The input is displayed on the LCD, and the game checks if the guess is within the valid range.
- If the guess is out of range, an error message is displayed, and the user is prompted to enter a valid number.
4. Checking the Guess
- The checkGuess() function compares the user’s guess to the target number.
- If the guess is correct, a congratulatory message is displayed, and the game restarts.
- If the guess is incorrect, feedback is given (too high or too low), and the remaining number of attempts is displayed.
- If the user runs out of attempts, the correct number is revealed before restarting the game.
5. Game Modes
- The game has two modes:
- 0-9 Range: The player has 3 attempts to guess a number between 0 and 9.
- 0-99 Range: The player has 7 attempts to guess a number between 0 and 99.
- The player can toggle between these two modes using the C key.
6. Additional Features
- The A key provides help, explaining how to play the game and the functions of the keys.
- The B key allows the user to backspace and correct their input.
- A buzzer provides audio feedback for correct and incorrect guesses.