Code Lock Game

by 770427 in Circuits > Arduino

259 Views, 1 Favorites, 0 Comments

Code Lock Game

Engineering Final Project  (Jan 20, 2023 at 8_55 AM).png

This device is essentially a game where you are given a simple arithmetic equation and solve it to find the answer to a code lock before it disappears, after you find it (or don't), you submit it via the DIP switch and push button. The goal is to try and get it correct as many time in a row to set a high score.

Supplies

test.PNG

(1x) One Arduino Uno,

(1x) Piezo,

(1x) LCD display (without I2C adapter),

(5x) 330 ohm resistors,

(5x) 10k ohm resistors,

(1x) Potentiometer,

(1x) DIP switch,

(1x) Push button,

(1x) RGB LED,

Red, Black, Green, and Dark Blue wires, or any equivalent.

Schematic

Schematic1.PNG
Schematic2.PNG

This schematic was taken from Tinker CAD after building it there.

Wiring It Up (Board to Board)

inc.PNG

These are all the connections that are from the breadboard to the breadboard, along with all of the components placed. Make sure if you are using a common cathode RGB LED you connect the cathode part to ground, I will also give a separate piece code so that it can also display the colors correctly.

Wiring It Up (board to Arduino)

co.PNG

While wiring up all the connections to the Arduino, make sure to connect ground and power on both ends of the breadboard, it doesn't matter where as long as it doesn't conflict with any other wiring. After wiring and programming everything, you will also need to adjust the contrast on the LCD display via the potentiometer.

Code (Setup)

11.PNG
22.PNG

The first thing the code does is import the libraries necessary for the LCD display then assign all the variables that the project will need, first all the variables for the physical components like the button and DIP switch, then all the variables needed for the game logic its self like the answer and points, and then finally all the variables needed for the hint to be generated. After all of that is declared, the function for the RGB LED is made and we move onto void setup. In void setup all the standard stuff is done such as setting the input and output types for all of the Arduino pins being used as well as the LCD begin.

Code (Void Loop)

5.PNG
2.PNG
3.PNG
4.PNG

Essentially what the code in void loop does, is first generate 4 random numbers between 1 and 0 then assign it to 4 integers, this will be the randomly generated answer to each attempt, these will each represent a single digit on the DIP switch. Then it will set a variable named "C", to another randomly generated number (B) subtracted by those original 4 numbers which were converted into a single integer previously (A). The point of doing that is so that each problem can have its own hint that you use to solve the problem, rather than a completely random guessing game with a 6% chance of getting it right, though whether it displays the hint is reliant on a separate variable (i), when when finishing its countdown it will make it disappear and display the message "Times Up". When you submit your answer through arranging the dip switch to match what you think the answer is, it will check what you submitted to what it randomly generated then say whether you got it right or wrong.


here is the idea behind the hint system visualized:

C = A (answer) - B

if we rearrange using basic algebra,

A = B + C

displaying "B + C" is very simple hint towards the answer,


Code

Code for both common cathode and anode RGB LED.

Video Demonstration