67 #define CHILD_ID 99 // Id of the sensor child
69 const byte eepromValid = 121;
72 const int programButton = 0;
74 const int knockSensor =
78 const int lockPin = 4;
83 const int rejectValue =
85 const int averageRejectValue =
87 const int knockFadeTime =
89 const int lockOperateTime =
91 const int maximumKnocks = 20;
92 const int knockComplete =
95 byte secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
96 int knockReadings[maximumKnocks];
97 int knockSensorValue = 0;
98 bool programModeActive =
false;
102 MyMessage lockMsg(CHILD_ID, V_LOCK_STATUS);
107 pinMode(ledPin, OUTPUT);
108 pinMode(knockSensor, INPUT);
109 pinMode(lockPin, OUTPUT);
110 pinMode(programButton, INPUT);
111 digitalWrite(programButton,
HIGH);
115 digitalWrite(lockPin,
120 setLockState(lockStatus,
true);
134 knockSensorValue = digitalRead(knockSensor);
135 if (digitalRead(programButton) ==
LOW) {
137 if (digitalRead(programButton) ==
LOW) {
138 if (programModeActive ==
false) {
139 programModeActive =
true;
140 digitalWrite(ledPin,
HIGH);
144 programModeActive =
false;
145 digitalWrite(ledPin,
LOW);
150 while (digitalRead(programButton) ==
LOW) {
158 if (knockSensorValue == 0) {
159 if (programModeActive ==
true) {
160 digitalWrite(ledPin,
LOW);
162 digitalWrite(ledPin,
HIGH);
165 if (programModeActive ==
true) {
166 digitalWrite(ledPin,
HIGH);
168 digitalWrite(ledPin,
LOW);
170 listenToSecretKnock();
176 void listenToSecretKnock()
180 for (i=0; i < maximumKnocks; i++) {
181 knockReadings[i] = 0;
184 int currentKnockNumber = 0;
185 uint32_t startTime = millis();
189 knockSensorValue = digitalRead(knockSensor);
191 if (knockSensorValue == 0) {
192 Serial.println(
"knock");
195 knockReadings[currentKnockNumber] = now - startTime;
196 currentKnockNumber ++;
199 if (programModeActive==
true) {
200 digitalWrite(ledPin,
LOW);
202 digitalWrite(ledPin,
HIGH);
205 if (programModeActive ==
true) {
206 digitalWrite(ledPin,
HIGH);
208 digitalWrite(ledPin,
LOW);
215 }
while ((now-startTime < knockComplete) && (currentKnockNumber < maximumKnocks));
216 Serial.println(
"end");
219 if (programModeActive ==
false) {
220 if (validateKnock() ==
true) {
224 setLockState(!lockStatus,
true);
226 Serial.println(
"fail unlock");
229 for (i=0; i < 4; i++) {
230 digitalWrite(ledPin,
HIGH);
232 digitalWrite(ledPin,
LOW);
243 void setLockState(
bool state,
bool doSend)
246 Serial.println(
"open lock");
248 Serial.println(
"close lock");
254 digitalWrite(ledPin, state);
255 digitalWrite(lockPin, state);
267 int currentKnockCount = 0;
268 int secretKnockCount = 0;
269 int maxKnockInterval = 0;
271 for (i=0; i<maximumKnocks; i++) {
272 if (knockReadings[i] > 0) {
275 if (secretCode[i] > 0) {
279 if (knockReadings[i] > maxKnockInterval) {
280 maxKnockInterval = knockReadings[i];
285 if (programModeActive ==
true) {
286 for (i=0; i < maximumKnocks; i++) {
287 secretCode[i] = map(knockReadings[i], 0, maxKnockInterval, 0, 100);
290 programModeActive =
false;
291 playbackKnock(maxKnockInterval);
295 if (currentKnockCount !=
305 int totaltimeDifferences = 0;
306 for (
int timeDiff = 0, i=0; i < maximumKnocks; i++) {
307 knockReadings[i]= map(knockReadings[i], 0, maxKnockInterval, 0, 100);
308 timeDiff = abs(knockReadings[i] - secretCode[i]);
313 totaltimeDifferences += timeDiff;
316 if (totaltimeDifferences / secretKnockCount > averageRejectValue) {
325 void readSecretKnock()
329 if (reading == eepromValid) {
330 for (
int i=0; i < maximumKnocks ; i++) {
338 void saveSecretKnock()
342 for (
int i=0; i < maximumKnocks; i++) {
349 void playbackKnock(
int maxKnockInterval)
351 digitalWrite(ledPin,
LOW);
353 digitalWrite(ledPin,
HIGH);
355 for (
int i = 0; i < maximumKnocks ; i++) {
356 digitalWrite(ledPin,
LOW);
358 if (secretCode[i] > 0) {
359 delay(map(secretCode[i], 0, 100, 0,
361 digitalWrite(ledPin,
HIGH);
365 digitalWrite(ledPin,
LOW);
371 int itterations = (knockFadeTime /
373 for (
int i=0; i < itterations; i++) {
384 void chirp(
int playTime,
int delayTime)
386 long loopTime = (playTime * 1000L) / delayTime;
387 pinMode(audioOut, OUTPUT);
388 for(
int i=0; i < loopTime; i++) {
389 digitalWrite(audioOut,
HIGH);
390 delayMicroseconds(delayTime);
391 digitalWrite(audioOut,
LOW);
393 pinMode(audioOut, INPUT);
401 if (message.
getType()==V_LOCK_STATUS) {
403 setLockState(message.
getBool(),
false);
406 Serial.print(
"Incoming lock status:");
407 Serial.println(message.
getBool());