In the previous project, we were creating an accelerometer game that recorded each time the CREATOR Kit is tapped. The result of the program was a list of times that the user tapped on the CREATOR Kit.
In this project, we'll make this a bit more useful — we'll create a secret knock. In this case the user will have to tap on the kit with a very specific rhythm. If the their rhythm is reasonably close to our "secret" rhythm, then they've corrected entered the secret knock.
A secret knock could be used to gain access to a room, to start another program, to send a message over the network, or in many other ways!
You'll need to wire up only the accelerometer and speaker as done in previous projects. Here is what your breadboard should look like:
Here is what the code for this project should look like at this point (with new changes highlighted with arrows):
The new additions we've made highlighted above are:
On Lines 48 through 58, we print out the rhythm (with stars ('*')). To do this, we first eliminate a two special cases: if there's no taps or if there's just one tap, we just let the user know. When there are two or more taps, for each tap, we print the relative amount of spaces from the previous tap, followed by a star.
On Lines 61 and 62, we define a secret knock that the user must reproduce.
On Lines 65 through 70, we test to see if the user's taps match our
secret_knock
. To match, the two lists must be the same length,
and each user tap time must be close to the secret knock time.
On Lines 72 through 75, we simply print out whether or not the user correctly entered the secret knock.
The secret knock in the code above is 2 quick knocks followed by 2 slightly slower knocks: KNOCK KNOCK KNOCK KNOCK.
If your timing is off, take a look at the numbers that print out in the Output Window (the first set is your knocks and the second set is the secret knock) and then try adjusting until you get the pattern. It might take a couple tries...
Can you make a more complicated secret knock?
Having trouble replicating the knock in the code? Try doing your own knock, copy of the numbers of your knock from the Output Window back into your code, and then you'll just have to replicate your own knock!