For a school project named "If This Then That" I had to build an interactive object using Arduino. I decided to build a color based puzzle with a simple feedback system.
What it does (in short):
- It determines the color of the object above the color sensor.
- It checks if that color matches the correct solution.
- It either raises the "you died" sign or not, depending on weather the puzzle was solved or not.
So you might think this puzzle is easy to solve?
Well, to someone who knows how it works it kind of is, even though you can switch around the solution! But don't worry, to someone who has no clue how it works it looks like magic. In my experience a lot of people thought the puzzle was about weight, not color. And, not to forget, you are the master of the puzzle, how many clues you give is up to you.
How I (would like to) use it:
I would like to use this puzzle as an interactive part of my D&D campaign.
Step 1: Materials
You'll need the following things before you can start building:
- A (creative) brain to come up with a puzzle
- Arduino Uno
- A servo motor
- (TCS3200) Color sensor
- 7x Male - Female jumper wires (preferably all different colors, highly recommended)
- 11x Male - Male jumper wires
- powerbank (in case you want it to work without your computer/laptop)
- A (shoe) box with lid(to build the puzzle on)
- a couple of pieces of cardboard (to make the walls, pillar in the middle and you died sign)
- Wooden blocks, (at least) one side needs to be bigger than the color-sensor.
- paint: black, red, blue and green (and any additional colors if you'd like*)
- A4 piece of white paper
- A black marker
- Strong double-sided adhesive tape
- A pencil
- Instant glue
- Paring knife or Stanley knife
* small side note on the colors: initially I also used purple but the sensor couldn't distinguish red and purple so I left it out. Be aware that other colors might not work out so nicely either (but they might, I haven't tried). My sensor was cheap, a more precise sensor can probably distinguish more colors.
Step 2: The Puzzle
The most important part of this project is to have a good brain cracking puzzle.
As you can see in the first picture (the one with the intro), I painted norse runes onto my blocks (on every side except for the bottom). They have to do with the puzzle. I then pasted a simple question on the case: "True wealth is ...?".
I used the picture above to translate the runes. I also gave the translations to the people trying on my puzzle.
The solution to the puzzle:
True wealth is happiness!
So what the player needs to do is replace the treasure (block with the wealth symbol) with the block with the happiness symbol on it.
Feel free to use my puzzle but I would encourage you to come up with your own.
Step 3: The Wiring
The wiring is quite simple since there are only 2 components (and the arduino). In the description I use the minimum number of wires, if you want to use more feel free to do so. (I used more wires on the picture)
Arduino to the breadboard: (Use 2 Male - Male wires)
- connect the 5V pin to the + side of your breadboard.
- connect one of the ground pins to the - side of your breadboard.
The servo: (Use 3 Male - Male wires)
the servo has three wires: power, ground and signal.
- Power is typically a red wire, connect this wire to the + side of your breadboard.
- Ground is typically a black or brown wire, connect this wire to the - side of your breadboard.
- Signal is typically a yellow or orange wire, connect this wire to a horizontal row on your breadboard.*
The color sensor: (Use 7 Male - Female wires)
- VCC: connect this wire to the + side of your breadboard (this is power).
- GND: connect this wire to the - side of your breadboard (this is ground).
- S0: connect this wire to a horizontal row on your breadboard.*
- S1: connect this wire to a horizontal row on your breadboard.*
- S2: connect this wire to a horizontal row on your breadboard.*
- S3: connect this wire to a horizontal row on your breadboard.*
- OUT: connect this wire to a horizontal row on your breadboard.*
*I'm not telling you exactly where to pin it on the breadboard because it is easier to figure out for yourself what is best in combination with your build.
Breadboard to Arduino: **(Use 6 Male - Male wires)
It's smart to see if everything works before putting it in the case, be aware that to do that you need to take these cables out en put them back in later. Be sure to remember or write down which cable is connected to what.
- connect the row you connected to the signal pin of the servo to pin 9 on the Arduino.
- connect the row you connected to the S0 pin of the color sensor to pin 4 on the Arduino.
- connect the row you connected to the S1 pin of the color sensor to pin 5 on the Arduino.
- connect the row you connected to the S2 pin of the color sensor to pin 6 on the Arduino.
- connect the row you connected to the S3 pin of the color sensor to pin 7 on the Arduino.
- connect the row you connected to the OUT pin of the color sensor to pin 8 on the Arduino.
** the rows I refer to above here are the ones on your breadboard.
Step 4: The Code
In the attachment here you will find the code you need to run this project. The comments in the code should be clear about what it does. If you want some more in depth information on how the color sensor works (for if you want to fully understand the code) I'd recommend you to check this tutorials "How does the sensor work?" section.
Though you aren't there yet, there are a couple of things you need to adjust in the code:
- Adjust the variables.
- Adjust the if-statements to fit your riddle (if needed).
It is handy to make the pillar and paint the blocks from the next step before you adjust the variables so you don't need to do it again.
Adjust the variables:
If you built the wiring correctly the code should work, grab a piece of paper, a pen and the (painted) blocks.*
- Temporary comment the void checkRiddle function out of the code. (so that part doesn't run)
- Upload the code to your Arduino.
- Open the serial monitor, it will start running and show you R= ... G= ... B= ... (and some more things, they don't matter now)
- Hold a colored block right in front of the sensor (as close as it will be in your final design) for a couple of seconds.
- While still holding it in front of the sensor take the power off your Arduino. (pull it out of the usb port)
- Write down the outer (highest and lowest) values the color sensor measured on each color (R,G and B).
- Pick the two values that vary the least (for example R and B).**
- Count +10 on the highest value and -10 off the lowest value so the sensor is allowed small mistakes.
- Now replace the values in the code with the ones you have now. (A comments says which values you should replace)
- If you are using a different color than me, change the serial.print as well.
- Now re-upload the code to your Arduino with it using your freshly measured values.
- Test if the serial monitor nicely prints the color you just put into the code by holding the same colored block in front of it. Make sure it is consistent as long as you hold it in front of the sensor.
- If no: :( check the values, see if you need to make the range bigger.
If yes: :) the code now recognizes this color.
- Now repeat this process (step 4 - 13) until all colors you want to use are integrated in the code.
- Don't forget to uncomment the void checkRiddle function!
* go to the "the case" step first if you haven't painted them yet.
** you can be more precise if you want by using all 3 (R,G and B) but for this project that is not needed.
Adjust the if-statements to your riddle: (only if you want to change the solution)
Step 5: The Case and Painting the Blocks
The case is a somewhat tricky part but it is not hard to make, it is mostly about having the correct measurements. I'm not giving my measurements because we are (most likely) not using materials the same size.
Painting the blocks:
Paint the blocks in the colors you want to use. I recommend red, green and blue because the it is easiest to distinguish those with the sensor.
If you are not making your own puzzle paint on the runes as well.
The pillar is to (partially) hide the color sensor and the cables attached to it.
To determine the measurements of the pillar you need to measure the sides of your color sensor, and determine how high you want your pillar to be (The one you see on the picture is 3 cm high). Then draw that onto cardboard and cut it out, you can paint them if you want.
Attach the wires to the sensor BEFORE you build it into the pillar, you won't be able to reach the pins anymore.
I used the double-sided adhesive tape to attach the sensor inside the pillar. You can see this on the third picture.
Make sure the leds on the sensor are inside the pillar (not over the edge), the blocks need to stand on it later.
The "you died" sign:
Cut a piece of cardboard and write "you died" on it. As you can see in the first picture I stuck a picture of an angry man with sword on the other side. Make sure the sign has a lug on the bottom. Use the lug to tape the sign to the servo.
Creating the room:
- Paint the box you chose for the case. (Any color you like, I used black)
- Draw the shape of the room onto the bottom of the box to determine the length of the walls. (Measure the lines with a ruler).
- Determine what height you want the walls to have. The walls in my example are 5 centimeters high.
- Now you have the dimensions of your walls, draw them onto the cardboard and cut them out.
- Paint the walls. (I painted them black)
- Glue the walls onto the box, on the lines you drew before.
You now have the room set up.
Holes for the cables and the sign:
You need to make two holes in the box, one for the cables and one for the "you died" sign. and be careful! holes can't be removed once they are there.
make a hole under the place where you want the color sensor to go. Make the hole as small as possible but avoid tension on the cables, you don't want them to get loose.
The hole for the "you died" sign needs to be a bit thicker than the cardboard you are using so it can come up without hitting the case. The length of the hole is dependent on how big the sign is. Use a Stanley knife or paring knife to make the hole.
Now use the double-sided tape to attach the servo motor to the side of the box.
Step 6: Any Questions??
So now you should be able to build an awesome color based puzzle with Arduino. If you have any questions, be sure to ask!
Have fun building!