Sunday, 8 February 2015

Arduino Knock Lock

Figure 1. The finished project

Arduino Starter Kit - Project 12

What Have I Done?

I made this project (Project 12) with my dad and we really enjoyed making it. The first time we put it together we noticed that it only locked when you clicked the button which was on the Inside of the box when we closed the lid. This meant that we had to alter the code to make the system lock when I knocked 3 times and that unlock when I knocked 3 times again. Basically we removed the switch completely! Below is the code we created to solve this.

Figure 1 shows the finished product in the box that the Arduino starter kit came in. It is being powered by a 9 volt battery. The black battery wire is inserted into the one labelled GND and  the red battery wire is inserted into the one labelled Vin.

Program code


#include <Servo.h>

Servo servo9;

const int piezo = A0;
const int switchPin = 2;
const int yellowLed = 3;
const int greenLed = 4;
const int redLed = 5;

int knockVal;
int switchVal;

const int quietKnock = 5;
const int loudKnock = 100;

boolean locked = false;
int numberOfKnocks = 0;

void setup() {
  servo9.attach(9);
  pinMode(yellowLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(switchPin, INPUT);
  Serial.begin(9600);
  digitalWrite(greenLed, HIGH);
  servo9.write(0);
  Serial.println("The box is unlocked!");
}

void loop() {
  if(locked == false) {
    /*switchVal = digitalRead(switchPin);
    if(switchVal == HIGH) {
      locked = true;
      digitalWrite(greenLed, LOW);
      digitalWrite(redLed, HIGH);
      servo9.write(90);
      Serial.println("The box is locked!");
      delay(1000);
    }*/
    knockVal = analogRead(piezo);
    if(numberOfKnocks < 3 && knockVal > 0) {
      if(checkForKnock(knockVal) == true) {
        numberOfKnocks++;
      }
      Serial.print(3 - numberOfKnocks);
      Serial.println(" more knocks to go");
    }
    if(numberOfKnocks >= 3) {
      servo9.write(90);
      locked = true;
      digitalWrite(greenLed, LOW);
      digitalWrite(redLed, HIGH);
      Serial.println("The box is locked!");
      numberOfKnocks = 0;
      delay(1000);
    }
  } 
  
  if(locked == true) {
    knockVal = analogRead(piezo);
    if(numberOfKnocks < 3 && knockVal > 0) {
      if(checkForKnock(knockVal) == true) {
        numberOfKnocks++;
      }
      Serial.print(3 - numberOfKnocks);
      Serial.println(" more knocks to go");
    }
    if(numberOfKnocks >= 3) {
      servo9.write(0);
      locked = false;
      digitalWrite(greenLed, HIGH);
      digitalWrite(redLed, LOW);
      Serial.println("The box is unlocked!");
      numberOfKnocks = 0;
      delay(1000);
    }
  }
}

boolean checkForKnock(int value) {
  if(value > quietKnock && value < loudKnock) {
    digitalWrite(yellowLed, HIGH);
    delay(50);
    digitalWrite(yellowLed, LOW);
    Serial.print("Valid knock of value ");
    Serial.println(value);
    return true;
  }
  else {
    Serial.print("Bad knock value ");
    Serial.println(value);
    return false;
  }
}