Coding
Coding
Coding
void setup() {
pinMode(lockMotor, OUTPUT);
pinMode(kamera, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(programSwitch, INPUT_PULLUP);
digitalWrite(kamera, HIGH);
Serial.begin(9600);
Serial.println("Program start.");
digitalWrite(greenLED, HIGH);
}
void loop() {
knockSensorValue = analogRead(knockSensor);
digitalWrite(kamera, HIGH);
if (digitalRead(programSwitch) == LOW) {
programButtonPressed = true;
// digitalWrite(kamera, LOW);
digitalWrite(redLED, HIGH);
} else {
programButtonPressed = false;
digitalWrite(redLED, LOW);
}
void listenToSecretKnock() {
Serial.println("knock starting");
int i = 0;
for (i = 0; i < maximumKnocks; i++) {
knockReadings[i] = 0;
}
int currentKnockNumber = 0;
int startTime = millis();
int now = millis();
digitalWrite(greenLED, LOW);
if (programButtonPressed == true) {
digitalWrite(redLED, LOW);
}
delay(knockFadeTime);
digitalWrite(greenLED, HIGH);
if (programButtonPressed == true) {
digitalWrite(redLED, HIGH);
}
do {
knockSensorValue = analogRead(knockSensor);
if (knockSensorValue >= threshold) {
Serial.println(knockSensorValue);
Serial.println("knock.");
now = millis();
knockReadings[currentKnockNumber] = now -
startTime;
currentKnockNumber ++;
startTime = now;
digitalWrite(greenLED, LOW);
if (programButtonPressed == true) {
digitalWrite(redLED, LOW);
}
delay(knockFadeTime);
digitalWrite(greenLED, HIGH);
if (programButtonPressed == true) {
digitalWrite(redLED, HIGH);
}
}
now = millis();
if (programButtonPressed == false) {
if (validateKnock() == true) {
triggerDoorUnlock();
} else {
Serial.println("Secret knock failed.");
digitalWrite(kamera, LOW);
digitalWrite(greenLED, LOW);
for (i = 0; i < 4; i++) {
digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, LOW);
delay(100);
}
digitalWrite(greenLED, HIGH);
delay(3000);
digitalWrite(kamera, HIGH);
}
} else {
validateKnock();
Serial.println("New lock stored.");
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
for (i = 0; i < 3; i++) {
delay(100);
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
delay(100);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
}
}
}
void triggerDoorUnlock() {
Serial.println("Door unlocked!");
int i = 0;
digitalWrite(lockMotor, HIGH);
digitalWrite(greenLED, HIGH);
delay (lockTurnTime);
digitalWrite(lockMotor, LOW);
boolean validateKnock() {
int i = 0;
int currentKnockCount = 0;
int secretKnockCount = 0;
int maxKnockInterval = 0;
if (programButtonPressed == true) {
for (i = 0; i < maximumKnocks; i++) {
secretCode[i] = map(knockReadings[i], 0,
maxKnockInterval, 0, 100);
}
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);
if (secretCode[i] > 0) {
delay( map(secretCode[i], 0, 100, 0,
maxKnockInterval));
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, HIGH);
}
delay(50);
}
return false;
}
if (currentKnockCount != secretKnockCount) {
return false;
}
int totaltimeDifferences = 0;
int timeDiff = 0;
for (i = 0; i < maximumKnocks; i++) {
knockReadings[i] = map(knockReadings[i], 0,
maxKnockInterval, 0, 100);
timeDiff = abs(knockReadings[i] - secretCode[i]);
if (timeDiff > rejectValue) {
return false;
}
totaltimeDifferences += timeDiff;
}
if (totaltimeDifferences / secretKnockCount >
averageRejectValue) {
return false;
}
return true;