// Tuning constants. Could be made vars and hoooked to potentiometers for soft configuration, etc.const int threshold = 3; // Minimum signal from the piezo to register as a knockconst int rejectValue = 25; // If an individual knock is off by this percentage of a knock we don't unlock..const int averageRejectValue = 15; // If the average timing of the knocks is off by this percent we don't unlock.const int knockFadeTime = 150; // milliseconds we allow a knock to fade before we listen for another one. (Debounce timer.)const int lockTurnTime = 650; // milliseconds that we run the motor to get it to go a half turn.

const int maximumKnocks = 20; // Maximum number of knocks to listen for.const int knockComplete = 1200; // Longest time to wait for a knock before we assume that it's finished.

digitalWrite(greenLED, LOW); // we blink the LED for a bit as a visual indicator of the knock. if (programButtonPressed==true){ digitalWrite(redLED, LOW); // and the red one too if we're programming a new knock. } delay(knockFadeTime); // wait for this peak to fade before we listen to the next one. digitalWrite(greenLED, HIGH); if (programButtonPressed==true){ digitalWrite(redLED, HIGH); } do { //listen for the next knock or wait for it to timeout. knockSensorValue = analogRead(knockSensor); if (knockSensorValue >=threshold){ //got another knock... //record the delay time. Serial.println("knock."); now=millis(); knockReadings[currentKnockNumber] = now-startTime; currentKnockNumber ++; //increment the counter startTime=now; // and reset our timer for the next knock digitalWrite(greenLED, LOW); if (programButtonPressed==true){ digitalWrite(redLED, LOW); // and the red one too if we're programming a new knock. } delay(knockFadeTime); // again, a little delay to let the knock decay. digitalWrite(greenLED, HIGH); if (programButtonPressed==true){ digitalWrite(redLED, HIGH); } }

now=millis();

//did we timeout or run out of knocks? } while ((now-startTime < knockComplete) && (currentKnockNumber < maximumKnocks));

// If we're recording a new knock, save the info and get out of here. if (programButtonPressed==true){ for (i=0;i<maximumKnocks;i++){ // normalize the times secretCode[i]= map(knockReadings[i],0, maxKnockInterval, 0, 100); } // And flash the lights in the recorded pattern to let us know it's been programmed. digitalWrite(greenLED, LOW); digitalWrite(redLED, LOW); delay(1000); digitalWrite(greenLED, HIGH); digitalWrite(redLED, HIGH); delay(50); for (i = 0; i < maximumKnocks ; i++){ digitalWrite(greenLED, LOW); digitalWrite(redLED, LOW); // only turn it on if there's a delay if (secretCode[i] > 0){ delay( map(secretCode[i],0, 100, 0, maxKnockInterval)); // Expand the time back out to what it was. Roughly. digitalWrite(greenLED, HIGH); digitalWrite(redLED, HIGH); } delay(50); } return false; // We don't unlock the door when we are recording a new knock. }

if (currentKnockCount != secretKnockCount){ return false; }

/* Now we compare the relative intervals of our knocks, not the absolute time between them. (ie: if you do the same pattern slow or fast it should still open the door.) This makes it less picky, which while making it less secure can also make it less of a pain to use if you're tempo is a little slow or fast. */ int totaltimeDifferences=0; int timeDiff=0; for (i=0;i<maximumKnocks;i++){ // Normalize the times knockReadings[i]= map(knockReadings[i],0, maxKnockInterval, 0, 100); timeDiff = abs(knockReadings[i]-secretCode[i]); if (timeDiff > rejectValue){ // Individual value too far out of whack return false; } totaltimeDifferences += timeDiff; } // It can also fail if the whole thing is too inaccurate. if (totaltimeDifferences/secretKnockCount>averageRejectValue){ return false; }