Arduino Password Guesser Game
by BigBird in Circuits > Electronics
482 Views, 1 Favorites, 0 Comments
Arduino Password Guesser Game
Hi this is my circuit I made this circuit is a small number guesser game that you need to guess what three of the numbers are to win.
The 7-segment display will act as a countdown timer starting from 10 seconds. The player must press a specific sequence of buttons on the remote within these 10 seconds. If they complete the sequence in time, the LCD will display "Success!" and a green LED will light up. However, if the player fails to complete the sequence in time or presses the wrong sequence, the LCD will display "Try Again!" and a red LED will light up.
Supplies
Arduino Uno
7-segment display
LCD 16x2 with I2C interface
Green LED
Red LED
IR sensor module
IR remote
330 ohm resistors (for LEDs and 7-segment display)
Breadboard and jumper wires
Putting All the Components on the Board
You can skip this step, but it's good to have all your components on the breadboard because it's faster and more convenient to assemble your circuit, and it helps you avoid a messy desk.
Connecting the 7-segemnt-display
- Connect to 5V on the breadboard through a 220 ohm resistor (both sides).
- Segment a: Connect to digital pin 2 on the Arduino through a 220 ohm resistor.
- Segment b: Connect to digital pin 3 on the Arduino through a 220 ohm resistor.
- Segment c: Connect to digital pin 4 on the Arduino through a 220 ohm resistor.
- Segment d: Connect to digital pin 5 on the Arduino through a 220 ohm resistor.
- Segment e: Connect to digital pin 6 on the Arduino through a 220 ohm resistor.
- Segment f: Connect to digital pin 7 on the Arduino through a 220 ohm resistor.
- Segment g: Connect to digital pin 8 on the Arduino through a 220 ohm resistor.
Connecting the LCD(I2C)
LCD 16x2 with I2C interface:
- SDA: Connect to the SDA pin on the Arduino (A4).
- SCL: Connect to the SCL pin on the Arduino (A5).
- VCC: Connect to 5V on the breadboard.
- GND: Connect to GND on the breadboard.
Connecting the LED's
Green LED:
- Anode (longer leg): Connect to digital pin 9 on the Arduino through a 220 ohm resistor.
- Cathode (shorter leg): Connect to GND on the breadboard.
Red LED:
- Anode (longer leg): Connect to digital pin 10 on the Arduino through a 220 ohm resistor.
- Cathode (shorter leg): Connect to GND on the breadboard.
Connecting the IR Sensor and Slide Switch
IR Sensor Module:
- VCC: Connect to 5V on the breadboard.
- GND: Connect to GND on the breadboard.
- OUT (signal output): Connect to digital pin 11 on the Arduino.
Slide Switch:
- TERMINAL 2: Connect to 5V on the breadboard.
- COMMON: Connect to GND on the breadboard.
- TERMINAL 1: Connect to digital pin 12 on the Arduino
The Code
So now we do the code before I give you the whole code we need to figure out your IR remotes HEX code, to figure out the HEX code you just simply find code only for it but worry not so that I have included it in my main code you won't have to worry about finding the code that works for you.
This was my hex code for buttons 1 - 9:
1 = FF30CF
2 = FF18E7
3 = FF7A85
4 = FF10EF
5 = FF38C7
6 = FF5AA5
7 = FF42BD
8 = FF4AB5
9 = FF52AD
Before I give you the main code I would still like to give the code to figure out your HEX code:
#include <IRremote.h>
const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop(){
if (irrecv.decode(&results)){
Serial.println(results.value, HEX);
irrecv.resume();
}
}
Just to make sure I will add the files to the two main files for the code, it will be there the game code and hex code files.
Now you can figure out your IR remote buttons. Now I can give you the main code but if you want to change the password you need to go to line 28 where the three buttons are defined and change the hex code there, and just to be sure i wrote comments so you wont get confused.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the pin for the IR receiver
const int RECV_PIN = 11;
// Define the slide switch pin
const int slideSwitchPin = 12;
// Define the LED pins
const int redLedPin = 10;
const int greenLedPin = 9;
// Define the 7-segment display pins
const int segA = 2;
const int segB = 3;
const int segC = 4;
const int segD = 5;
const int segE = 6;
const int segF = 7;
const int segG = 8;
// Define the sequence of buttons to be pressed
unsigned long buttonSequence[] = {0xFF30CF, 0xFF30CF, 0xFF18E7}; // button "1,1,2" (You can change the password by changing the hex code here)
int buttonIndex = 0;
// Define the countdown time
int countdown = 9;
// Set up the IR receiver
IRrecv irrecv(RECV_PIN);
decode_results results;
// Variable to track the game state
bool gameOn = false;
// Function to turn off all segments of the 7-segment display
void clearDisplay() {
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
}
// Function to display numbers 0-9 on the 7-segment display
void displayNumber(int num) {
clearDisplay();
switch (num) {
case 0:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
break;
case 1:
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
break;
case 2:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segG, LOW);
digitalWrite(segE, LOW);
digitalWrite(segD, LOW);
break;
case 3:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segG, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
break;
case 4:
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
break;
case 5:
digitalWrite(segA, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
break;
case 6:
digitalWrite(segA, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
break;
case 7:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
break;
case 8:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;
case 9:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;
}
}
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Set the LED pins as output
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
// Set the 7-segment display pins as output
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
// Set up the IR receiver
irrecv.enableIRIn();
// Set the slide switch pin as input
pinMode(slideSwitchPin, INPUT);
// Enable internal pull-up resistor for the slide switch pin
digitalWrite(slideSwitchPin, HIGH);
}
void loop() {
// Read the state of the slide switch
int slideSwitchState = digitalRead(slideSwitchPin);
// Check if the slide switch is in the ON position and the game is off
if (slideSwitchState == HIGH && !gameOn) {
// Turn on the game
gameOn = true;
// Add any necessary code to start the game's functionality
lcd.clear(); // Clear the LCD
lcd.print("Game On"); // Print the game on message
}
// Check if the slide switch is in the OFF position and the game is on
if (slideSwitchState == LOW && gameOn) {
// Turn off the game
gameOn = false;
// Add any necessary code to stop the game's functionality
digitalWrite(greenLedPin, LOW); // Turn off the green LED
digitalWrite(redLedPin, LOW); // Turn off the red LED
lcd.clear(); // Clear the LCD
lcd.print("Game Off"); // Print the game off message
}
// Check if the game is on
if (gameOn) {
// If there are any IR commands, decode them
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX); // Print the received IR code in hexadecimal
// If the button pressed is the next in the sequence
if (results.value == buttonSequence[buttonIndex]) {
buttonIndex++; // Move to the next button in the sequence
if (buttonIndex == sizeof(buttonSequence) / sizeof(buttonSequence[0])) { // If the entire sequence was pressed
digitalWrite(greenLedPin, HIGH); // Turn on the green LED
lcd.clear(); // Clear the LCD
lcd.print("Success!"); // Print the success message
delay(1000); // Wait for 1 second
digitalWrite(greenLedPin, LOW); // Turn off the green LED
lcd.clear(); // Clear the LCD
buttonIndex = 0; // Reset the button index for the next round
}
} else { // If the button pressed is not in the sequence
digitalWrite(redLedPin, HIGH); // Turn on the red LED
lcd.clear(); // Clear the LCD
lcd.print("Try Again!"); // Print the failure message
delay(1000); // Wait for 1 second
digitalWrite(redLedPin, LOW); // Turn off the red LED
lcd.clear(); // Clear the LCD
buttonIndex = 0; // Reset the button index for the next round
}
irrecv.resume(); // Receive the next value
}
// Update the countdown timer
if (countdown >= 0) {
displayNumber(countdown); // Display the current countdown value on the 7-segment display
countdown--; // Decrement the countdown after displaying
} else {
digitalWrite(redLedPin, HIGH); // Turn on the red LED
lcd.clear(); // Clear the LCD
lcd.print("Time's Up!"); // Print the failure message
delay(1000); // Wait for 1 second
digitalWrite(redLedPin, LOW); // Turn off the red LED
lcd.clear(); // Clear the LCD
countdown = 9; // Reset the countdown for the next round
}
}
delay(1000); // Wait for 1 second
}
Small Video of It Working/tinkerCad Link
https://www.tinkercad.com/things/hmGlHkSS61K