Arduino Puzzle Box

by witchmorgana in Circuits > Arduino

702 Views, 1 Favorites, 0 Comments

Arduino Puzzle Box

20200113_094330.jpg

For this project we are going to be making a puzzle box that works with music. The basic gist of it is that when you press a button it releases a tune and the Arduino calculates which buttons are pressed so that it knows what is the right and what is the wrong answer.

Supplies

1 x Arduino uno

1 x 1k Ohm resistor

5 x 220 Ohm resistors

1 xPiezo Buzzer

5 x pushbutton 6x6 mm

1 x set Jumper wires

1 x perf/strip board

1 x solderset

1 x hinge

1 x clamp

1 x small chest of drawers/wood

1x hot glue gun + glue sticks

1 x 9v battery + holder

bit of worbla

paint

2020-01-13.png

To start with you have to press your pins in your breadboard. Now connect analog 2 with a wire to the corner of the board. We are going to connect two resistors to it in a line. The first is a 10k Ohm resistor. To the other end of this resistor we connect wire to the analog ground. The second resistor is a 220 Ohm resistor we connect to the first button. Use another resistor of the same value in order to connect the second open side of the button with the second button. Keep connecting the buttons like this up to the last one. At the last one you grab a wire and connect it to a corresponding closed side of the button and connect it to the next one in line. You now repeat the process you did with the resistors only know with plain wires. Connect the first button to the analog 3,3V port on your Arduino. All in all you wil get a kind of cross patern as depicted below.

Next plug you buzzer into the breadboard and secure one side to the digital ground and the other to the 12 port. It is smart to already upload the following code into your Arduino so you can test if everything is working correctly. If it is you can start soldering the components together. Do this by taking them out of the breadboard and soldering the wires and connections together directly. If you think you need more length in between the buttons you can add some extra wire in between the resistors. The breadboard is no longer necessary at this point.

20200112_144352.jpg

After everythingis soldered it is time to make the box itself. I used a cheap set of drawers as a base for mine. I simply cut in half lengthwise and removed the back and the side I cut through. You should have two c shaped pieces now. Cut one side of one of them to use as a lid. Now rotate one of the remaining pieces so that they fit together like a lidless box and glue them together. Once the glue has set properly, drill a small hole in each side of the box for the buttons and a bigger for the buzzer in the lid.

20200113_085618.jpg
20200113_085539.jpg

Now you can get to painting the box. I did mine with a flower design based on BOTW but you can really choose any design you want. After this is done you can put the buttons inside the holes and put a glob of glue on the back of the button and the surrounding wood. The same principle applies to the buzzer but mine fit perfectly in the hole making it unnecessary. Next you grab some worbla and heat and cut it in order to make some small buttons. Carefully glue them to onto the buttons but make sure not to use to much glue because you might accidentally get the buttons stuck. Now you can paint them over in order to make them blend in with the box more.

Lastly you glue or screw, the clamp and the hinges on the box and lid connecting the two.

Now that your box is complete all you have to do is put the Arduino and battery inside it and close the lid.

Code

// This is the code for a Zelda themed puzzle/music box.

// this connects your code to the list of notes in yhe other tab

#include "pitches.h"

// this variable will ensure that the Arduino sees a longer press of the button as just one press

int same = 0;

// from here it will read your input

int k = 2;

//this is your output pin

int speaker = 12;

// below are the end melodies

int Zelda[] = {NOTE_B4, NOTE_D5, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_D5, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_D5, NOTE_A5, NOTE_G5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A5};

int ZeldaTime[] = {2, 4, 2, 4, 4, 2, 2, 2, 4, 4, 2, 4, 2, 2, 2, 4, 4, 2};

int Epona[] = {NOTE_D5, NOTE_B4, NOTE_A4, NOTE_D5, NOTE_B4, NOTE_A4, NOTE_D5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_A4};

int EponaTime[] = {4, 4, 1.5, 4, 4, 1.5, 4, 4, 2, 2, 1};

int Saria[] = {NOTE_F4, NOTE_A4, NOTE_B4, NOTE_F4, NOTE_A4, NOTE_B4, NOTE_F4, NOTE_A4, NOTE_B4, NOTE_E5, NOTE_D5, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_G4, NOTE_E4};

int SariaTime[] = {8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 8, 3, 8, 8, 8, 2, 1};

// the normal duration of a note

int BEATTIME = 300;

// the counter thats going to keep track of were we are in the solution

int teller = 0;

void setup() {

Serial.begin(9600);

pinMode(2, INPUT);

pinMode(1, INPUT);

}

void loop() {

// reads the input from the pins

k = analogRead(2);

int p = analogRead(1);

//if there isn't any button pressed there should be no tone

if ( teller == 0) {

noTone(12);

}

//if the read data corresponds with the parameters run the described bit of code

if (k > 320 && k < 350) {

rechts();

}

//""

else if (k < 310 && k > 290) {

links();

}

//""

else if (k > 260 && k < 280) {

boven();

}

//""

else if (k> 240 && k < 260) {

onder();

}

//""

else if (k > 220 && k < 240) {

a();

}

// if it doesn't(when no button is pressed), run this code

else {

// reset same so it can be used the next time a button is pressed

same = 0;

// if the counter is up to a certain number run the described bit of code

if ( teller == 166) {

zelda();

}

//""

else if (teller == 386) {

saria();

}

//""

else if ( teller == 266) {

epona();

}

//""

else if (teller == 999) {

//play this tone to mark the error

tone(speaker, NOTE_C3,BEATTIME);

//reset the counter to 0

teller = 0;

}

}

//the delay between input and output

delay(100);

// prints the values of your input in the serial monitor

Serial.println(k);

}

// this is the first melody

void zelda() {

//this sets a counter that updates itself while playing the melody so it can read it and stop when it should

for (int i = 0; i < sizeof( Zelda); i++) {

//says how long a note should last

int ZTime = 1000 / ZeldaTime[i];

//generates the tones

tone (speaker, Zelda[i], ZTime);

//creates the correct delay

int pause = ZTime * 1.30;

delay(pause);

//resets counter

teller = 0;

// prints the values of your input in the serial monitor

Serial.println(teller);

}

}

//""

void epona() {

for (int i = 0; i < sizeof( Epona); i++) {

int ETime = 1000 / EponaTime[i];

tone (speaker, Epona[i], ETime);

int pause = ETime * 1.30;

delay(pause);

teller = 0;

Serial.println(teller);

}

}

//""

void saria() {

for (int i = 0; i < sizeof( Saria); i++) {

int STime = 1000 / SariaTime[i];

tone (speaker, Saria[i], STime);

int pause = STime * 1.30;

delay(pause);

teller = 0;

Serial.println(teller);

}

}

void rec

hts() {

// if this wasn't already pressed

if (same == 0) {

// if the counter currently has one of these values add this to it

if (teller == 0 || teller == 132 || teller == 165 || teller == 232 || teller == 265 || teller == 331 || teller == 374 ) {

teller = teller + 1;

}

// otherwise set it to 999 so you hear te error noise

else {

teller = 999;

}

// set same to one so the Arduino doesn't read a prolonged press as multiple presses

same = 1;

}

// play the tone

else {

tone(speaker, NOTE_A4, BEATTIME);

}

// prints the values of your input in the serial monitor

Serial.println(teller); }

//""

void links() {

if (same == 0) {

if ( teller == 133 || teller == 254 || teller == 244 || teller == 332 || teller == 375 || teller == 221) {

teller = teller + 11;

}

else if (teller == 0) {

teller = 111;

}

else {

teller = 999;

}

same = 1;

} else{

tone(speaker, NOTE_B4, BEATTIME);

} Serial.println(teller);

}

//""

void boven() {

if (same == 0) { if ( teller == 111 || teller == 144 || teller == 233 ) {

teller = teller + 21;

}

else if (teller == 0) {

teller = 221;

}

else {

teller = 999;

}

same = 1;

}

else {

tone(speaker, NOTE_D5, BEATTIME);

Serial.println(teller);

}

}

//""

void onder() {

if (same == 0) {

if ( teller == 343 ) {

teller = teller + 31;

}

else if (teller == 0) {

teller = 331;

} else {

teller = 999;

} same = 1;

} else {

tone(speaker, NOTE_F4, BEATTIME);

Serial.println(teller);

}

}

//""

void a() {

if (same == 0) {

if (teller == 0) {

teller = 461;

}

else {

teller = 999;

}

same = 1;

}

tone(speaker, NOTE_D4, BEATTIME);

Serial.println(teller);

}