Introduction: Secrets of the Pyramid - Arduino Escape Room

Interested in Ancient Egypt and DIY projects? You're in the right place.

This how-to guide will teach you how to create your own rainbow light show escape room using Arduino's NeoPixel and keypad. Although this guide is themed around Ancient Egypt, a theme is not required. The player is prompted to answer a question displayed on the serial monitor with the user's input on a 4x4 keypad and the output is displayed on a NeoPixel strip.

Ready to jump in?

Supplies

1 Arduino UNO

1 USB Cable

1 4x4 Keypad

1 NeoPixel Strip - 8

11 Jumper Wires

Step 1: Code

Code is the most crucial aspect to pay close attention to since it is easy to make small mistakes. The code used to program this circuit requires a lot of setup before getting to the code mechanics. To begin, we need to inform the Ardunio about extra information about each of our components, the keypad and NeoPixel. Open the code file below for a better understanding.

Let's start with the keypad.

Within the code file provided, line 2 is providing the Arduino with code for the keypad using a code library. Lines 4 and 5 are declaring how the keypad is designed. Next, we need to map out each key, its value, and its location on the keypad. We do this by creating a multidimensional array named keys; this can be seen in lines 8 to 13. For our keypad to communicate with the Arduino, we declare which pin the keypad is connected to in lines 16 and 17. Lastly, we have to declare that we made an object named keypad, you see this in line 20.

Now we need to program the NeoPixel strip.

Like the keypad, we need to provide a code library specifically related to the NeoPixel. This is seen in line 24 using #include. Just like the keypad, we need to specify how many lights are on the NeoPixel strip and which pins is it plugged into on the Arduino. Using #declare, a little different from the keypad, on lines 26 and 28. Don't forget, the Arduino does not know that this component is an object so we declare it as one in line 30. Lastly, we need to specify what color lights we would like to cycle through on the NeoPixel strip using a multidimensional array (this guide uses colorShow[]). Since we have 8 lights, I used 8 colors corresponding to a rainbow. You can view my array on lines 33 to 42.

Finally, we are at void setup().

Within this function, we need to provide code to prepare the NeoPixel for use using pixels.begin(). Next, we need to turn all the lights off using a for loop by setting each light to the RGB color, 0, 0, 0. We also need to prepare the serial monitor too by setting its value to 9600. Lastly, we program the serial monitor to display a question. For this escape room, the question is, "How many main pyramids are within the Giza Pyramid Complex?". All of this code can be seen in lines 45 to 55.

The most important part, making the components do things with void loop().

First we need to declare a 'key' variable so we can record which key is getting pressed, this is on line 59. After creating an if statement stating that if the key is pressed.., we will program the Arduino to display the user's input on the serial monitor. After this, we need to declare the user's input and the correct answer, this is present on lines 66 and 67. Create an if with an if else statement for the user's correct and incorrect answer.

We can start programming the pixel light show in the first if statement for when the user gets the answer correct.

Start by displaying a message that says that the user won and then create an infinite while loop, this is on lines 71 and 73. Next declare 2 integer variables with each equaling 0 (this guide uses y and x). These will be used for cycling through the colors we declared in the colorShow array earlier. Create 2 for loops, each containing 3 statements, int i = 0, i < NUMPIXELS, and i++. This will loop the amount of lights present on the NeoPixel strip and integer i will come in handy for programing the lights. After coding a .1 second delay, use pixels.setPixelColor() to set the color and their light location, see line 80. Use pixels.show() to display the color. The x variable counts up by 1 each time it is called in the function while y stays the same. This is because we only need the values in one row of the colorShow array but in different columns. To complete this for loop, we need to add 1 to the y variable and multiply the x variable by 0. This will navigate through the colorShow array by finding the cycling through the rows but always resetting the x back to its initial value, 0. The second for loop will contain the same statements and delay as the previous one. Using the pixels.setPixelColor(), set the light to i again and each color to 0. This will turn off all the lights after one color cycle. Don't forget to show the pixel color too! All of this code is within lines 71 to 92.

The very last piece of code consists of programming the user's incorrect answer within the if else statement created earlier.

The incorrect input will display all red lights one time on the NeoPixel strip. Create a for loop that only loops once (this is on line 97) and within that loop we will create 2 for loops like we did previously. Each for loop will contain the same statement we used previously (int i = 0, i < NUMPIXELS, and i++), a .1 second delay, setting the pixel color light to i, and showing the NeoPixel. The only thing that needs to be changed is the colors, for the first for loop, program the color to red, and for the second for loop, program the color to off. Lastly, display a "Try Again" message for the player to re-try. All of this code is within lines 95 to 110.

Step 2: Hardware

Are you still there? Don't worry, the hard part is over. Next we will program the Arduino physically.

Starting with the keypad, it has 8 ports so we will need to plug them into 8 pins on the Arduino. Grab 8 jumper wires and plug the keypad into these 8 digital pins, going from left to right use pins: D9, D8, D7, D6, D5, D4, D3, D2.

For the NeoPixel strip, we need to connect it to power, ground, and input with the last 3 jumper wires. We will be using the ports at the end of the NeoPixel strip; each labeled corresponding with their function, GND, +5V, and DIN. The GND and +5V ports will of course connect to the +5V pin and a GND pin within the power section on the Arduino to power the NeoPixel. The last port, DIN, will connect to D11 in the digital section on the Arduino. This sends the code to the NeoPixel strip.

Step 3: Play!