Introduction: Arduino Mastermind Box (Prototype)

What are we making?

The final product of this build will allow you to play a simple version of mastermind to unlock your self-made box.

The box can be made in many shapes and sizes and allows for many interesting adaptations.

In this instructable i will show you how to set up, code and build this fun chest!

Step 1: Minimal Requirements:

The following is a list of items that are needed to make the bare minimum of the build.

- An Arduino

- A device to control the Arduino

- Wires

- Resistors

- Hinge

- Breadboard or solder board

- 180 Degree, Servo motor

- 4 LED’s (Color is up to the builder)

- 4 Push button’s

- A Plan for a container

- Building materials for said container

These are the requirements to duplicate my build:

- An Arduino

- Laptop

- Wires (male -> male)

- Resistors

- Breadboard

- 180 Degree, Servo motor

- Hinge

- 4 LED’s (Blue, Yellow, Orange, Red)

- 1 RGB LED

- 4 Push buttons

- IKEA Kvarnik 3 set box (http://www.ikea.com/us/en/catalog/products/00256675/#/60256663)

- Plush toy

- Screws (2,5 x 10)

- Elastic

- Cylindrical pegs or Cylindrical pieces of wood

- Glue

- Saw

- Knife

- Soldering equipment

Step 2: Planning Your Box

Before you can start this build you will have to plan out what kind of container you would like to use and/or build.

Your box can be of any size but make sure to have enough space if you want to integrate the Arduino and/or Bread/Solder Board.

My build:

For my own build I used the Kvarnik 3 set box from IKEA due to its practicality for having 3 box sizes to experiment with. To see how I mapped and planned out my box, see the pictures above.

*The rest of the tutorial will be based on the box i used.

Step 3: Building: Measuring, Drawing, and Cutting the Box

Grab the lid of the largest box and cut out a circle equal to the size of the lid of the smallest box. Cut careful as we want to save the disc you cut out. You can use a sharp knife, strong scissors or a saw to cut out the circle.

Step 4:

Cut a rectangular piece of wood and grab your hinge, 4 screws and a screwdriver or drill. Make sure the piece of wood is large enough to support the screws but small enough not to block the lid from sitting on the box. Use the left over cut out as the lid and screw it in place using a screwdriver and the hinge.

Step 5:

Get your drill and lid and drill out the holes for the LEDs, Push Buttons and 2 screws in the top of the lid for the mechanism.

Step 6:

Set the biggest lid aside for now and grab your cylindrical piece of wood and a saw. Cut 4 pieces from the piece of wood 5.5 cm long.

Step 7:

Grab your medium size lid and screw your newly cut pegs into a square at the edge of the of the lid creating a platform for your smallest box to rest on.

Step 8:

As the last step of the box creation we need the smallest box, cut out a small square near the rim so the servo motor has room to move. The cut needs to be about 3 to 4 cm’s deep. And 5cm’s wide.

After cutting out the segment use a drill to drill out a hole for the RGB led. And poke 2 holes near the cut out to create a rest for the servo-motor.

Step 9: The Arduino Setup

This step will visually show how to connect all the wires to make sure all the buttons and LED’s will work properly with the code.

The fritzing diagram shows how all connections have to be made.

When assembling this circuit make sure to have long cables connected to the LED’s and buttons as they will need considerable range to be properly mounted to the lid.

Step 10: Soldering

All the LED’s and Buttons will have to be soldered to increase the range at which it can be mounted.

The servo should have no problem reaching the bread or solder board.

Step 11: The Code

Copy and paste the code from step 14 to your Arduino project and verify it, upload it to the Arduino when your project is done.


Step 12: Assembly

Now that all components are ready it’s time to assemble the box and circuitry.

1. Put your Arduino and Bread/solder board into the largest box.

(If you’re not using Wi-fi, cut a hole for the USB cord to fit through)

2. Add all the buttons / LED’s to the largest lid.

3. Connect all lid wires to the circuitry.

4. Connect the RGB led and Servo motor to the circuitry.

5. Put the medium lid platform over the Arduino.

6. Put the smallest box on the platform.

7. Line up the pieces.

8. Pull the elastic band over the rim to make the lid automatically pop up.

(You might have to add a piece of wood underneath the elastic band to change the bands angle, it might not pop up if the angle is to tight.)

9. Upload the code to the Arduino.

Step 13: Play!

Now that all the pieces are in place you are ready to crack the code!

The buttons represent 1,2,3 and 4 from left to right.
If you guess a number and position correctly the LED will turn on.
if you guess the number correct but it is in the wrong position the LED will blink slowly.
If you guess the number incorrectly and is not present in the code the LED will blink fast.

If you manage to get all 4 digits correct and in the right place the box will pop open and reward you for your effort!

I hope you enjoyed this instructable and that it might inspire you to make great and fun projects!

Step 14: The Code

int led1 = 2; // This sets numbers to names. This is used later for led[i]

int led2 = 3;

int led3 = 4;

int led4 = 5;

int i = -1; // Sets i to -1, used to only randomize the code when setup(); is called.

int j = 0; // Resets j to 0 for a new round, j is used to as an index when the right code is called.

#include // includes the servo library

Servo servo1; // Name servo

double code[4] = {1, 1, 1, 1}; // Creates an array called code, it's consists of 4 digits.

double correct[5] = {0, 0, 0, 0, 0}; // Creates an array called correct, it's consists of 5 digits and is used to see if all numbers have been guessed correctly.

double led[4] = {2, 3, 4, 5}; // Creates an array called led, it's consists of 4 digits and is used to activate the leds when the index is called.

void setup() {

// Setup is also used to reset the program when the correct code has been entered.

// initialize serial communications at 9600 bps:

Serial.begin(9600);

double correct[5] = {0, 0, 0, 0, 0}; //Resets the correct array

// Sets the buttons (9 to 12) to be recognized as input and the LED's (2 to 5) as output.

pinMode(9, INPUT);

pinMode(10, INPUT);

pinMode(11, INPUT);

pinMode(12, INPUT);

pinMode(2, OUTPUT);

pinMode(3, OUTPUT);

pinMode(4, OUTPUT);

pinMode(5, OUTPUT);

servo1.attach(1); // attaches the servo on pin 1

servo1.write(90); // Set the Servo to this position

delay(2000); // Hold this position for 2000ms to reset the lid

servo1.write(170); // lock the lid

i = -1; // randomizes the code if i = -1

digitalWrite(led1, LOW); // Resets all LED's

digitalWrite(led2, LOW);

digitalWrite(led3, LOW);

digitalWrite(led4, LOW);

analogWrite(8, random(0,255)); // Set the RGB to a random color

analogWrite(7, random(0,255));

analogWrite(6, random(0,255));

}

void generateCode()

{

// randomize the code if i = -1

randomSeed(random());

for (int i = 0; i < 4; i++)

{

correct[0] = 0;

correct[1] = 0;

correct[2] = 0;

correct[3] = 0;

correct[4] = 0;

code[i] = random(1,5);

}

//debug code so you don't have to figure out the code.

Serial.println("code");

Serial.println(code[0]);

Serial.println(code[1]);

Serial.println(code[2]);

Serial.println(code[3]);

}

void retry() {

// if code is incorrect this function is called

Serial.begin(9600);

double correct[5] = {0, 0, 0, 0, 0};

i = 0; // i set to 0 so code is not reset.

digitalWrite(led1, LOW);

digitalWrite(led2, LOW);

digitalWrite(led3, LOW);

digitalWrite(led4, LOW);

}

void loop() {

if (i == -1){

generateCode(); // generate a new code if i = -1

i++;

}

// code 1 ////////////////////////

// The following code will read out if the input is equal to the number in the current position.

// if the number and position is correct the led will turn on and the array "correct" will be set to 1 for that position

// if the number is correct but the position is wrong the led will blink slowly and continue to the next position, Correct is not set to 1

// if the number is not correct at all the led will blink fast and continue to the next position, Correct is not set to 1

// if all correct is = to 1. The leds will blink and the servo will change position to allow the box to open. the function setup is called for the game to begin anew.

// if any of the numbers in the array "correct" are 0 the red led will blink once and the function retry is called.

if (digitalRead(9) == HIGH) // if button is pressed >

{

if (code[i] == 1){ // if code is correct to button pressed

digitalWrite((led[i]), HIGH); // turn ledon

i++; // advance index

correct[i] = 1; // set correct index to 1

delay(250);

}

else if (code[1] == 1 or code[2] == 1 or code[3] == 1 or code[4] == 1){

Serial.println("right, but wrong location");

digitalWrite((led[i]), HIGH);

delay(500); // delay so button won't be input to often during 1 press.

digitalWrite((led[i]), LOW); // Led slow blink code.

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

i++;

delay(250);

}

else if (code[i] != 1){

Serial.println("wrong");

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

i++;

delay(250);

}

}

if (digitalRead(10) == HIGH)

{

if (code[i] == 2){

digitalWrite((led[i]), HIGH);

i++;

correct[i] = 1;

delay(250);

}

else if (code[1] == 2 or code[2] == 2 or code[3] == 2 or code[4] == 2){

Serial.println("right, but wrong location");

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

i++;

delay(250);

}

else if (code[i] != 2){

Serial.println("wrong");

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

digitalWrite((led[i]), HIGH);

delay(200);

i++;

delay(250);

}

}

if (digitalRead(11) == HIGH)

{

if (code[i] == 3){

digitalWrite((led[i]), HIGH);

i++;

correct[i] = 1;

delay(250);

}

else if (code[1] == 3 or code[2] == 3 or code[3] == 3 or code[4] == 3){

Serial.println("right, but wrong location");

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

i++;

delay(250);

}

else if (code[i] != 3){

Serial.println("wrong");

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

i++;

delay(250);

}

}

if (digitalRead(12) == HIGH)

{

if (code[i] == 4){

digitalWrite((led[i]), HIGH);

i++;

correct[i] = 1;

delay(250);

}

else if (code[1] == 4 or code[2] == 4 or code[3] == 4 or code[4] == 4){

Serial.println("right, but wrong location");

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

delay(500);

digitalWrite((led[i]), LOW);

delay(500);

digitalWrite((led[i]), HIGH);

i++;

delay(250);

}

else if (code[i] != 4){

Serial.println("wrong");

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

delay(200);

digitalWrite((led[i]), LOW);

delay(200);

digitalWrite((led[i]), HIGH);

i++;

delay(250);

}

}

// checks if the entire code is correctly entered.

if (digitalRead(led4) == HIGH and correct[1] == 1 and correct[2] == 1 and correct[3] == 1 and correct[4] == 1) {

if (j <= 20){

digitalWrite(led2, LOW);

digitalWrite(led1, LOW);

digitalWrite(led3, LOW);

digitalWrite(led4, LOW);

delay(100);

digitalWrite(led2, HIGH);

digitalWrite(led1, HIGH);

digitalWrite(led3, HIGH);

digitalWrite(led4, HIGH);

delay(100);

j++;

i = 0;

int i = 0;

servo1.write(90);

}

else if (j > 20)

{

j = 0;

setup();

}}

// if not correct, retry

else if (digitalRead(led4) == HIGH and correct[1] == 0 or digitalRead(led4) == HIGH and correct[2] == 0 or digitalRead(led4) == HIGH and correct[3] == 0 or digitalRead(led4) == HIGH and correct[4] == 0) {

delay(2000);

digitalWrite(led4, LOW);

digitalWrite(led2, LOW);

digitalWrite(led1, LOW);

digitalWrite(led3, LOW);

delay(600);

digitalWrite(led4, HIGH);

delay(600);

i = 0;

int i = 0;

retry();}}