Intro: Arduino Treasure Hunt
I have always liked movies like National Treasure and the like. Ones that has a team of people that are running all over trying to solve some riddle or a string of riddles to, get the gold, get the girl, save the world, become instantly rich,etc.
I have three adult children and getting them gifts for Christmas or birthdays has always been a challenge. The average gift holds their interest for only a short time and I wanted to make something that they would remember and talk about with their children. So I created a treasure hunt that wound its way around our town placing clues and keys in libraries, police stations, school play grounds, friendly businesses, parks, memorials and anyplace else that catches my fertile imagination. The first version was a horrible failure. The kids were good sports about my mistakes and we muddled through. Version two was much better as I got my pride out of the way and asked for help. Version three backslid some as I started using Arduinos to make the final prize more flashy. The fourth version was a total success. This is an instructable based on the fourth version or how I built my "Treasure Box"
I have to tell you up front that the average hunt that I create takes me about a month or two to create on paper and then another month to get everything set and placed and people to help etc. But this instructable is for just the box. How your imagination applies it is up to you. For more information on how I created my treasure hunts go to my companion instructable on tips and tricks on how to create a treasure hunt for this box.
This box is basically a box with a drawer off the bottom. Using an Arduino Mega that has an I2C LCD and a 4x4 keypad attached as a controller and five reed switches that are used to give input when the "keys" are inserted (more on this later) A standard hobby servo unlocks the drawer.
Why the Mega? Glad you asked. I used the Arduino to actually print the clues on the LCD screen. Since I am using a 4x20 (4 lines, 20 spaces) LCD display I have to make "screens" that flash onto the screen and then pause some and then flash another screen. This I have found takes a large amount of memory. The input/outputs could be handled by and Uno or similar but the text strings suck up memory fast. If you are better at Arduino than I am then you could load the text strings to a SD card and then read them in as needed. My Kung Fu is not that strong yet but patience grasshopper and you(I) will learn.
Step 1: How the Treasure Box Works...
The box is attached to a post or fence or some sort of fixed object that the treasure hunters are introduced to right away. This tells them where the final prize is and what they need to do (more or less) to unlock the drawer and retrieve the prize. In this case the team is four collage aged adults so the prize was four gift cards to a local hangout for lunch.
Five items are hidden an need to be recovered. (this is where your imagination comes in) The first is the "Ring of Power". This is simply a piece of wood that I cut out with a band saw then recessed a nickel sized magnet in the face then placed a sticker over it to conceal the magnet. (see picture)
The other four are puzzle pieces that are cut from the face plywood of the box and neodymium magnets are glued flush in holes drilled in the backs of each piece.
The "Ring of Power" is discovered first. This is waved across a secret spot just below the keypad. The magnet activates a reed switch and the Arduino turns on via a Pololu power controller (more on this later). The LCD lights up and tells the team where to look for the next clue. After a short time the Arduino turns itself off to save power also via the Pololu controller. The searchers run about looking and solving riddles and finally the first "key" is recovered. The ring of power is waved over the spot again to power up the box, then the key is placed in the face of the box. The key is recognized and then the next clue is given. There is a opportunity to repeat the clue if needed. If not, the Arduino times out, triggers the Pololu switch and powers down. This happens for the rest of the keys until all four are placed in the face of the box.
For the final hurrah, I created a riddle that is a list of questions. Each question's answer is a number. Add, subtract, multiply and divide the various answers to give the final number. That number is a code that when entered via the keypad opens the drawer and presents the prize to the team. Of course there is much more drama in the actual hunt but that is the basics.
Step 2: What You Will Need...
1) A wooden box with a drawer in the edge. I built mine. I'll give you the general measurements but if you want to reuse something else or build it a different way to get the same results feel free. (more details later)
2) Arduino Mega(or compatible)
3) Pololu Mini Pushbutton Power Switch with Reverse Voltage Protection (part number 2808). This can be plugged directly into the mega board with header pins but can be remote mounted. This is how the Arduino turns itself off. More on this later. https://www.pololu.com/product/2808/resources
4) 5 reed switches. These are switches that are triggered by magnets. They are usually used in door alarm contacts. I got mine by careful scavenging but they are available at Jameco.
5) a 4x4 keypad with array wiring. This can be made up with 16 switches if you want but for about $4 I got a five pack of them from Ebay.
6) one Red LED and 250 ohm resistor
7) one Green LED and 250 ohm resistor
8) standard hobby servo - available at Jameco or Frys or any RC hobby store
9) Plexiglas pieces about 3"x4"x 1/16' this is for a backer for the puzzle pieces that are cut out of the front of the box. Home depot or Lowe's or any good hardware store will carry small sheets of this. The thinner it is the better the magnets will work through them.
10) battery - I used a rechargeable lead acid 6 volt battery because I had it lying around but 4 AA batteries will work, just for not as long. Depending on how you program, either will work just fine.
11) (1) nickel sized magnet. A large diameter magnet is needed to be able to trigger the reed switch though the 1/2 plywood. The magnet that I used comes out of the transmission pan of Mercedes Benz cars. Go to a dealership and ask the guys in the back. They probably have a stack of them they will give you.
12) (6) neodymium magnets around 1/4 inch in diameter. I sacrificed several K'nex pieces to get the perfect magnets
11) wire, connectors, misc hardware, tools, time, a fair bit of imagination, and patience.
Step 3: Building the Box Face...
I built the face of my box out of birch plywood and used hardwood for the sides simply for the reason that I wanted this thing to be reused several times. The birch plywood I bought from Hughes Hardwoods in Sacramento. The hardwood pieces are carefully scavenged pieces from pallets. I started with this step because the size of the electronics would determine the size of the box. In retrospect I could have made the thing about 1/3 the size but it does have a sense of wow when you come upon it during the hunt.
Start with the face plywood and build everything else around it. Mine is 19"x 14" +/-
Locate the center of the plywood side to side and then about 1/3 down from the top. As you see in the picture there are three things that need to be in line on the back. From the top is the LCD, then the keypad and on the back is the "magic spot" where a reed switch is located to activate the Pololu switch.
Using a scroll saw I cut a hole for the LCD screen to press fit into. You will be gluing it in from the back but a tight fit makes it look cleaner. I used a router to round over the hole sides.
Figure out where you want your puzzle pieces to be. They don't have to be puzzle pieces. They could be random shapes just so long as each will only fit in its own hole and only one way. I don't recommend a circle, a perfect square, or triangle because those shapes can be inserted in more than one way. This may make the magnets not line up with the reed switches when the key is inserted.
I fired up the scroll saw and randomly cut 4 puzzle pieces out of the face. One of my pieces is shaped so that it can fit in the hole two different ways. (oopps!) I used a bit of paint to identify which way the piece is supposed to be inserted.
Just below the hole for the LCD is a jelly bean shape hole to allow the ribbon cable for the keypad to pass through to the back where all the electronics are. I situated the keypad where I wanted it to be then lifted the edge and marked where the hole needed to be. I made the hole wide so that when the sticky backing of the keypad was stuck to the wood the ribbon wouldn't leave a lump under the keypad. I also had to bevel the edges some to get it to lay flat. Try it out on a scrap piece of wood to see how it will lay and where you want it to be.
Step 4: Building the Box...continued
Looking at the front and the back you see where I cut the holes for the puzzle pieces. I spaced them out so that I could attach the pieces of Plexiglas and still have room around for other stuff. After painting the face green, I screwed the Plexiglas pieces over the backsides of the puzzle piece hole to cover them. Four screws is probably more than is needed to hold the Plexiglas but I wanted it to lay flat so I overdid it.
The puzzle pieces have two 1/4 inch holes drilled just deep enough for small neodymium magnets to be pressed in flush then super glued. After the magnets are glued into the pieces, they are placed in the holes and viewed from the back. A small piece of steel strip is then glued to the Plexiglas to align with the center magnet just enough for the center magnet to stick to it. The reed switches are then glued in alignment with the upper magnet so that they are triggered by the second magnet. I found that two magnets are required because the pull of the first magnet to the metal weakens the field so that the reed switches are not triggered. So one magnet for sticking and one for triggering.
The edges started out as oak 2x4s from a pallet. A couple of trips through the planer and the the table saw and I had a step cut into the top and bottom edges that allows the 1/2 inch plywood to sit below the surface by about 1/8 inch. To make it so that I cold get it apart again I used machine screws with inserts glued into the side pieces.
The inserts are computer circuit board standoffs and the screws that fit them are #6-32
After the frame is attached to the box, flip it over and mount the keypad and the LCD screen to the face. I used a little hot glue on the LCD screen to make sure that it wouldn't pop out.
Step 5: About the Pololu Switch...
The Pololu switch has several terminals and not all of them are used here.
Vin and ground is where the battery is connected
Vout and ground are connected to the Vin and ground of the Arduino.
DO NOT CONNECT POWER FROM POLOLU SWITCH TO 5V PORT ON ARDUINO YOU WILL TOAST THE BOARD. The Vin port on the Arduino passes the incoming power through the power regulator so that the board only has 5V. Connect the Vout of the Pololu to the Vin of the Arduino.
The reed switch that is below the keypad on the backside connects +v of the battery to the "on" terminal of the Pololu switch.
Pin 12 on the Arduino is connected to the "off" terminal on the Pololu switch. When the sketch runs a timer and decides it is time to shut off the Arduino it digitalWrites pin 12 HIGH and this turns off the Pololu switch. Once the Arduino powers down pin 12 drops back to zero and the Pololu switch can be reactivated with the magnet.
Wiring diagram to follow in later slides.
(*inside program trick)
I know you're thinking,"If the Arduino shuts off then doesn't it start back from the beginning of its program? How do you get it to remember where it was and not have to view the previous clues every time the ring of power is used?"
Excellent question grasshopper. This is jumping ahead some but here is a taste of the secrets of Arduino Kung Fu.
I use an EEPROM.write to save a number to the onboard EEPROM before shutdown. The EEPROM is a section of memory that is not affected when the power is lost to the board. Every Arduino has one. When the board restarts with the use of the ring of power, The first part of the startup is to read the EEPROM number back in to tell the sketch where it left off. The sketch will skip ahead to that spot. Easy squeezy. More on this when we get to the sketch.
Step 6: Component Connections...
If you use the code that I wrote, the electronics attach to the Arduino like this.
2,3,4,5 are connected to the vertical collums of the keypad from left to right
6 is the PWM output to the servo
7,8,9,10 are connected to the horizontal rows of the keypad
12 is connected to the Pololu switch in the OFF port
A0, A1 are connected to green led with resistor and red resistor respectively
SDA, SCL, +5v and gnd on the Mega are connected to SDA, SCL, +5v and gnd of the I2C LCD back
ground is connected to one side of all of the key reed switches.
22 is connected to key 1 reed switch and ground (when the reed switch is triggered 22 is LOW)
23 is connected to key 2 reed switch and ground
24 is connected to key 3 reed switch and ground
25 is connected to key 4 reed switch and ground
(see the drawing for all of the connections)
You may ask why did you spread out the connections all over the Mega instead of concentrating them more or less in one spot. I originally wrote the program for an Uno. When I discovered that I had more program than would fit in an Uno I switched to the Mega but didn't alter the pin connections. Since the program already worked, why fix what is not broken.
Step 7: The Keypad...
For those who have never used an array type keypad this is how they work.
In an effort to reduce the number of wires that come out of a key pad, conductors are laid out in rows and columns. This type of keypad takes some Kung Fu to make them work but it also reduces the number of pins that you need to use.
In a standard wired key pad you have one common wire and one wire for each button. In this case that would be a total of 17 wires.
In an array wired keypad you have only 8. Here's the catch. The micro controllers has 4 pins set as outputs (the rows) and 4 pins set as inputs (the columns) You can only read 4 buttons at a time.
"What???!!! What good is that to me I bought a 12 button keypad!"
Only one of the row is turned on at a time. For example the first row is turned on, this puts power to one side of each button on the top row (1,2,3,A) If 1 is pressed then you know power is flowing from output 1 to input 1. If 2 is pressed then you know that power is flowing from output 1 to input 2. If 3 is pressed then power is flowing from output 1 to input 3, and if A is pressed then power is flowing from output 1 to input 4.
If you were to turn output 2 on that would put power to one side of each of the buttons on the second row (4,5,6,B) if 4 is pressed power flows from output 2 to input 1. If 5 is pressed power flows from output 2 to input 2. If 6 is pressed then power flows from output 2 to input 3 and lastly if B is pressed power flows from output 2 to input 4.
How do i read the whole key pad you ask. Another good question grasshopper. Here is how it works. The micro controller thinks very fast, so what happens is this. Row one is turned on which is connected to 1,2,3,A and all four of the inputs are looked at. If no power is found at any one of the inputs then that row is turned off and then row 2 is turned on which is attached to 4,5,6,B. Then all four inputs are looked at again. If no power is noted at the inputs then that row is turned off and row three is turned on which is attached to 7,8,9,C and the inputs are again looked at. If no power is again noted at the inputs then that row off and the fourth row is turned on which is connected to *,0,#,D. and again the inputs are looked at for power. If no power is noted then row 4 is turned off and the processes starts all over again with row one.
*Important part - If you turn on more than one row and a button is pressed then there could be two possibilities as to which button is pressed since there is more than one source power coming from two different rows. Say you turn on row 1 and row 2. If you press the 1 button then power will be seen at input 1. If you press button 4 then input 1 will see power again. Got it?
Here is where it gets cool. The micro controller moves so fast that no matter how fast you push the button the scan will see your finger on a button a bunch of times before you can get your finger off the button. In the program you write an if statement. "If my finger is seen on a button then stop the scan and wait for the button to be released (this keeps you from entering 5000 button presses before you can get the finger up.) Once your finger releases the button then using the row and column chart set a variable to reflect what number was pressed. Then go back to scanning the keypad again waiting for another button press. An if statement has to be written for every combination of row and column.
I can hear some of you saying "There is a library for that already made, its called keypad.h" That is correct, there is, however I didn't want to constantly be scanning for a button press, only at certain times and this way it is somewhat easier for me to control, or at least in my head it is. The scan is written into a separate function from the loop function. At certain times during the loop if I want to look for key presses then I send the program to that function which then runs the scan once. I have written my loop to progress in steps and ignore all the rest. So as the loop comes around the and that step is active then the scan is called for. Mostly easy squeezy.
Step 8: Attaching the Electronics..continued
I used a hot glue gun to attach the Arduino to the back of the face, then ran each set of wires along the board and glued them down every so often with a hot glue dot. Use two dots of glue with the Arduino in case you want to remove it at a later date. A big blob will make it very hard to remove it if you need to re solder something. (hard won knowlage) Also you'll notice that all my wires have been braided to keep them together. When you have daughters you learn how to braid.
Screw on both pieces of Plexiglas centered over the puzzle piece holes. Then insert a piece into its hole and view it from the back. From the back side, hot glue a small strip of metal across the center magnet of the puzzle piece so that your piece sticks to it. Connect the reed switch to a volt meter. When you place the reed switch in range of the magnet it will close. Glue it down while still connected to the volt meter so that know that it will be triggered when the piece is inserted. Do this for all four pieces.(see picture)
I picked a random location for the "ring of power" reed switch because of space. You can place it wherever you like. I placed mine below the keypad so the "ring of power" must be placed over that spot. This reed switch is wired to the Pololu switch. This reed switch connects the +6 volts of the battery to the "on" port of the Pololu switch. On the opposite side of the plywood I placed a sticker that had the same insignia as the ring of power. Then instructed the team to "place the ring seal against the seal on the box" This way I knew that it would be triggered consistently.
I glued my battery off to the side because that is where it fit. Depending on what kind of battery you are using you may need to shuffle things around some.
Step 9: Creating the Drawer...
The drawer that I made was not really a drawer but more like a flat piece of plywood on runners
Again I overdid it and used ball bearing drawer slides because I had them laying around. The drawer has an edge piece to it that blends in with the other three side. When release by the hobby servo, the drawer via gravity slides down out the bottom of the box and stops with a satisfying bang that never fail to make the team jump and me grin.
I mounted the drawer slides on the back and then the drawer on to it. A "U" shaped cutout allowed the servo and latch to be mounted in the middle of the drawer. There is probably an easier way to make this but it is the materials that I had so looking at the pictures you can see how it works.
The latch was scavenged from an old copy machine but I have seen almost the same thing for sale at Home Depot for a drawer latch. Be creative.
Step 10: Mounting the Box to Something...
In the beginning I had the idea that I would mount this to a iron fence or something like that. I ended up getting the use of a gated vestibule on main street. It was a door to a business that was blocked from the inside. They had placed a fancy iron gate across the entrance which gave me about 5 feet of covered area that was gated. I couldn't attach to anything in there so I made a quick pedestal. This is a brake rotor from a ford truck. A piece of flat stock to span the hole. A 5 foot scrap piece of 3 1/2 inch square tube. (Who doesn't have a piece of this lying around right?) and some some flat metal stock pieces welded to give the box something to screw to.
Step 11: The Two Codes...
I have two pieces of code. The first one uses the same wiring as the second.
Code A - clue_post_servo_calabrate.INO
When you load this code it sets the servo position in approximately the center of the sweep on a 180 degree servo. The LCD screen asks you to set the unlock position. Attach your box with servo and latch installed to the mega. Adjust the position of your servo until the lock mechanism is fully unlocked.
Press B on the keypad and the program shifts to the lock position and resets the servo to the middle position again. Adjust the position of your servo until the lock mechanism is fully locked but not up against a stop and humming.
Press the A button and the program will shift back to the unlock setting that you previously entered.
Press the B button and the program will shift back to the lock setting that you previously entered.
When you found the correct end points, write the numbers down because you will be entering them into the main sketch.
Step 12: The Box Code...
Here is the final code to make the whole thing work. Remember the servo numbers that you wrote down? In the top declaration section of the code look for two variables; servoClosed and servoOpen. Alter the numbers to those that you wrote down for your servo installation.
Some things about operating this. Since you will be altering the clues you will want to "playback" the code to see how everything looks. Every time the screen shows "shutting down..." the EEPROM is being updated to the current place in the program. If you wish to reset the EEPROM and view the program from the beginning then turn the power off then on while holding down the "A" button until "EEPROM reset to 0" is displayed. This resets the EEPROM and starts the program over from the beginning.
If you are all done with your alterations and want to reset the EEPROM and then shut it down so that your team discovers it and it starts up at the beginning; turn the power off then on while holding the "B" button until "EEPROM reset to 0" is displayed. A few seconds later the Arduino will shut itself off. With the next wave of the Ring of Power the program will start from the beginning.
The clues have been made generic so you will have to adapt them to your situation. The GPS numbers are also generic so you will have to adjust them as well to your situation. Feel free to modify, cut , paste, adapt, add to or subtract from this code for your application. It is a pretty large bit of programming so I have taken care to comment as much of it as possible. When modifying it set your Arduino IDE to display line numbers by going to file - Preferences and check the display lines numbers. Note where the changes that you are making are by line number so that you can go back and adjust if you need. Also it would be a good idea to make a second copy of this and change that one so if you alter the sketch so that it doesn't work anymore you have something to look at to figure out what went wrong. I am working a second instructable on the building of the actual treasure hunt itself and I will post the link here when that is complete as a companion.
This took me 5 months to put this together so be patient and go slow to make sure that your treasure hunt works as smoothly as possible. Good Luck
A Machine Tech