Dynamic Combination Lock Box with Arduino Uno
For this project I wanted to not only create a Combination Lock Box, but also have the combination change as a function of time. In order to achieve this, I needed the following materials.
- Arduino Uno
- Green and Red LED
- 9V Battery
- 3V Coin Cell Battery
- DS3231 AT24C32 IIC High Precision RTC Module Clock Timer Memory Module for Arduino
- 4 x 4 Matrix Array 16 Key Membrane Switch Keypad Keyboard for Arduino AVR PIC
- 220K Ohm Resistor
I first needed to solder together the materials and create a static combination lock box. I set the rule that pressing * after entering the password submits it, and pressing ‘#’ resets the currently entered passcode. I downloaded the Keypad library for Arduino from online.
After I set up the static keypad, I want to make the password a little more interesting. What if the passcode wasn’t a set number, but depended on what time or day it was?
I decided to set a rule that the passcode would:
- Be a 3 digit number
- Be the factorial of the current day (Sunday = 1, Monday = 2, etc) modulo 101
- Be submitted by pressing * and reset by pressing #
My logic behind finding modulo 101 of the passcode was to ensure that the number was not larger than a 3 digit number. Technically, it could have been mod any 3 digit number (for instance 999), to keep the passcode 3 digits.
The function for getting the factorial of the date is below:
Please note that the function had to be type “long”. Originally, making it into an integer caused a bunch of strange errors and ultimately resulted in a negative number output. After a ton of troubleshooting, I realized that this issue was caused because the factorial number was too big to fit into the int data type.
In order to ensure that the password is always 3 digits, I first created a default keycode 000. When getting the factorial value, we replace the digits with the appropriate new keycode.
For certain entries, such as 1 or 4, the keycode would end up being 100, since the factorial of 1 is just 1, or 240, since the factorial of 4 is 24.
In addition, the DS3231 tells time and keeps the time correct once you set it the first time in the code. This also required the DS3231 library which I found online.
Please see videos below for a quick demo, and the full code on my github.
Ta-da! A dynamic keypad that takes a mathematician who knows the secret to open.