• No results found

Knock-Knock Door Lock

N/A
N/A
Protected

Academic year: 2021

Share "Knock-Knock Door Lock"

Copied!
58
0
0

Loading.... (view fulltext now)

Full text

(1)

IN

DEGREE PROJECT TECHNOLOGY, FIRST CYCLE, 15 CREDITS

,

STOCKHOLM SWEDEN 2020

Knock-Knock Door Lock

Unlocking your door with a secret knock

sequence

FILIP ANDERSSON

DAVID BERGLING

(2)
(3)

Knock-Knock Door Lock

Unlocking your door with a secret knock sequence

FILIP ANDERSSON DAVID BERGLING

Bachelor’s Thesis at ITM Supervisor: Nihad Subasic

Examiner: Nihad Subasic

(4)
(5)

Abstract

The door lock - a key function in every modern home, as well as a product which is today undergoing drastic change. The digital revolution has not left the door lock untouched, and there is today a wide variety of digital door locks utiliz-ing technologies rangutiliz-ing from touch displays to futiliz-ingerprint readers.

The premises of the project was to try a different approach to the digital door lock and implement it schoolyard-style using a secret knock sequence, set by the user. The secret sequence would consist of two elements, both a rhythmi-cal and a positional pattern. To unlock the door, the user would therefore need to simultaneously knock the correct rhythm and knock at the correct positions. To record and analyze the knock vibrations, piezo electric sensors con-nected to an Arduino Uno were used. To unlock the door, a small servo motor, two gears and a timing belt were used to turn the lock turning knob .

Despite a short time frame and hardships due to the limita-tions caused by the coronavirus outbreak, a fully functional prototype which fulfilled both reliability and consistency was achieved. It could with good consistency recognize the test sequence, which was the intro rhythm to ”We Will Rock You” knocked in a square pattern. But even with flawless functionality, the conclusion would have been the same. The security of the system may be exceptional from a technical perspective, but it falls flat when the human factor is taken into account. The ability for a potential burglar to simply watch as one executes the secret knock is too much of a security hazard for the system to ever be con-sidered as a competitor to other alternatives. The usage of the system is therefore better suited for applications where the thrill of using the product is more important than the actual security of it.

Keywords: door lock, knock sensor, rhythm recognition, piezo sensor mounting, time difference of arrival, Arduino Uno, servo motor.

(6)

Referat

Knack-Knack Öppna Tack

D¨orrl˚aset - en grundl¨aggande funktion i varje modernt hem, och samtidigt en produkt som idag genomg˚ar drastisk f¨or¨andring. Som resultat av den digitala revolutionen finns det idag en uppsj¨o av olika digitala d¨orrl˚as som utnyttjar allt fr˚an touchsk¨armar till fingeravtrycksl¨asare.

Syftet med projektet var att testa en annorlunda tappning p˚a det digitala d¨orrl˚aset, och f¨orverkliga n˚agot m˚anga ha-de dr¨omt om i barndomen - ett d¨orrl˚as aktiverat genom ett eget hemligt knackningsm¨onster. Det hemliga knack-ningsm¨onstret skulle d˚a best˚a av b˚ade ett rytmiskt och ett positionellt m¨onster. D¨armed skulle den som ville ¨oppna d¨orren beh¨ova knacka b˚ade r¨att rytm, och p˚a r¨att st¨allen p˚a d¨orren. F¨or att spela in och analysera vibrationerna fr˚an knackningarna anv¨andes piezoelektriska sensorer kopplade till en Arduino Uno. Mekanismen som styrde sj¨alva upp-l˚asningen bestod av en liten servomotor, tv˚a kugghjul och en kuggrem som kopplade samman det hela.

Trots en begr¨ansad tidsram och motg˚angar orsakade av co-ronaviruset lyckades en fullt fungerande prototyp med h¨og p˚alitlighet f¨ardigst¨allas. Den kunde med h¨og tillf¨orlit-lig-het k¨anns igen testsekvensen, som var introrytmen till ”We Will Rock You” knackat i m¨onstret av en kvadrat. Men ¨aven med felfri prestanda hade slutsatsen varit densamma. S¨akerheten f¨or systemet m˚avara exeptionell ur ett rent tek-niskt perspektiv, men den faller platt n¨ar den m¨anskliga faktorn tas med i ber¨akningen. Risken att en potentiell in-brottstjuv helt enkelt tittar p˚a n¨ar n˚agon knackar det hem-liga m¨onstret ¨ar f¨or stor f¨or att systemet n˚agonsin skulle kunna betraktas som en seri¨os konkurrent till andra digita-la d¨orrl˚as. Produkten ¨ar d¨armed b¨ast l¨ampad f¨or anv¨and-ningsomr˚aden d¨ar n¨ojet av att anv¨anda den ¨ar av h¨ogre prioritet ¨an sj¨alva s¨akerheten.

Nyckelord:d¨orrl˚as, knacksensor, rytmigenk¨anning, piezo sensor montering, time difference of arrival, Arduino Uno, servomotor.

(7)

Acknowledgements

Firstly, we would like to thank Seshagopalan Thorapalli Muralidharan for the con-stant support and guidance throughout the project, especially during the school shutdown when he was needed the most. He was a cornerstone not only to this project, but to the entire mechatronics course.

We would also like to thank Nihad Subasic for organizing the course, lecturing and giving constructive feedback throughout the project.

Lastly, we would like to thank ITM for providing the mechatronics projects with a budget and the freedom to choose projects ourselves.

(8)

Contents

1 Introduction 1 1.1 Background . . . 1 1.2 Purpose . . . 1 1.3 Scope . . . 2 1.4 Method . . . 2 2 Theory 3 2.1 Arduino Uno . . . 3

2.2 Signal interpretation and sampling . . . 3

2.2.1 Knock and sequence detection . . . 3

2.2.2 Filtering and processing . . . 4

2.3 Mechanism for locking and unlocking . . . 4

2.3.1 Lock turning torque . . . 6

2.4 Localization of a knock . . . 7

2.4.1 Method 1: TDOA with geometrical constraints . . . 7

2.4.2 Method 2: Simple TDOA . . . 9

3 Demonstrator 11 3.1 Hardware . . . 12

3.1.1 Turning knob gear . . . 12

3.1.2 Motor and transmission . . . 13

3.1.3 Base plate and servo rig . . . 13

3.1.4 Sensors and circuitry . . . 14

3.2 Software . . . 16

3.2.1 Knock rhythm recognition . . . 16

3.2.2 Knock location recognition . . . 17

4 Results 19

5 Discussion and conclusion 21

6 Future work 23

(9)

Appendices 26

(10)

List of Figures

2.1 Piezo electric sensor used to detect knocks for the project, edited in

Photoshop CC 2015 [1]. . . 4

2.2 Plot of piezo sensor readings with increasing knock strength, gathered from the Arduino Serial Plotter and edited in Photoshop CC 2015. . . . 5

2.3 Tool used for measuring lock turning torque, edited in Photoshop CC 2015. . . 6

2.4 Door sensor setup for method 1, made in Photoshop CC 2015. . . 8

2.5 Door sensor setup for method 2, made in Photoshop CC 2015. . . 9

3.1 Final prototype of the Knock-Knock Door Lock. . . 11

3.2 Turning knob gear mounted on the door turning knob, created in Solid Edge 2019 and rendered in KeyShot 7. . . 12

3.3 Transmission from servo motor to turning knob, created in Solid Edge 2019 and rendered in KeyShot 7. . . 13

3.4 Final design of the locking/unlocking mechanism, created in Solid Edge 2019 and rendered in KeyShot 7. . . 14

3.5 Sensor mounting rig made out of LEGO bricks, created in Mecabricks and edited in Photoshop CC 2015. . . 15

3.6 Circuit diagram for the electronics, created in Tinkercad and edited in Photoshop CC 2015. . . 15

(11)

List of Abbreviations

CAD - Computer Aided Design KTH - Royal Institute of Technology LED - Light Emitting Diode

DC - Direct Current

TDOA - Time Difference of Arrival 3D - Three Dimensional

(12)
(13)

Chapter 1

Introduction

1.1

Background

The door lock. An invention that dates back to the Egyptian era, and a fundamental part of today’s society. The Yale pin cylinder door lock, which was invented in 1848, is still the most common type of door lock in modern homes, and hasn’t seen much change in the past hundred years [2].

Today there is a growing market for alternative door lock solutions, with a wide range of digital alternatives which enable more convenient ways of locking/unlocking the door. Many systems require the user to replace the door lock altogether, while some can be mounted on an existing door lock to extend its functionality.

With new digital solutions there are also new safety hazards, like hackability and risk of digital malfunctions [3], [4]. These are the challenges the new alternatives need to overcome if they are to replace the traditional pin cylinder door locks altogether.

1.2

Purpose

The purpose of this project was to investigate how a vibration sensor activated by a door knock sequence could be implemented to control the locking and unlocking of a pin cylinder door lock. The goal was to construct a unit which is easily mounted on a traditional door lock, to enable the user to easily install and control the door using a chosen knock pattern. The main questions which will be investigated during the project are the following:

• How can security and reliability be achieved with the Knock-Knock Door Lock?

• How can the Knock-Knock Door Lock compete with other digital solutions available on the market?

(14)

CHAPTER 1. INTRODUCTION

1.3

Scope

The limitations set by KTH Royal Institute of Technology for the project was a time frame of five months and a budget of 1000 SEK. The budget mainly impacted what sensors could be acquired for measuring the input, which in turn affected the performance of the system. The time frame limited how much of the purpose could be fulfilled. Broad adaptability would have required extensive testing, which had to be excluded to fit the time frame.

The project was also limited by the shutdown of KTH’s school perimeters in response to the coronavirus outbreak. During the second half of the project’s time frame all work including mechanical construction had to be done from home. Production of 3D-printed parts was also impossible during a significant time period, which induced some creative at-home-solutions.

1.4

Method

The project started with a literature study to investigate the field. No specific studies discussing the usage of piezo sensors for knock recognition existed, and therefore a large portion of the information was gathered by conducting original research. The most valuable studies available were ones which discussed sensor usage along with microcontrollers.

The project can be divided into three parts: the construction of the mechanical system for turning the lock, the development of software to record and analyze the knocks, and the set up of sensors to communicate with a microcontroller which controls the turning mechanism.

The construction of the mechanical turning mechanism was based on research on lock turning torque, brainstorming and iterative prototype testing. The sensors were chosen with inspiration from similar projects, and the software was written from scratch.

(15)

Chapter 2

Theory

2.1

Arduino Uno

The microcontroller used for the project is an Arduino Uno, which is based on the ATmega328P chip. It has 14 digital input/output pins and 6 analog input pins which can be used to read and receive data from electrical components. To program an Arduino Uno, Arduino’s own integrated development environment is used. It’s based on the programming language C++ and includes a large number of built in functions and libraries [5].

The speed at which the Arduino Uno can sample data is determined by it’s sample rate. For the analog input pins it’s by default set to operate at 9600 Hz, which yields the most accurate results. If a higher sample rate is desired, it can be increased by changing the prescaler which by default is set to 128. An increase in sample rate will however decrese the sampling accuracy [6], [7].

2.2

Signal interpretation and sampling

The knock detection was achieved using piezoelectric sensors, see Figure 2.1. Piezo-electric sensors utilize a piezoPiezo-electric material which induces a voltage when de-formed. In this application the sensors were mounted on the inside of a door, recording the knock-induced vibrations transmitted through the door. Piezoelectric sensors can be used to record vibrations, but also inversely to induce vibrations if a voltage is applied [8]. This application can be heard when a smoke alarm is triggered, as many smoke alarms produce sound using piezo elements.

2.2.1 Knock and sequence detection

By connecting the piezo sensor to the analog input pin of the Arduino Uno, the sensor readings can be converted to digital values and individual knocks can thus be registered. To be able to recognize an entire sequence, the software stores the

(16)

CHAPTER 2. THEORY

Figure 2.1. Piezo electric sensor used to detect knocks for the project, edited in

Photoshop CC 2015 [1].

time elapsed between each knock in an array. This array can then be analyzed to determine if the correct sequence was executed.

2.2.2 Filtering and processing

Since a piezo sensor is a very primitive measuring tool, some custom digital analysis of the signal was needed. Depending on door type, the vibration from the knock is dampened differently. Background noise is also a factor which varies; different buildings have different levels of background noise, which can require signal filtering adjusted to prevailing conditions. It was however discovered that the sensors ac-quired for the project were not sensitive enough to register any significant ambient noise, hence only the difference in door dampening needed to be accounted for. From testing and signal analysis, the most effective filter for the circumstances in question was concluded to be an amplitude high-pass filter. In practice, it means that the signal amplitude registered by the piezo sensors has to be greater than the set value of the threshold, see Figure 2.2. Thus, most unintentional inputs will be ignored (such as footsteps, loud noises, vibrations etc.). However, occasional transients can still be registered but are not frequent enough to be regarded as a problem. Thus, the only adjustment needed were the amplitude thresholds for the sensors. The final system therefore enabled the end-user to easily calibrate these parameters.

2.3

Mechanism for locking and unlocking

To unlock the door, a servo motor was used. A servo motor is an electric motor with fixed rotational freedom and a built-in sensor which keeps track of the motor’s general position [9]. It is not a precision motor, but this is compensated by its price and size. Since the rotation needed to unlock a standard door lock is roughly 90° and precision is not vital, a servo motor was the perfect choice.

(17)

2.3. MECHANISM FOR LOCKING AND UNLOCKING

Figure 2.2. Plot of piezo sensor readings with increasing knock strength, gathered

from the Arduino Serial Plotter and edited in Photoshop CC 2015.

A servo motor is appropriate over a DC or step motor since it is small, cheap and easy to control. A DC motor would require an extra gearbox module for downshifting, and a step motor would be unnecessarily precise for the task of turning a lock.

The transmission from motor to lock turning knob was the part of the construc-tion process which underwent the most iteraconstruc-tions. A concentric mounting was first considered because it required no gearing. The drawback however was that the con-struction would build straight outwards. This was both less aesthetically pleasing and could also intrude on the space needed to grasp the door handle. Therefore the idea was quickly dismissed.

Bevel gears and regular gears would build sideways as opposed to straight out, and was therefore an appealing alternative. Both gear types were tested, but resulted in forces on the gear teeth that were too large for the plastic assembly to hold together. A timing belt connecting parallel gears together distributed the torque force over many gear teeth, which seemed to be a key factor for the assembly to hold together and work properly. This solution combined effective space usage with reliable torque transmission, and was therefore proceeded with.

(18)

CHAPTER 2. THEORY

2.3.1 Lock turning torque

To choose an appropriate motor for the task of turning a lock, data on lock turning torque needed to be acquired. A testing tool was constructed, and consisted of a lever which could be mounted on a lock turning knob, see Figure 2.3. The lever could then be turned by pulling it at a known distance from the center, L, with a newtonmeter in the direction perpendicular to the lever and registering the required force F. Thereby, the turning torque could be calculated using equation 2.1

M = F · L (2.1)

Seven different door locks were tested to determine the required torque from the servo. The tests yielded the results in Table 2.1.

Door lock 1 2 3 4 5 6 7

Turning torque [Nm] 0.27 0.27 0.36 0.18 0.13 0.25 0.20

Table 2.1. Required lock turning torque for different door locks

Figure 2.3. Tool used for measuring lock turning torque, edited in Photoshop CC

(19)

2.4. LOCALIZATION OF A KNOCK

2.4

Localization of a knock

A big flaw with the concept of a lock controlled with a simple knock sequence is that anyone simply could listen and repeat the knock sequence to unlock the door. To prevent this, the idea of having to knock at specific parts of the door to increase security was investigated.

There were multiple methods that could have been used to achieve this localization. What they all have in common is that the methods utilize multiple sensors placed at known locations. Depending on where on the door the knock is initiated, the sensors detect the knock at different times. The time difference then makes it possible to locate where the knock was initiated by geometrical calculations. This is known as Time Difference of Arrival (TDOA) methods [10], [11]. A problem which occurred while testing TDOA methods with the Arduino Uno was that the software had trouble measuring the time differences. The cause of the problem turned out to be the sampling frequency of the Arduino Uno, which was by default set at 9600 Hz, meaning one sample per 104 µs. The time for the knock vibrations to travel from one sensor to another was close to this time, which resulted in that the software could not reliably determine which sensor was actually triggered first.

This problem was solved by increasing the sample frequency of the Arduino Uno from 9600 to 76 800 Hz by changing the prescaler to 16. With the higher sampling frequency of one sample per 13 µs, the TDOA measurments were more accurate. 2.4.1 Method 1: TDOA with geometrical constraints

Equations describing the geometrical constraints of the sensors and knock locations were formulated. Together they formed a system of nonlinear equations, see Figure 2.4 and equation 2.2-2.5. To avoid rigorous testing and advanced calculations, it was assumed that the doors were isotropic, and transmitted vibrations at the same speed in all directions.

By having three sensors, the system had four unknown parameters; the distance

d between the knock location and the closest sensor, and the angles α, β and φ.

Following are the equations describing the geometric constraints of the case shown in Figure 2.4.

~ex : (b + d) · cos(α) + d · cos(β) = x2 (2.2)

~ey : (b + d) · sin(α) − d · sin(β) = 0 (2.3)

(20)

CHAPTER 2. THEORY

Figure 2.4. Door sensor setup for method 1, made in Photoshop CC 2015.

~ey : (b + d) · sin(α) + (a + d) · cos(ϕ) = y3 (2.5)

When the first knock gets detected (in this case by sensor 2) the distances a and b can be calculated

a= t3· v (2.6)

b= t1· v (2.7)

where t1 and t3 are the time taken for sensor 1 and 3 to register the knock after

sensor 2, and v is the vibration travel speed in the door.

In order to find the location of the knock, the coordinates (xk, yk) had to be calcu-lated. This could be done by solving for the roots of the system of equations formed above, but due to the system being nonlinear this is analytically very hard to do. Therefore numerical methods are better suited, where Newton-Raphson’s method is one of many that can be used to accomplish this [12]. Applying such a method to this system would have required matrix operations such as multiplication and in-version, which would normally be done easily in MATLAB. In this case however, it

(21)

2.4. LOCALIZATION OF A KNOCK

needed to be done in real-time on the microcontroller. This would require advanced mathematical C-programming and take much more time to implement than using a more simplified approach to TDOA, described in method 2.

2.4.2 Method 2: Simple TDOA

A simpler way of using TDOA is to have sensors placed in such a way that knock vibrations in certain regions always reach a specific sensor first, see Figure 2.5. A knock in sensor region 1 will always trigger sensor 1 first, and a knock in sensor region 3 will always trigger sensor 3 first etc.

Figure 2.5. Door sensor setup for method 2, made in Photoshop CC 2015.

Since this method only distinguishes differences between (in this case) four different regions, it is much less accurate than method 1 which gives the exact position. This is however compensated by the fact that it would be extremely hard for most users to remember and execute a knock sequence with rhythmic and geometric precision. This sensor setup also makes it possible for the end-user to position them in any way they want, which increases its security significantly. Therefore, the decision to continue with method 2 was made.

(22)
(23)

Chapter 3

Demonstrator

The final construction consisted of four piezoelectric sensors, a servo motor and a transmission which connected the motor to the door’s locking mechanism, see Figure 3.1. All components were connected to an Arduino Uno which controlled the whole system. It had reliable functionality for recording a knock sequence (in regard to both rhythm and general position) and determining if the recorded sequence was the same as the secret one or not.

The prototype also had functionality for the user to easily record a new secret knock sequence, or to calibrate the sensitivity of the sensors. With the system in place, the user also retained the possibility to use the door normally with a key.

(24)

CHAPTER 3. DEMONSTRATOR

3.1

Hardware

Due to the fact that the construction is an add-on and not a standalone product, its geometry was limited to what fit on already existing doors. Therefore, the starting-point of all construction was the measuring and analyzing of prevailing geometry. The workflow was characterized by iterative testing and rapid prototyping. Ideas were roughly sketched in CAD, manufactured (usually with 3D-printers or laser cutters) and tested, usually multiple times per work session. It was decided early on in the project to initially focus on designing and manufacturing all the parts that would be needed. This came to be a valuable decision later on when the coronavirus outbreak hit Sweden and KTH was forced to close down the school perimeters. The only parts that were not finished by that time were the mounting rigs for the sensors, as the importance of their function was not realized until later on in the project. When that time came, the rigs were made with LEGO bricks, which worked surprisingly well.

3.1.1 Turning knob gear

The gear on the lock turning knob is a prime example of an iterative design process. Over a dozen iterations were made, testing different gear and knob geometries to get an adequate final result, see Figure 3.2. The inside of the gear was made to have interference fitting with the knob, and the gears on the outside were made to fit a standard belt geometry. The material was chosen to be 8 mm acrylic as it was easy to laser cut and the gear load was not sufficient to motivate a tougher material such as metal.

Figure 3.2. Turning knob gear mounted on the door turning knob, created in Solid

(25)

3.1. HARDWARE

3.1.2 Motor and transmission

The gear is supplied with torque by a servo motor. The torque is transmitted from the servo to the knob turning gear via a timing belt and a gear mounted on the motor’s output shaft. The gearing ratio was chosen to be 1.4 as the servo had 40% excess roatational freedom when mounted to the door turning knob, and as such a gearing ratio put less load on the servo, see Figure 3.3. The servo was first powered directly from the Arduino Uno from the 5V output pin, which resulted in the servo occasionally moving by itself. This was resolved by powering the servo with an external power supply consisting of four AAA batteries wired in series (6V). This resulted in a more stable behaviour.

Figure 3.3. Transmission from servo motor to turning knob, created in Solid Edge

2019 and rendered in KeyShot 7.

3.1.3 Base plate and servo rig

When the motor and lock turning mechanism was finished, it needed to be attached to the door lock. Since it would be very intricate to attach gears, a servo and an Arduino Uno directly to the door, it seemed appropriate to construct a tailor-made base plate for all the components to attach to. Thereby only the base plate (constructed from 3mm acrylic) had to be attached to the door. This was easily done with some double-sided tape.

The servo rig which positions the servo and its corresponding gear, attaches to the base plate with two bolts through two slits. This allows the servo rig to be shifted sideways which allows the timing belt to be tensioned.

(26)

CHAPTER 3. DEMONSTRATOR

knob turning gear. Measuring, sketching and testing 3D-printed draft models led to a final design which functioned as desired, see Figure 3.4.

Figure 3.4. Final design of the locking/unlocking mechanism, created in Solid Edge

2019 and rendered in KeyShot 7.

3.1.4 Sensors and circuitry

For the sensors to register good readings, a sensor mounting rig was needed. This was concluded after testing many sensor mounting alternatives, including taping and gluing. The key to achieve good sensitivity was to press the sensor against a surface using a rigid object which does not absorb the vibrations of the sensor. Due to the quarantine, this construction was made from home using LEGO bricks and a screw with plastic on the tip, see Figure 3.5. The sensor mounting rig was mounted on the door using double sided tape and worked surprisingly well, allowing for good sensor readings.

(27)

3.1. HARDWARE

Figure 3.5. Sensor mounting rig made out of LEGO bricks, created in Mecabricks

and edited in Photoshop CC 2015.

Since piezo sensors can generate voltages of up to 90V, they were connected to the microcontroller in parallel with 1 MegaOhm resistors, to protect the board from damaging currents. To initiate the recording to set a new secret knock sequence and to calibrate the piezo sensor thresholds, a pushbutton was used. Two LEDs were also used to help the user navigate the program.

The motor control pin and pushbutton pins were connected directly into the Arduino Uno, while the LEDs were connected in series with 20 kOhm resistors to regulate the current running through them. A soldering board was used to connect all wires and resistors together. The complete circuit diagram for the electronics can be seen in Figure 3.6.

Figure 3.6. Circuit diagram for the electronics, created in Tinkercad and edited in

(28)

CHAPTER 3. DEMONSTRATOR

3.2

Software

The program running on the Arduino Uno is based upon a structure of different states, where each state completes a specific task. The program includes the fol-lowing states:

• Listening for a knock (idle) • Knock sequence recording • Knock sequence analyzing • Locking/unlocking of the door

• Recording of new secret knock sequence • Calibration of piezo sensor thresholds

After a state has completed its task, the program jumps into another state, which one depending on the outcome of the completed task. If for example the state ”Knock sequence analyzing” determines that the knock sequence was correct, the program will enter the state ”Locking/unlocking of the door”.

To allow the user to manually lock/unlock the door with the lock turning knob or with its corresponding key, the servo motor is only receiving voltage from the Arduino Uno during the locking/unlocking sequence. This allows the motor to rotate freely without any resistance when it’s not in use, thus allowing the door lock to be used as originally intended.

3.2.1 Knock rhythm recognition

A key feature for the knock rhythm recognition to work properly was for it to be able to recognize knock patterns regardless of tempo. Therefore, the code analyzes the knock pattern by dividing all time periods in between the knocks with the longest time period. Thereby, all time periods become expressed as fractions of the longest one in the sequence. To exemplify, a 4-knock sequence with the times in between the knocks [0.3s, 0.3s, 0.6s], would in the analyzing state be divided by 0.6s, the longest time period, and become [0.5, 0.5, 1].

The classification of a knock rhythm as correct or incorrect is simply done by mea-suring the difference between the registered knock sequence and the secret knock sequence. If the largest difference between the two vectors is within a certain tol-erance, the knock is classified as correct. Testing revealed that a tolerance of 0.2 yielded both ease of knocking the correct sequence and high security.

(29)

3.2. SOFTWARE

To continue on the previous example, if the secret knock sequence was set to (for example) [0.4, 0.65, 1], and the registered sequence was [0.5, 0.5, 1], the differences would have been [0.1, 0.15, 0]. Since all differences are below 0.2, the knock sequence would have been classified as correct, and the state of the program would change to ”Locking/unlocking of the door”.

3.2.2 Knock location recognition

The classification of a knock position sequence as correct or incorrect is far simpler than for the rhythm. The program simply measures the difference between the recorded position sequence and the secret position sequence. If there is a difference, the sequence is classified as incorrect.

(30)
(31)

Chapter 4

Results

The final prototype achieved its preordained purpose to a high degree. The un-locking/locking of the door lock worked consistently thanks to a simple and well functioning mechanical construction. The knock rhythm detection also worked well, much thanks to software design decisions based on testing. The knock position de-tection performed inadequately during a large part of the project, but was brought up to an adequate level of performance after the sensor calibration was put in place. A general observation regarding knock detection was that thicker, more sound-proof doors yielded less reliable sensor readings than thinner doors.

In the final phase of the project, the finished prototype was mounted on a sound-proof test door, and testing resulted in a secret knock sequence being correctly identified in 34/35 cases. To answer the question ”How can security and reliability

be achieved with the Knock-Knock Door Lock?”, the following factors had significant

contribution to the performance of the prototype:

• Sensor mounting - mounting the sensors by pressing them against the door instead of using some sort of adhesive yielded superior sensitivity

• High sampling frequency - crucial for the positional recognition to work if the sensors were to be placed close together

• Good mechanical construction and software - for smooth and consistent lock turning that still allows the door lock to function with its corresponding key • Knocking technique - too soft of a knock was hard to measure on any door

(32)
(33)

Chapter 5

Discussion and conclusion

A part of the project which remained largely unexplored at the time of the deadline was how the performance of the positional recognition software could have been improved. While the decision to continue with the simple TDOA method allowed for the project deadlines to be met, it also limited what knowledge could be gathered from testing the prototype. As the simple TDOA method only measured which sensor was activated first, a significant amount of information was left out. When for example the sensor furthest away from the knock position was registered as the first one to detect the knock’s vibration, there was little data to troubleshoot with. For more informative test results, the exact time of each and every sensor reading would have had to be recorded and analyzed, and preferably with a matrix of sensors across the door. In comparison, the rhythm detection software analyzed the door vibrations continuously over time, which enabled detailed monitoring and enough data to tailor the software to the task.

The security of the system is phenomenal from a technical perspective. Since the sequence for unlocking the door requires both the correct rhythmic and positional pattern, the number of possible combinations is extremely high. Rough estimates show that just a fiveknock pattern yields over 400 million different combinations -more than 390 000 times -more than a five-digit number combination. The lock can therefore be regarded as impossible to crack with brute force guessing. Unfortu-nately, despite the exceptional technical performance, the security of a lock is also reliant on who has the key - and in this case anyone can make a copy of it. The risk for a possible intruder to get hold of the secret knock simply by spying on the lock owner, either in person or by placing some sort of hidden camera, is too great to overlook.

Regarding the question ”Can the Knock-Knock Door Lock compete with other digital

solutions available on the market?”, the assessment is that the system is not suitable

(34)

CHAPTER 5. DISCUSSION AND CONCLUSION

Although, there could be niche markets for the product. Suitable applications would be where security is not of top priority - but rather the convenience or thrill of unlocking a door, for example on a kids playhouse. Sparsely populated areas where the risk of being watched is lower could also be a market, for example having a secret family-knock for the shared holiday cabin.

(35)

Chapter 6

Future work

One functionality which never saw the light of day was the Knock-Knock Door Locks ability to detect the lock turning knob’s position. In other words, if the door was locked using the secret knock and then later on got unlocked with the door’s key, the software would still believe that the door was in the locked position, and try to unlock the already unlocked door if triggered. This problem would have been circumvented with some type of sensor to determine the state of the gears connected to the lock turning knob. This could have been achieved with a potentiometer rotating together with the gears, sending the corresponding voltage to the microcontroller.

Another function which would increase the systems readiness for the consumer market is calibration with respect to locking knob rotation. The idea was that the user would simply have to mount the system over the lock turning knob and manually turn the knob from its locked to its unlocked position - the system would then remember those angles and be calibrated.

Lastly, the possibility to utilize a more sophisticated form of TDOA method for positional detection should be investigated. The possibility to improve the per-formance could either rely on better software, such as a fully developed method 1 TDOA, or/and on better hardware, such as better sensors.

(36)
(37)

Bibliography

[1] digikey.se, “Piezoimg,” 2020-05-10. [Online]. Available: https://bit.ly/ 2LkDLCS

[2] J. Graham-Cumming, The Geek Atlas: 128 Places Where Science and

Tech-nology Come Alive. O’Reilly, 2009, ISBN: 0596523203.

[3] B. Ali and A. I. Awad, “Cyber and physical security vulnerability assessment for iot-based smart homes,” Sensors, vol. 18, no. 3, p. 817, 2018. [Online]. Available: https://doi.org/10.3390/s18030817

[4] E. Fernandes, J. Jung, and A. Prakash, “Security analysis of emerging smart home applications,” in 2016 IEEE Symposium on Security and Privacy (SP), 2016, pp. 636–654. [Online]. Available: https://doi.org/10.1109/SP.2016.44 [5] arduino.com, “Arduino uno,” 2020-05-10. [Online]. Available: https:

//store.arduino.cc/arduino-uno-rev3

[6] Atmel Corporation, “Atmega328p datasheet,” 2020-05-10. [On-line]. Available: http://ww1.microchip.com/downloads/en/DeviceDoc/ Atmel-7810-Automotive-Microcontrollers-ATmega328P Datasheet.pdf

[7] N. Gammon, “Adc conversion of the arduino (analogread),” 2020-05-10. [Online]. Available: https://www.gammon.com.au/adc

[8] G. Gautschi, “Piezoelectric sensors,” in Piezoelectric Sensorics. Springer, 2002, pp. 73–91. [Online]. Available: https://doi.org/10.1007/978-3-662-04732-3 5 [9] Dejan, “How servo motors work,” 2020-05-10.

[On-line]. Available: https://howtomechatronics.com/how-it-works/ how-servo-motors-work-how-to-control-servos-using-arduino/

[10] C. F. Scola and M. D. B. Ortega, “Direction of arrival estimation: A two microphones approach,” 2010. [Online]. Available: http://urn.kb.se/resolve? urn=urn:nbn:se:bth-3131

(38)

BIBLIOGRAPHY

[11] A. Sundkvist and J. Hellberg, “Best pal: Ball exercise sound tracking pal,” 2018. [Online]. Available: http://urn.kb.se/resolve?urn=urn:nbn:se:kth: diva-230250

(39)

Appendix A

C code on the Arduino

1

2 /* Knock - K n o c k D o o r L o c k

3 * A r d u i n o p r o g r a m for s e n s o r reading , r h y t h m r e c o g n i t i o n and s e r v o

4 * m o t o r c o n t r o l 5 * 6 * B a c h e l o r ’ s t h e s i s p r o j e c t in M e c h a t r o n i c s at KTH , 7 * R o y a l I n s t i t u t e of T e c h n o l o g y 8 * D a t e : 2020 -05 -26 9 * W r i t t e n by : D a v i d B e r g l i n g & F i l i p A n d e r s s o n 10 * C o u r s e c o d e : M F 1 3 3 X 11 * E x a m i n e r : N i h a d S u b a s i c 12 * TRITA - nr : ITM - EX 2 0 2 0 : 3 2 13 *

14 * The c o d e was w r i t t e n for and u s e d w i t h the f o l l o w i n g h a r d w a r e :

15 * 4 x P i e z o e l e c t r i c s e n s o r e l e m e n t s d35 mm 16 * A r d u i n o Uno rev 3 17 * F E E T E C H S t a n d a r d S e r v o F S 5 1 0 6 B 18 * S t a n d a r d p u s h b u t t o n 19 * S t a n d a r d L E D s 20 * R e s i s t o r s 21 * C u s t o m b u i l t m o u n t i n g r i g s 22 *

23 * The f o l l o w i n g p r o g r a m d e t e c t s k n o c k s on a d o o r and a n a l y s e s the

24 * r h y t h m i c a l and p o s i t i o n a l

25 * s e q u e n c e , and can ( if the c o r r e c t s e q u e n c e is e x e c u t e d ) u n l o c k the

26 * d o o r f r o m the i n s i d e .

27 * The p r o g r a m u s e s a p u s h b u t t o n i n t e r f a c e to let the u s e r set a new

28 * s e c r e t k n o c k s e q u e n c e 29 * and / or c a l i b r a t e the s e n s o r t h r e s h o l d s . 30 * The p r o g r a m is b u i l t u p o n a case - s t r u c t u r e , w i t h e a c h c a s e 31 * e x e c u t i n g a s p e c i f i c t a s k . 32 */ 33 34 # i n c l u d e " m a t h . h " 35 # i n c l u d e < S e r v o . h > 36 37 # i f n d e f cbi

(40)

APPENDIX A. C CODE ON THE ARDUINO

38 # d e f i n e cbi ( sfr , bit ) ( _ S F R _ B Y T E ( sfr ) &= ˜ _BV ( bit ) )

39 # e n d i f

40 # i f n d e f sbi

41 # d e f i n e sbi ( sfr , bit ) ( _ S F R _ B Y T E ( sfr ) |= _BV ( bit ) )

42 # e n d i f 43 44 // c o n s t a n t p a r a m e t e r s 45 int aa = 0; 46 int bb = 0; 47

48 c o n s t int p i e z o 1 = A1 ; // i n p u t pin for p i e z o s e n s o r 1

49 c o n s t int p i e z o 2 = A2 ; // i n p u t pin for p i e z o s e n s o r 2

50 c o n s t int p i e z o 3 = A3 ; // i n p u t pin for p i e z o s e n s o r 3 e

51 c o n s t int p i e z o 4 = A4 ; // i n p u t pin for p i e z o s e n s o r 4

52 53 c o n s t int L E D b l u e = 13; 54 c o n s t int L E D y e l l o w = 12; 55 c o n s t int b u t t o n = 11; 56 57 c o n s t int m a x k n o c k = 20; // m a x i m u m a m o u n t of k n o c k s a l l o w e d in the s e q u e n c e

58 c o n s t f l o a t tol = 0 . 2 ; // the t o l e r a n c e for how p r e c i s e the k n o c k s e q u e n c e has to be (0 -1) 59 60 c o n s t int l o c k e d = 40; // d e g r e s n e e d e d to w r i t e to s e r v o in o r d e r to l o c k 61 c o n s t int deg = 1 1 0 ; 62 c o n s t int u n l o c k e d = l o c k e d + deg ; // d e g r e s n e e d e d to w r i t e to s e r v o in o r d e r to u n l o c k 63 64 // t h e s e v a r i a b l e s w i l l c h a n g e : 65 f l o a t t h r e s h o l d 1 = 4 . 3 7 ; // t h r e s h o l d for w h e n a s i g n a l is r e g i s t e r e d as a k n o c k by s e n s o r 1 66 f l o a t t h r e s h o l d 2 = 5 . 8 9 ; // t h r e s h o l d for w h e n a s i g n a l is r e g i s t e r e d as a k n o c k by s e n s o r 2 67 f l o a t t h r e s h o l d 3 = 7 . 5 2 ; // t h r e s h o l d for w h e n a s i g n a l is r e g i s t e r e d as a k n o c k by s e n s o r 3 68 f l o a t t h r e s h o l d 4 = 4 . 1 5 ; // t h r e s h o l d for w h e n a s i g n a l is r e g i s t e r e d as a k n o c k by s e n s o r 4 69 70 f l o a t s e c k n o s e q [ m a x k n o c k -1] = { 0 } ; // s e c r e t k n o c k s e q u e n c e 71 int s e c p o s s e q [ m a x k n o c k ] = { 0 } ; // s e c r e t p o s i t i o n s e q u e n c e 72 73 int p o s v e k [ m a x k n o c k ] = { 0 } ; 74 f l o a t t v e k [ m a x k n o c k -1] = { 0 } ; 75 f l o a t t f r a c [ m a x k n o c k -1] = { 0 } ; 76 77 l o n g t e x i t ; // t i m e w h i t h o u t a k n o c k b e i n g d e t e c t e d 78 l o n g t m a x ; // m a x i m u m t i m e b e f o r e the r e c o r d i n g s t o p s 79 l o n g t ; // a l s o t i m e s t a m p for c a l c u l a t i n g t i m e b e t w e e n k n o c k s 80 81 int i ; // k n o c k c o u n t e r 82 int ii ; // g e n e r a l c o u n t e r 83

(41)

84 int b u t t o n c t r ; // c o u n t e r for d e t e r m i n i n g for how l o n g the b u t t o n has b e e n p r e s s e d d o w n 85 int s t a t e ; // d e t e r m i n e s w h i c h c a s e to e n t e r 86 87 int p o s d i f f ; 88 f l o a t t d i f f [ m a x k n o c k ] = { 0 } ; 89 f l o a t m a x t d i f f ; 90 91 S e r v o s e r v o ; // s e r v o m o t o r n a m e 92 int pos ; // p o s i t i o n of s e r v o 93 int d o o r s t a t e ; // k e e p s t r a c k if the d o o r is c l o s e d or o p e n e d . 0 = locked , 1 = u n l o c k e d 94 95 d o u b l e c a l 1 ; // v a r i a b l e s for c a l i b r a t i n g the p i e z o s e n s o r s 96 d o u b l e c a l 2 ; 97 d o u b l e c a l 3 ; 98 d o u b l e c a l 4 ; 99 100 v o i d s e t u p () 101 { 102 S e r i a l . b e g i n ( 9 6 0 0 ) ; // i n i t i a t e s c o m m u n i c a t i o n w i t h USB to the c o m p u t e r 103 104 p i n M o d e ( LEDblue , O U T P U T ) ; 105 d i g i t a l W r i t e ( LEDblue , LOW ) ; 106 107 p i n M o d e ( L E D y e l l o w , O U T P U T ) ; 108 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 109 110 p i n M o d e ( piezo1 , I N P U T ) ; // d e c l a r e the p i e z o s e n s o r 1 as an i n p u t 111 p i n M o d e ( piezo2 , I N P U T ) ; // d e c l a r e the p i e z o s e n s o r 2 as an i n p u t 112 p i n M o d e ( piezo3 , I N P U T ) ; // d e c l a r e the p i e z o s e n s o r 3 as an i n p u t 113 p i n M o d e ( piezo4 , I N P U T ) ; // d e c l a r e the p i e z o s e n s o r 4 as an i n p u t 114 115 p i n M o d e ( button , I N P U T _ P U L L U P ) ; 116 117 s e r v o . a t t a c h ( 1 0 ) ; // a s s i g n s s e r v o to pin 10 118 s e r v o . w r i t e ( u n l o c k e d ) ; // p u t s s e r v o in u n l o c k e d p o s i t i o n 119 120 d e l a y ( 5 0 0 ) ; 121 s e r v o . d e t a c h () ; // d e t a c h e s s e r v o f r o m pin 10 122

123 sbi ( ADCSRA , A D P S 2 ) ; // s e t s p r e s c a l e r to 16 to i n c r e a s e ADC

f r e q u e n c y 124 cbi ( ADCSRA , A D P S 1 ) ; 125 cbi ( ADCSRA , A D P S 0 ) ; 126 127 p o s d i f f = 0; 128 i = 0; 129 b u t t o n c t r = 0;

(42)

APPENDIX A. C CODE ON THE ARDUINO 130 131 s t a t e = 5; // t e l l s the c o d e to s t a r t at c a s e 5 132 d o o r s t a t e = 1; // d o o r s t a r t s as u n l o c k e d 133 134 d e l a y ( 2 0 0 0 ) ; 135 } 136 137 f l o a t m a x i m u m (f l o a t vek []) // f u n c t i o n t h a t f i n d s the m a x i m u m v a l u e in an a r r a y 138 { 139 f l o a t m a x v a l = 0; 140 141 for( ii = 0; ii <= m a x k n o c k -1; ii ++) 142 { 143 if( vek [ ii ] > m a x v a l ) 144 { 145 m a x v a l = vek [ ii ]; 146 } 147 } 148 149 r e t u r n m a x v a l ; 150 } 151 152 v o i d l o o p () { 153 154 s w i t c h( s t a t e ) 155 { 156 157 // -158 159 c a s e 1: // l i s t e n for a k n o c k 160 161 if( a n a l o g R e a d ( p i e z o 1 ) >= t h r e s h o l d 1 ) 162 { 163 p o s v e k [ i ] = 1; 164 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 165 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 166 i ++; 167 t = m i l l i s () ; 168 s t a t e = 2; 169 d e l a y ( 7 5 ) ; 170 d i g i t a l W r i t e ( LEDblue , LOW ) ; 171 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 172 d e l a y ( 7 5 ) ; 173 } // p i e z o 1 174 175 e l s e if( a n a l o g R e a d ( p i e z o 2 ) >= t h r e s h o l d 2 ) 176 { 177 p o s v e k [ i ] = 2; 178 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 179 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 180 i ++;

(43)

181 t = m i l l i s () ; 182 s t a t e = 2; 183 d e l a y ( 7 5 ) ; 184 d i g i t a l W r i t e ( LEDblue , LOW ) ; 185 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 186 d e l a y ( 7 5 ) ; 187 } // p i e z o 2 188 189 e l s e if( a n a l o g R e a d ( p i e z o 3 ) >= t h r e s h o l d 3 ) 190 { 191 p o s v e k [ i ] = 3; 192 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 193 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 194 i ++; 195 t = m i l l i s () ; 196 s t a t e = 2; 197 d e l a y ( 7 5 ) ; 198 d i g i t a l W r i t e ( LEDblue , LOW ) ; 199 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 200 d e l a y ( 7 5 ) ; 201 } // p i e z o 3 202 203 e l s e if( a n a l o g R e a d ( p i e z o 4 ) >= t h r e s h o l d 4 ) 204 { 205 p o s v e k [ i ] = 4; 206 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 207 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 208 i ++; 209 t = m i l l i s () ; 210 s t a t e = 2; 211 d e l a y ( 7 5 ) ; 212 d i g i t a l W r i t e ( LEDblue , LOW ) ; 213 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 214 d e l a y ( 7 5 ) ; 215 } // p i e z o 4 216 217 e l s e if( d o o r s t a t e == 1) // if the d o o r is u n l o c k e d 218 { 219 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 220 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 221 } // e l s e if 222 223 e l s e // if the d o o r is l o c k e d 224 { 225 d i g i t a l W r i t e ( LEDblue , LOW ) ; 226 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 227 } // e l s e 228 229 w h i l e( d i g i t a l R e a d ( b u t t o n ) == LOW ) 230 { 231 b u t t o n c t r ++; 232 d e l a y ( 1 0 0 ) ; 233 234 if( b u t t o n c t r >= 20 && b u t t o n c t r < 50)

(44)

APPENDIX A. C CODE ON THE ARDUINO 235 { 236 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 237 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 238 239 if( d i g i t a l R e a d ( b u t t o n ) == H I G H ) 240 { 241 s t a t e = 5; 242 b u t t o n c t r = 0; 243 d e l a y ( 4 0 0 0 ) ; 244 245 for( ii = 0; ii < m a x k n o c k ; ii ++) // z e r o e s s e c p o s s e q and s e c k n o s e q 246 { 247 s e c p o s s e q [ ii ] = 0; 248 249 if( ii >= 1) 250 { 251 s e c k n o s e q [ ii -1] = 0; 252 } 253 254 } // for 255 256 } // if b u t t o n 257 258 } // if 20 <= b u t t o n c t r < 50 259 260 e l s e if( b u t t o n c t r >= 50) // if the b u t t o n is p r e s s e d d o w n for 5 s e c o n d s or m o r e 261 { 262 d i g i t a l W r i t e ( LEDblue , LOW ) ; 263 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 264 265 if( d i g i t a l R e a d ( b u t t o n ) == H I G H ) 266 { 267 s t a t e = 7; 268 b u t t o n c t r = 0; 269 } 270 } 271 272 e l s e if( d i g i t a l R e a d ( b u t t o n ) == H I G H ) 273 { 274 s t a t e = 4; 275 b u t t o n c t r = 0; 276 277 aa = 0; 278 bb = 0; 279 } // e l s e if 280 281 } // w h i l e 282 283 b r e a k; 284 285 //

(45)

-286 287 c a s e 2: // r e c o r d the r h y t h m f r o m the k n o c k s e q u e n c e 288 289 t e x i t = m i l l i s () - t ; 290 291 if( a n a l o g R e a d ( p i e z o 1 ) >= t h r e s h o l d 1 ) // r e c o r d s the t i m e

b e t w e e n k n o c k i and k n o c k i +1 and s t o r e s it in the a r r a y t v e k

292 { 293 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 294 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 295 t v e k [ i -1] = m i l l i s () - t ; 296 t = m i l l i s () ; 297 p o s v e k [ i ] = 1; 298 i ++; 299 d e l a y ( 7 5 ) ; 300 d i g i t a l W r i t e ( LEDblue , LOW ) ; 301 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 302 d e l a y ( 7 5 ) ; 303 } // if p i e z o 1 304 305 e l s e if( a n a l o g R e a d ( p i e z o 2 ) >= t h r e s h o l d 2 ) // r e c o r d s the t i m e b e t w e e n k n o c k i and k n o c k i +1 and s t o r e s it in the a r r a y t v e k

306 { 307 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 308 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 309 t v e k [ i -1] = m i l l i s () - t ; 310 t = m i l l i s () ; 311 p o s v e k [ i ] = 2; 312 i ++; 313 d e l a y ( 7 5 ) ; 314 d i g i t a l W r i t e ( LEDblue , LOW ) ; 315 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 316 d e l a y ( 7 5 ) ; 317 } // e l s e if p i e z o 2 318 319 e l s e if( a n a l o g R e a d ( p i e z o 3 ) >= t h r e s h o l d 3 ) // r e c o r d s the t i m e b e t w e e n k n o c k i and k n o c k i +1 and s t o r e s it in the a r r a y t v e k

320 { 321 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 322 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 323 t v e k [ i -1] = m i l l i s () - t ; 324 t = m i l l i s () ; 325 p o s v e k [ i ] = 3; 326 i ++; 327 d e l a y ( 7 5 ) ; 328 d i g i t a l W r i t e ( LEDblue , LOW ) ; 329 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 330 d e l a y ( 7 5 ) ; 331 } // e l s e if p i e z o 3 332 333 e l s e if( a n a l o g R e a d ( p i e z o 4 ) >= t h r e s h o l d 4 ) // r e c o r d s the t i m e b e t w e e n k n o c k i and k n o c k i +1 and s t o r e s it in the a r r a y t v e k

(46)

APPENDIX A. C CODE ON THE ARDUINO 335 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 336 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 337 t v e k [ i -1] = m i l l i s () - t ; 338 t = m i l l i s () ; 339 p o s v e k [ i ] = 4; 340 i ++; 341 d e l a y ( 7 5 ) ; 342 d i g i t a l W r i t e ( LEDblue , LOW ) ; 343 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 344 d e l a y ( 7 5 ) ; 345 } // e l s e if p i e z o 4 346 347 if( t e x i t >= 2 0 0 0 || i == m a x k n o c k ) // if 2 s e c o n d s h a v e p a s s e d w i t h o u t a k n o c k or the s e q u e n c e is too l o n g 348 { 349 350 if( i >= 2) // if 2 or m o r e k n o c k s are r e g i s t e r e d 351 { 352 s t a t e = 3; 353 } // if 354 355 e l s e // o n l y 1 k n o c k is r e g i s t e r e d 356 { 357 358 for( ii = 1; ii <= 10; ii ++) // L E D y e l l o w b l i n k s 359 { 360 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 361 d e l a y ( 6 0 ) ; 362 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 363 d e l a y ( 6 0 ) ; 364 } 365 366 for( ii = 0; ii < m a x k n o c k -1; ii ++) // z e r o e s the a r r a y s 367 { 368 t v e k [ ii ] = 0; 369 } 370 371 for( ii = 0; ii < m a x k n o c k ; ii ++) // z e r o e s p o s v e k 372 { 373 p o s v e k [ ii ] = 0; 374 } 375 376 s t a t e = 1; 377 i = 0; 378 d e l a y ( 1 0 0 0 ) ; 379 380 } // e l s e 381 382 } // if 2 sec or too l o n g 383 384 b r e a k; 385 386 //

(47)

-387 388 c a s e 3: // a n a l y z e the r h y t h m 389 390 aa ++; 391 t m a x = m a x i m u m ( t v e k ) ; // f i n d s the max v a l u e in t v e k 392 393 for( ii = 0; ii < i ; ii ++) // d i v i d e s t v e k w i t h t m a x to get t f r a c 394 { 395 t f r a c [ ii ] = t v e k [ ii ]/ t m a x ; 396 } 397 398 for( ii = 0; ii <= m a x k n o c k -2; ii ++) // c a l c u l a t e s the m a x i m u m t i m e a k n o c k d i f f e r s f r o m the c o r r e c t s e q u e n c e 399 { 400 t d i f f [ ii ] = abs ( t f r a c [ ii ] - s e c k n o s e q [ ii ]) ; 401 } // for 402 403 m a x t d i f f = m a x i m u m ( t d i f f ) ; 404 405 for( ii = 0; ii <= m a x k n o c k -1; ii ++) 406 { 407 p o s d i f f = abs ( p o s v e k [ ii ] - s e c p o s s e q [ ii ]) + p o s d i f f ; 408 } 409

410 if( m a x t d i f f < tol && p o s d i f f == 0) // if the k n o c k s e q u e n c e

is c o r r e c t 411 { 412 i = 0; 413 p o s d i f f = 0; 414 bb ++; 415 416 for( ii = 0; ii < m a x k n o c k -1; ii ++) // z e r o e s the a r r a y s 417 { 418 t v e k [ ii ] = 0; 419 t f r a c [ ii ] = 0; 420 t d i f f [ ii ] = 0; 421 } 422 423 for( ii = 0; ii < m a x k n o c k ; ii ++) // z e r o e s p o s v e k 424 { 425 p o s v e k [ ii ] = 0; 426 } 427 428 for( ii = 1; ii <= 10; ii ++) // L E D b l u e b l i n k s 429 { 430 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 431 d e l a y ( 6 0 ) ; 432 d i g i t a l W r i t e ( LEDblue , LOW ) ; 433 d e l a y ( 6 0 ) ; 434 } 435 436 s t a t e = 4;

(48)

APPENDIX A. C CODE ON THE ARDUINO 437 b r e a k; 438 } 439 440 e l s e // if the k n o c k s e q u e n c e is w r o n g 441 { 442 i = 0; 443 p o s d i f f = 0; 444 445 for( ii = 0; ii < m a x k n o c k -1; ii ++) // z e r o e s the a r r a y s 446 { 447 t v e k [ ii ] = 0; 448 t f r a c [ ii ] = 0; 449 t d i f f [ ii ] = 0; 450 } 451 452 for( ii = 0; ii < m a x k n o c k ; ii ++) // z e r o e s p o s v e k 453 { 454 p o s v e k [ ii ] = 0; 455 } 456 457 for( ii = 1; ii <= 10; ii ++) // L E D y e l l o w b l i n k s 458 { 459 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 460 d e l a y ( 6 0 ) ; 461 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 462 d e l a y ( 6 0 ) ; 463 } 464 465 s t a t e = 1; 466 d e l a y ( 1 0 0 ) ; 467 b r e a k; 468 } 469 470 // -471 c a s e 4: // u n l o c k / l o c k the d o o r 472 473 s e r v o . a t t a c h ( 1 0 ) ; 474 475 if( d o o r s t a t e == 0) // if d o o r is locked , u n l o c k it and c h a n g e s t a t e to 1 ( u n l o c k e d ) 476 {

477 for( pos = l o c k e d ; pos <= u n l o c k e d ; pos ++)

478 { 479 s e r v o . w r i t e ( pos ) ; 480 d e l a y (5) ; 481 } // for u n l o c k 482 483 d o o r s t a t e = 1; 484 } // if l o c k e d 485 486 e l s e if ( d o o r s t a t e == 1) // if d o o r is u n l o c k e d , l o c k it and c h a n g e s t a t e to 0 ( l o c k e d )

(49)

487 {

488 for( pos = u n l o c k e d ; pos >= l o c k e d ; pos - -)

489 { 490 s e r v o . w r i t e ( pos ) ; 491 d e l a y (5) ; 492 } // for l o c k 493 494 d o o r s t a t e = 0; 495 } // e l s e if u n l o c k e d 496 497 s t a t e = 1; 498 s e r v o . d e t a c h () ; 499 d e l a y ( 1 5 0 ) ; 500 b r e a k; 501 502 // -503 504 c a s e 5: // w a i t i n g for k n o c k to i n i t i a t e r e c o r d i n g of s e c r e t s e q u e n c e 505 506 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 507 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 508 509 if( a n a l o g R e a d ( p i e z o 1 ) >= t h r e s h o l d 1 ) 510 { 511 s e c p o s s e q [ i ] = 1; 512 d i g i t a l W r i t e ( LEDblue , LOW ) ; 513 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 514 i ++; 515 t = m i l l i s () ; 516 s t a t e = 6; 517 d e l a y ( 7 5 ) ; 518 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 519 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 520 d e l a y ( 7 5 ) ; 521 } // if p i e z o 1 522 523 e l s e if( a n a l o g R e a d ( p i e z o 2 ) >= t h r e s h o l d 2 ) 524 { 525 s e c p o s s e q [ i ] = 2; 526 d i g i t a l W r i t e ( LEDblue , LOW ) ; 527 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 528 i ++; 529 t = m i l l i s () ; 530 s t a t e = 6; 531 d e l a y ( 7 5 ) ; 532 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 533 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 534 d e l a y ( 7 5 ) ; 535 } // if p i e z o 2 536 537 e l s e if( a n a l o g R e a d ( p i e z o 3 ) >= t h r e s h o l d 3 )

(50)

APPENDIX A. C CODE ON THE ARDUINO 538 { 539 s e c p o s s e q [ i ] = 3; 540 d i g i t a l W r i t e ( LEDblue , LOW ) ; 541 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 542 i ++; 543 t = m i l l i s () ; 544 s t a t e = 6; 545 d e l a y ( 7 5 ) ; 546 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 547 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 548 d e l a y ( 7 5 ) ; 549 } // if p i e z o 3 550 551 e l s e if( a n a l o g R e a d ( p i e z o 4 ) >= t h r e s h o l d 4 ) 552 { 553 s e c p o s s e q [ i ] = 4; 554 d i g i t a l W r i t e ( LEDblue , LOW ) ; 555 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 556 i ++; 557 t = m i l l i s () ; 558 s t a t e = 6; 559 d e l a y ( 7 5 ) ; 560 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 561 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 562 d e l a y ( 7 5 ) ; 563 } // if p i e z o 4 564 565 b r e a k; 566 567 // -568 569 c a s e 6: // r e c o r d i n g of s e c r e t s e q u e n c e 570 571 t e x i t = m i l l i s () - t ; 572 573 if( a n a l o g R e a d ( p i e z o 1 ) >= t h r e s h o l d 1 ) // r e c o r d s the t i m e

b e t w e e n k n o c k i and k n o c k i +1 and s t o r e s it in the a r r a y t v e k

574 { 575 d i g i t a l W r i t e ( LEDblue , LOW ) ; 576 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 577 t v e k [ i -1] = m i l l i s () - t ; 578 t = m i l l i s () ; 579 580 s e c p o s s e q [ i ] = 1; 581 582 i ++; 583 d e l a y ( 7 5 ) ; 584 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 585 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 586 d e l a y ( 7 5 ) ; 587 } // if p i e z o 1 588

(51)

589 e l s e if( a n a l o g R e a d ( p i e z o 2 ) >= t h r e s h o l d 2 ) // r e c o r d s the t i m e b e t w e e n k n o c k i and k n o c k i +1 and s t o r e s it in the a r r a y t v e k

590 { 591 d i g i t a l W r i t e ( LEDblue , LOW ) ; 592 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 593 t v e k [ i -1] = m i l l i s () - t ; 594 t = m i l l i s () ; 595 596 s e c p o s s e q [ i ] = 2; 597 598 i ++; 599 d e l a y ( 7 5 ) ; 600 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 601 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 602 d e l a y ( 7 5 ) ; 603 } // e l s e if p i e z o 2 604 605 e l s e if( a n a l o g R e a d ( p i e z o 3 ) >= t h r e s h o l d 3 ) // r e c o r d s the t i m e b e t w e e n k n o c k i and k n o c k i +1 and s t o r e s it in the a r r a y t v e k

606 { 607 d i g i t a l W r i t e ( LEDblue , LOW ) ; 608 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 609 t v e k [ i -1] = m i l l i s () - t ; 610 t = m i l l i s () ; 611 612 s e c p o s s e q [ i ] = 3; 613 614 i ++; 615 d e l a y ( 7 5 ) ; 616 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 617 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 618 d e l a y ( 7 5 ) ; 619 } // e l s e if p i e z o 3 620 621 e l s e if( a n a l o g R e a d ( p i e z o 4 ) >= t h r e s h o l d 4 ) // r e c o r d s the t i m e b e t w e e n k n o c k i and k n o c k i +1 and s t o r e s it in the a r r a y t v e k

622 { 623 d i g i t a l W r i t e ( LEDblue , LOW ) ; 624 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 625 t v e k [ i -1] = m i l l i s () - t ; 626 t = m i l l i s () ; 627 628 s e c p o s s e q [ i ] = 4; 629 630 i ++; 631 d e l a y ( 7 5 ) ; 632 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 633 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 634 d e l a y ( 7 5 ) ; 635 } // e l s e if p i e z o 4 636 637 if( t e x i t >= 2 0 0 0 || i == m a x k n o c k ) // if 2 s e c o n d s h a v e p a s s e d w i t h o u t a k n o c k or the s e q u e n c e is too l o n g 638 {

(52)

APPENDIX A. C CODE ON THE ARDUINO 639 640 if( i >= 2) // if 2 or m o r e k n o c k s are r e g i s t e r e d 641 { 642 t m a x = m a x i m u m ( t v e k ) ; // f i n d s the max v a l u e in t v e k 643 644 for( ii = 0; ii < i -1; ii ++) // d i v i d e s t v e k w i t h t m a x to get t f r a c 645 { 646 s e c k n o s e q [ ii ] = t v e k [ ii ]/ t m a x ; 647 } 648 649 for( ii = 1; ii <= 10; ii ++) // L E D b l u e b l i n k s 650 { 651 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 652 d e l a y ( 6 0 ) ; 653 d i g i t a l W r i t e ( LEDblue , LOW ) ; 654 d e l a y ( 6 0 ) ; 655 } 656 657 s t a t e = 1; 658 i = 0; 659 } // if 660 661 e l s e // o n l y 1 k n o c k is r e g i s t e r e d 662 { 663 for( ii = 0; ii < m a x k n o c k -1; ii ++) // z e r o e s the a r r a y 664 { 665 t v e k [ ii ] = 0; 666 } 667 668 for( ii = 0; ii < m a x k n o c k ; ii ++) // z e r o e s s e c p o s s e q 669 { 670 s e c p o s s e q [ ii ] = 0; 671 } 672 673 for( ii = 1; ii <= 10; ii ++) // L E D y e l l o w b l i n k s 674 { 675 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 676 d e l a y ( 6 0 ) ; 677 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 678 d e l a y ( 6 0 ) ; 679 } 680 681 s t a t e = 5; 682 i = 0; 683 d e l a y ( 1 0 0 0 ) ; 684 } // e l s e 685 686 } // if 2 sec or too l o n g 687 688 b r e a k; 689 690 //

(53)

-691 c a s e 7: // c a l i b r a t i o n of p i e z o s e n s o r s 692 693 d e l a y ( 7 0 0 0 ) ; 694 695 c a l 1 = 0; 696 c a l 2 = 0; 697 c a l 3 = 0; 698 c a l 4 = 0; 699 ii = 1; 700 701 w h i l e(1) // c a l i b r a t i o n for s e n s o r 1 702 { 703 if( a n a l o g R e a d ( p i e z o 1 ) > 1) 704 { 705 c a l 1 = c a l 1 + a n a l o g R e a d ( p i e z o 1 ) ; 706 ii ++; 707 } // if 708 709 if( d i g i t a l R e a d ( b u t t o n ) == LOW ) 710 { 711 t h r e s h o l d 1 = c a l 1 / ii ; 712 713 for( ii = 0; ii < 1; ii ++) // L E D y e l l o w b l i n k s 714 { 715 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 716 d e l a y ( 2 0 0 ) ; 717 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 718 d e l a y ( 2 0 0 ) ; 719 } 720 721 ii = 1; 722 d e l a y ( 5 0 0 0 ) ; 723 b r e a k; 724 } // if b u t t o n 725 726 } // w h i l e s e n s o r 1 727 728 w h i l e(1) // c a l i b r a t i o n for s e n s o r 2 729 { 730 if( a n a l o g R e a d ( p i e z o 2 ) > 1) 731 { 732 c a l 2 = c a l 2 + a n a l o g R e a d ( p i e z o 2 ) ; 733 ii ++; 734 } // if 735 736 if( d i g i t a l R e a d ( b u t t o n ) == LOW ) 737 { 738 t h r e s h o l d 2 = c a l 2 / ii ; 739 740 for( ii = 0; ii < 2; ii ++) // L E D y e l l o w b l i n k s 741 { 742 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 743 d e l a y ( 2 0 0 ) ;

(54)

APPENDIX A. C CODE ON THE ARDUINO 744 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 745 d e l a y ( 2 0 0 ) ; 746 } 747 748 ii = 1; 749 d e l a y ( 5 0 0 0 ) ; 750 b r e a k; 751 } // if b u t t o n 752 753 } // w h i l e s e n s o r 2 754 755 w h i l e(1) // c a l i b r a t i o n for s e n s o r 3 756 { 757 if( a n a l o g R e a d ( p i e z o 3 ) > 1) 758 { 759 c a l 3 = c a l 3 + a n a l o g R e a d ( p i e z o 3 ) ; 760 ii ++; 761 } // if 762 763 if( d i g i t a l R e a d ( b u t t o n ) == LOW ) 764 { 765 t h r e s h o l d 3 = c a l 3 / ii ; 766 767 for( ii = 0; ii < 3; ii ++) // L E D y e l l o w b l i n k s 768 { 769 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 770 d e l a y ( 2 0 0 ) ; 771 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 772 d e l a y ( 2 0 0 ) ; 773 } 774 775 ii = 1; 776 d e l a y ( 5 0 0 0 ) ; 777 b r e a k; 778 } // if b u t t o n 779 780 } // w h i l e s e n s o r 3 781 782 w h i l e(1) // c a l i b r a t i o n for s e n s o r 4 783 { 784 if( a n a l o g R e a d ( p i e z o 4 ) > 1) 785 { 786 c a l 4 = c a l 4 + a n a l o g R e a d ( p i e z o 4 ) ; 787 ii ++; 788 } // if 789 790 if( d i g i t a l R e a d ( b u t t o n ) == LOW ) 791 { 792 t h r e s h o l d 4 = c a l 4 / ii ; 793 794 for( ii = 0; ii < 4; ii ++) // L E D y e l l o w b l i n k s 795 { 796 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 797 d e l a y ( 2 0 0 ) ;

(55)

798 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 799 d e l a y ( 2 0 0 ) ; 800 } 801 802 b r e a k; 803 } // if b u t t o n 804 805 } // w h i l e s e n s o r 4 806 807 if( t h r e s h o l d 1 <= 1 || t h r e s h o l d 2 <= 1|| t h r e s h o l d 3 <= 1 || t h r e s h o l d 4 <= 1) 808 { 809 810 for( ii = 1; ii <= 10; ii ++) // L E D y e l l o w b l i n k s 811 { 812 d i g i t a l W r i t e ( L E D y e l l o w , H I G H ) ; 813 d e l a y ( 6 0 ) ; 814 d i g i t a l W r i t e ( L E D y e l l o w , LOW ) ; 815 d e l a y ( 6 0 ) ; 816 } // for 817 818 b r e a k; 819 } // if 820 821 e l s e 822 { 823 for( ii = 1; ii <= 10; ii ++) // L E D b l u e b l i n k s 824 { 825 d i g i t a l W r i t e ( LEDblue , H I G H ) ; 826 d e l a y ( 6 0 ) ; 827 d i g i t a l W r i t e ( LEDblue , LOW ) ; 828 d e l a y ( 6 0 ) ; 829 } // for 830 831 s t a t e = 1; 832 b r e a k; 833 } // e l s e 834 835 } // s w i t c h 836 837 } // l o o p

(56)
(57)
(58)

References

Related documents

symmetric power call option, on Figure 4.2(a), it pays off if up to maturity time the underlying foreign exchange rate is between the exercise price K and the upper barrier B

It was also observed that of all the models based on chemical kinetics it was only model KM10 which claimed that the ignition delay time is inversely proportional to the

Engine efficiency based on fuel flows and brake torque was within 26 - 40 % at different engine speeds and torque levels, which was lower than the same engine running with

I figur 9 visas ett förslag på hur flera enheter kan placeras i förhållande till varandra och kundroboten (orange cirkel, ej skalenlig) för att ge de efterfrågade tre

Based on studies (Crippa et al. 2012) done in other fields of research my hypothesis is that materials perceived as organic such as wood and fur will be considered a more

as  proliferation,  differentiation,  extracellular  matrix  production,  apoptosis,  tissue  repair 

-Ce ll Cr osstalk withi n the Bone Marr ow Je nn y Stjernber g Link öping 2011 Jenny Stjernberg 2011. Knock Knock Knock, Who

Förutsättningar för att skapa denna form av lärande och tillämpning av EBP är stöd från organisationens ledning med tydlighet i ansvar, stöd för kunskapsutveckling samt