Step 2: Program The Arduino

This section assumes that you know how to connect your Arduino microcontroller to you computer, compile and upload a sketch. If you don't know how to do that you probably shouldn't be doing this Instructable. But spending some time on this page and doing some of the examples and tutorials there might bring you up to speed.

We're going to upload our sketch before doing any of the electronics so we can test the electronics as we go.

#1: Download
Download the file secret_knock_detector.pde at the bottom of this section and copy it to your sketchbook. (Or view the text and cut and paste it into a new sketch.)

(Tip: If the name of the downloaded file is something like "BARS5HS13H8SW.tmp" simply rename it to secret_knock_detector.pde. and you're good to go.)

Open the sketch and compile it.  It should compile properly the first go, but it's good to be sure.

Connect your Arduino and upload the sketch.

If you have any trouble, check the troubleshooting section at the Arduino site.

Code overview:
For the curious, here's a look at a few bits of code if you're interested in tinkering:
(If you're not curious, go to the next section)

about Line 28: const int threshold = 4;

This is the sensitivity of the knock detector.  If you get a lot of noise, raise this (up to 1023), if you're having a hard time hearing knocks you can lower it (as low as 1).

about Line 29: const int rejectValue = 25;
about Line 30: const int averageRejectValue = 15;

Both of these are used to determine how accurately someone has to knock.  They are percentages and should be in the range of 0-100. Lowering these means someone must have more precise timing, higher is more forgiving.  averageRejectValue should always be lower than rejectValue.

Settings of about 10 and 7 make it hard for two people to knock the same knock even if they know the rhythm. But it also increases the number of false negatives. (ie: You knock correctly and it still doesn't open.)

about Line 31: const int knockFadeTime = 150;

This is a crude debounce timer for the knock sensor.  After it hears a knock it stops listening for this many milliseconds so it doesn't count the same knock more than once.  If you get a single knock counted as two then increase this timer.  If it doesn't register two rapid knocks then decrease it.

about Line 32: const int lockTurnTime = 650;

This is now many milliseconds we run the motor to unlock the door.  How long this should be depends on the design of your motor and your lock.  It's okay if it runs a little bit long since I've designed a simple slip clutch into the design, but it's better for all the parts if it doesn't run too much.

about Line 34: const int maximumKnocks = 20;

How many knocks we record.  20 is a lot.  You can increase this if your secret hideout is protected by devious drummers with good memories.  Increase it too much and you'll run out of memory.

about Line 35: const int knockComplete = 1200;

Also known as the maximum number of milliseconds it will wait for a knock.  If it doesn't hear a knock for this long it will assume it's done and check to see if the knock is any good.  Increase this if you're a slow knocker.  Decrease it if you're a fast knocker and are impatient to wait 1.2 seconds for your door to unlock.

about Line 39: int secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 5.....

This is the default knock that it recognizes when you turn it on.  This is weird rhythmic notation since every value is a percentage of the longest knock.   If you're having a hard time getting it to recognize "shave and a hair cut" change this to {100,100,100,0,0,0...  and a simple sequence of 3 knocks will open it.

about Line 51:  Serial.begin(9600);
about Line 52: Serial.println("Program start.");

Uncomment these lines to see some debug info on the serial port.  There are a few other lines of debugging code set throughout the rest of code that you can uncomment to see what's going on internally.

Be sure to set your serial port to the right speed.

The rest of the code is commented so you can see how it works but you probably won't need to change it if you aren't changing the design.

I have made a prototype, still have to assemble it to my door. I used a self made relay module to power my motor. Thanks for the instructable...... it works like a dream.
<p>why I do like you, but it does not work.reply shivaduy248@gmail.com,please</p>
<p>Cheers, I turned this into a geocache, and built it into a ammo can =)</p><p>https://www.youtube.com/watch?v=ufI1bx3c7rg</p>
<p>My version is installed in a fridge and using an electromagnetic lock - keep those office thieves away! </p><p>http://ink361.com/app/users/ig-240858804/jas_cal/photos/ig-870056726990911200_240858804</p>
<p>Guys - thank you for this tutorial! I used the code to program a secret sliding library door project and it works great. The secret knock is the part people like the best. People giggle when I show it to them. I owe you a beer!</p><p>http://www.instructables.com/id/Automatic-Secret-Library-Door/</p>
Fantastic work! I love how well the bookshelf hides your secret room. Never would have guessed it was a pocket door.<br><br>Thanks for sharing!
<p>Here we are! Finally done after months of procrastination! I added a buzzer and a third led (not in the picture). The code was changed very slightly but it works perfectly. A latch in the back hangs it on the door. I must thank you, however for the genius of this invention.</p>
<p>hi sir, how did you do this one. where do i put the new code for the led and the piezo.. please, needed for a presentation, our prof. did not give us enough time to master this. and it serves as our final exam.. thank you in advance..</p>
<p>Beautiful work! Thanks for sharing!</p>
<p>here is my little one in an iphone box<iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/HhrNM2kSQzk" width="500"></iframe><br></p>
I have modified the Arduino code to add the functionality of ''Users''. What it does is that it let you program a secretCode for four users. For example: If you push the button X times, after 4 seconds the green LED will blink X times and then you enter program mode (simply do your secret code). I also added the functionality to take a picture when your sequence is bad with LabVIEW and a WebCam camera. Then the picture is saved in my folder and pictures are incrementing with numbers.
Looks great, and really nice added features! I had done a version with his &amp; hers knocks, but this goes well beyond that!<br><br>Fantastic job!
I saw your project some time back and I thought it was great. I decided to take it and improve on it, the result was a system that locks and unlocks. Also if a person inputs an incorrect knock, the system will send a signal via bluetooth to a computer terminal to snap a picture and save it.<br> <br> <a href="http://www.youtube.com/watch?v=6k6XwyB8Hh0">http://www.youtube.com/watch?v=6k6XwyB8Hh0</a><br> <br> Check out the vid. Hope you like it.<br>
That really made my week! It looks fantastic and works great. And the bluetooth camera is a great idea of added security.
Hi Steve!<br /> As you know, I already started to do mine, with a few modifications. I uploaded a new video with a new option:<br /> <object height="344" width="425"> <param name="movie" value="http://www.youtube.com/v/d-HtuH90IMw&amp;hl=en_US&amp;fs=1&amp;hd=1" /> <param name="allowFullScreen" value="true" /> <param name="allowscriptaccess" value="always" /><embed allowfullscreen="true" allowscriptaccess="always" height="344" src="http://www.youtube.com/v/d-HtuH90IMw&amp;hl=en_US&amp;fs=1&amp;hd=1" type="application/x-shockwave-flash" width="425" /></object><br /> <br /> As soon as I finish my project I'll post the code online<br />
Did you finish yet?&nbsp; Can't wait to see the code.<br />
Fantastic work with the servo and the unlock option. &nbsp;(And an off&nbsp;switch!)<br /> <br /> It looks like your setup is really sensitive. &nbsp;Really nicely done! Your&nbsp;achievement&nbsp;patch is on the way!<br />
I have been playing around with this code for the past hour or so and found out how you can hard code in your own rhythm if need be.... at the part that looks like this<br /> // If we're recording a new knock, save the info and get out of here.<br /> &nbsp; if (digitalRead(programSwitch)==HIGH){<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i=0;i&lt;maximumKnocks;i++){ // normalize the times<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; secretCode[i]= map(knockReadings[i],0, maxKnockInterval, 0, 100); <br /> <br /> add this little guy<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(secretCode[i]);<br /> and open up your serial monitor....<br /> make sure you un-commented the serial begin up at the setup and then it should spit some numbers back at you... put these numbers into the array and enjoy.<br />
<object height="344" width="425"> <param name="movie" value="http://www.youtube.com/v/zBNTQkVUHvw&amp;hl=en_US&amp;fs=1&amp;" /> <param name="allowFullScreen" value="true" /> <param name="allowscriptaccess" value="always" /><embed allowfullscreen="true" allowscriptaccess="always" height="344" src="http://www.youtube.com/v/zBNTQkVUHvw&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" width="425" /></object>Whoot finally got my relays for my motor and heres a neat little Idea for your visual feedback....<br /> <br />
<object height="344" width="425"> <param name="movie" value="http://www.youtube.com/v/ZGCkVcrUNXM&amp;hl=en_US&amp;fs=1&amp;color1=0x3a3a3a&amp;color2=0x999999" /> <param name="allowFullScreen" value="true" /> <param name="allowscriptaccess" value="always" /><embed allowfullscreen="true" allowscriptaccess="always" height="344" src="http://www.youtube.com/v/ZGCkVcrUNXM&amp;hl=en_US&amp;fs=1&amp;color1=0x3a3a3a&amp;color2=0x999999" type="application/x-shockwave-flash" width="425" /></object>&nbsp;
Oh, and for your&nbsp;awesome&nbsp;work you have been added as an&nbsp;official&nbsp;Master of Secret Knocks. &nbsp;Your patch is on its way!
&nbsp;This is awesome! &nbsp;Thanks for sharing your code and video. &nbsp;Having the door unlatch too makes it that much more mysterious. &nbsp;And I like the feedback through the peephole.<br /> <br /> Great work!<br /> <br /> And if I ever have any questions about taping something to something else I'll be asking you. :)<br />
<p>Great project. I was wondering if anyone could recommend a small cheap microprocessor that could be used in place of the arduino? </p>
<p>my friend and i have made it, but we're using different code, we made the code by ourself, because your program was not working for us, we don't know why. and we add some features : LCD 16x2, servo, and buzzer</p><p>we inspired by yours, </p>
<p>very good </p>
<p>Hey, this is a really cool project and I have got it completed. Two items that are vexing me. 1. The motor runs all the time, but stops for 650 when the knock is applied correctly (polarity maybe?) I know it is a simple fix, but it is just escaping me. 2. The NPN transistor gets smoking hot. I have the correct resistor and even changed it out with a new one. Still getting hot (very). Thoughts. I love this and we are adding it into our business. Thanks for the help. I will send you a pic of the final project if you would like. It is eventually going to run a 12V relay to a maglock. </p>
<p>why I do like you, but it does not work.reply shivaduy248@gmail.com,please</p>
<p>can i use any type of buzzer or what else can be substitute for the function of the piezo speaker </p>
<p>can i use any type of buzzer or what else can be substitute for the function of the piezo speaker</p>
<p>can't open the code:(</p>
<p>can't open the code:(</p>
<p>can't open the code:(</p>
<p>can you progam it lock win you do the knock?????</p>
my green led is blinking all the time- then the ref led . Is the poezo to sensitiv?says &quot;wrong code&quot;( 2 blinks)
<p>Hye sir..can i know the full coding to make the motor turns clockwise and anticlockwise..(open and close lock). I'm using a reduction gear motor</p>
<p>sir can you show me the image how all the wire been soldering.....i dont know how to solder which wire...can please show full image the board that you solder...thank you sir...</p>
<p>This project is awesome.i made it but i want to store the new password in EEPROM.Can you please tell me how to edit the code so that it stores the new password in EEPROM.</p><p>Thank you,</p><p>Aamir</p>
<p>Hi,</p><p>I really liked the idea and stuff. I want to make it for my class project. I need a kit as early as possible as I have to submit it by 11/30/2015. Can anybody tell me how can I get the kit quickly? </p><p>Thanks for your cordial help in advance.</p>
<p>I noticed that if I power cycle the arduino, it looses the knock pattern.</p><p>Is there a way to have it save the pattern so it doesn't get lost in case of a powercycle?</p>
<p>Can i do it on atmega 8?</p>
<p>Can it be made without the arduino? I plan on making it as a college project and I can just use an PIC18F45K20</p>
bro do u hav the codingfor the reset button
<p>Can I use a side button to generate a <strong>auto lock</strong> function?</p>
i would use it in the school locker
<blockquote>how to open it <strong>inside ?</strong></blockquote>
<p>how to control inside?</p>

About This Instructable


1,673 favorites


Bio: Creative swashbuckler. Writer for MAKE Magazine, presenter of inventions on TV, radio, magazines and newspapers. Professional problem solver. Annoyingly curious. Hacker of all things from ... More »
More by Grathio: Book Light Reading Light Video: Making your glove work with a touch screen 60 Second Mobile
Add instructable to: