Hardware Reference
In-Depth Information
void loop() {
sKnock.checkKnock();
}
The current configuration detects a “shave and a haircut, two bits” type of knock. It sounds like “dah, dit, dit, dah,
dit, pause, dit, dit”, and can be visualized like “_.._. ..” You can change this to any knock combination by defining the
pauses in the antiknocks. The pause ratio is used to determine if there are any matching knocks.
Most of the work is completed in the SecretKnock object. First, the pin configurations include the servo—the
green LED is pin 3, the red LED is pin 4, the piezo's knock sensor is analog pin 1, the program button is pin 0, and the
servo pin is #define SERVO_PIN 1 , which is digital pin 1.
Then the secret knock properties are defined, as in Listing 9-14.
Listing 9-14. Definging the Properties of the Secret Knock
threshold = 500; // Minimum signal from the piezo to register as a knock
rejectValue = 25; // If an individual knock is off by this percentage of a knock we
don't unlock.
averageRejectValue = 15; // If the average timing of the knocks is off by this percent we
don't unlock.
knockFadeTime = 200; // milliseconds we allow a knock to fade before we listen for
another one. (Debounce timer.)
lockTurnTime = 650; // milliseconds that we run the motor to get it to go a half turn.
lockMotor = 2;
knockComplete = 1200; // Longest time to wait for a knock before we assume that it's finished.
Once this is complete, the code is ready to perform checkKnock() in the main loop() function. Once the first
knock is detected, it will seek to match a knock pattern.
The enclosure can be any kind of box that you want; you use the servo as a lock that opens when the secret knock
triggers the move-servo code.
You can program the code into the ATtiny85 using the technique demonstrated in Listing 9-13, but be sure to
disconnect the servo.
The servo code, as shown in Listing 9-13, is simplified to manually pulse the servo to make it move. This
technique requires the chip to keep pulsing for the length of time it takes to move the servo. The result is that only one
servo at a time can be active. Even if you were to configure multiple servos, you could only move one at a time.
The key to reading the knock sensor is inside of the checkServo() function. This is analogRead(knockSensor) ,
which checks if the piezo is greater than the trigger threshold. If so, the code will start listening for a knock pattern.
A knock pattern is recognized by the ratio of pauses within a certain tolerance. The code that makes that
comparison appears in Listing 9-15.
Listing 9-15. The Code That Identifies the Secret Knock
// Sees if our knock matches the secret.
// returns true if it's a good knock, false if it's not.
// to do: break it into smaller functions for readability.
boolean SecretKnock::validateKnock()
{
int i=0;
 
Search WWH ::




Custom Search