Crack the Code Game, Arduino Based Puzzle Box

by thediylife in Circuits > Arduino

44107 Views, 339 Favorites, 0 Comments

Crack the Code Game, Arduino Based Puzzle Box

Crack The Code Game, Built Into A DIY Safe Box
Crack The Code Game, Built Into A DIY Safe Puzzle Box.jpg
The Box Opens To Put Something Inside.jpg
The LEDs Light Up To Show Correct Digits And Positions.jpg
Crack The Code Safe Puzzle Box.jpg
Safe Dial.jpg

In this Instructable, I’m going to be showing you how to build your own crack the code game in which you use a rotary encoder dial to guess the randomly generated code to the safe. There are 8 LEDs on the front of the safe to tell you how many of the digits you’ve guessed are correct and how many are in the right place as well.

The safe is initially open, allowing you to put something into the inside compartment. The Arduino and battery are housed in a separate compartment in the back. You then push the dial to lock the safe, which is done using a servo on the inside of the door. You then need to input the code by turning the dial to select the digits and pushing the dial to confirm each digit. After your fourth digit is chosen, the safe displays how many of your digits are correct and how many of them are in the correct place using the red and green LEDs on the door.

A red LED indicates a correct digit and a green LED indicate that it’s also in the correct place. So you need to light up all four red and green LEDs in order to crack the code and open the safe.

The safe keeps track of how many guesses you’ve made in order to crack the code and this is displayed once you have managed to crack it. It may sound complicated at first but it's actually not that difficult, you just need to remember and build upon your previous guesses. Most of the time you should be able to crack the code in 5 to 10 guesses, depending on how lucky your initial guesses are.

If you enjoy this Instructable, please consider voting for it in the Arduino contest.

Supplies

In order to build this Crack The Code safe box, you'll need:

You'll also need some basic tools, wood glue, a glue gun and a soldering iron.

The parts for the safe box need to be laser cut. If you don't have access to a laser cutter, consider using an online laser cutting service, they've become quite affordable and will cut and deliver the components to your door.

This is the laser cutter which I'ved used in the Instructable - K40 Laser Cutter

Assemble the Box

Box Components Were Designed In Inkscape.jpg
Box Components Were Laser Cut.jpg
Laser Cut Components.jpg
Glue Box Components Together.jpg
Gluing The Box Sides Together.jpg
Box Hinges Glued Into Place.jpg
Sand Some Of The Inside Edge Of The Box Hinge.jpg
Pieces To Hold The Back Screws In Place.jpg
Drill Holes For The Back Screws.jpg
Install The Electronic Components.jpg

I designed the safe box in Inkscape, to be cut from 3mm MDF. You could also cut the parts from 3mm acrylic or plywood if you'd like. If you use a different thickness material then you'll need to adjust the slots in the box components so that they fit together correctly.

You can download the laser cutting files here.

There are 6 panels which make up the outsides of the box, the back and front have cutouts in them for the front and back doors. The panels are labelled in the print file so that you can keep track of them.

The dial is also made up using some laser cut pieces which are then glued together.

There are three decorative panels which are stuck to the top and two sides of the box to make it look more like a safe. There are also two panels which make up the door and a divider panel which goes into the middle of the box to separate the safe compartment from the electronics compartment.

The pieces fit onto a single piece of MDF 400 x 500mm and can be divided up into smaller pieces if your laser cutter is not big enough to cut all of the pieces at once.

I started out gluing the decorative panels onto the top and sides first. Make sure that you’ve got the pieces in the correct order so that you know which are which. There are three different pieces, the top and bottom are the same, the sides are the same and the front and back are the same.

Once the panels are dry, you can assemble the box.

Make sure that the cutouts for the centre divider are on the sides. These are to run any wires from the front of the box to the back of the box where the Arduino and battery sit.

The hinges are also laser cut and are just glued into place once you’ve lined up the door. Make sure that they’re parallel to the door or you’ll have difficulty opening it. You may also need to sand a little bit off of the inside hinged edge of the door so that it doesn’t rub on the edge of the box as it moves past.

Glue the four squares into the corners behind the back panel to hole the screws for the back cover.

You can then drill the holes for the screws and started mounting the screen, the Arduino, the back cover and lastly the encoder.

Connect the Electronics

Schematic.jpg
Glue Components To The Box.jpg
Soldering Components Together.jpg
Wiring Once Complete.jpg
Servo Door Lock.jpg
Pin Headers To Arduino Uno.jpg

I designed the circuit on a breadboard in Tinkercad Circuits and added in the OLED display afterwards.

We’ve got 8 LEDs connected to the digital IO pins 6 to 13. The locking servo is connected to pin 5. The encoder connected to pins 2, 3 and 4 and the OLED display is connected to the Arduino’s I2C interface.

I used a 220 ohm resistor for each LED, soldered directly onto the negative led and I connected the components together using coloured ribbon cable to keep the wiring neat and to help keep track of which wire needed to go to each Arduino pin.

I pushed the ribbon cables through to the back compartment and the soldered some pin header strips onto the ribbon cable to plug into the Arduino.

I also mounted a power switch onto the back cover and connected this to a battery plug to connect to a rechargeable battery to power the game. You could also use a 9V battery if you'd like.

Lastly, you’ll need to position the locking servo towards the edge of the door so that it passes over the lip in the box and the arm is able to push up against the inside of the lip to lock the box. This isn’t the strongest locking mechanism but it is really simple and it works well for the game’s purpose.

Programming the Arduino

Code 1.jpg
Code 2.jpg

I’m not going to go through the code in as much detail as usual as there is quite a lot to it. I have done a detailed writeup explaining each part of it which you can find along with the code download through this link - Crack The Code Game Code.

In summary; we start by importing libraries to control the OLED display and the servo.

We then set the parameters for the display and create all of our variables. There are quite a few variables dedicated to tracking the encoder turns as these are done through rising edge interrupts on pins 2 and 3.

There are two code arrays created, on to store the randomly generated code and one to store the users current guess.

In the setup function we start the display, attach the servo, set the IO pin modes and then display the Crack The Code text animation on the display.

The loop functions flashes the LEDs and displays the message push to lock safe which then waits until the user pushes the dial to start the game. The same code is run at the end of a game which then displays the number of attempts and waits for a dial press to start a new game.

There is some debouncing code on the encoder pushbutton and once pushed, the servo locks the safe and a random code is generated. The code then calls a function to ask the user to input their guess and then another to check the guess, this is repeated until the user guesses the code correctly.

There is a function to update the code being displayed which is called every time the encoder is turned and the displayed code needs to change.

The function to generate a new code simply assigns a random digit to each of the four elements in the code array.

The function to input a code guess allows the user to select a digit using the encoder and then confirm each digit input by pushing the encoder down.

The check code guess function then looks through the guessed code and decides how many digits are correct and how many are in the correct place.

The update LEDs function switches the correct number of red and green LEDs on based on the users guess.

The startup ani function displays the Crack The Code animation on startup.

Lastly, two interrupt functions manage the input from the encoder, one incrementing the digit upwards when turned clockwise and one downwards when turned anticlockwise.

Playing the Crack the Code Game

Box Locked.jpg
Entering A Code.jpg
Code Cracked.jpg
Showing Number Of Attempts.jpg

The best way to learn how to play the game is by watching the video at the beginning, there are two examples of the game being played near the end.

The safe is initially unlocked, allowing you to put something inside it.

You then push the dial to lock the safe and generate a new code.

The guessed code is input using the dial to increment the digit and a push on the dial to go to the next digit or to confirm the code once all four digits are selected.

The LEDs on the front then light up to tell us what was correct in our guess.

You then use this feedback to make your next guess until you're able to guess the correct code and open the safe again. Once you put the correct code in, the safe unlocks and the number of attempts it took you to crack the code is displayed.

Enjoy building your own crack the code safe box. If you enjoyed this Instructable, please consider voting for it in the Arduino contest.