Step 2Program The Arduino
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.)
#2:
Open the sketch and compile it. It should compile properly the first go, but it's good to be sure.
#3:
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.
Debugging:
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.
| « Previous Step | Download PDFView All Steps | Next Step » |














































If you're still having trouble after following those instructions post again.
im 11 years old and i know how to program the arduino
you make the code really precise and pretty good
the last time i did a progect i got confused but this is
clear:-o
The motor I link in Step 1 is sold by a Canadian company so shipping costs, etc should be minimal.
Thank you for this great instructable.
PS. i had some problems at first. Happens to be that my knock sensor is really hard to trigger. Even with threshold = 1. It's a really small knocksensor encased in plastic.
Thanks so much for the great project. I'll be watching you for any other great ideas.
Fortunately that line doesn't actually -do- anything so it's not super critical.
It looks like I haven't updated the PDE here in several versions. You can get the latest here:
http://grathio.com/assets/secret_knock_detector.pde
I'll also try to update the coke embedded above, but the Instructables interface doesn't make it easy.
This normalizes the data so the longest time between knocks gets "100" and the rest are adjusted proportionately. We do this so it doesn't matter if you knock fast or slow as long as you get the rhythm correct.
The code I provided would require some kind of conversion. If you're programming the PIC in C the conversion should be pretty straight forward since the Arduino code is mostly straight C. If you're using Assembly or some other language you'll have to port the whole thing over. How hard that is depends on the language and how good you are with it. I've commented my code pretty well. You might find the Arduino reference manual useful for seeing what specific instructions do (like timing and reading/writing pins).