Knock Lock Fun!

Locks always had a special place in my heart. I remember the amazement of playing with a lock and a key when I was in 4th grade. And then in 5th grade, I remember how we all got to have our own lockers, and hence we had to buy our own locks. My mom got me a silver one that was branded Yale for some weird reason, and that lock served me very well. It was only in 11th grade that I must have misplaced it somewhere. But you get the idea.

And so, now that I had to work on a knock-lock, of course I was really really excited. Basically, what happens is that you press a button to “lock” something and the servo motor turns its blades to lock something. To unlock the device, you have to tap the piezo three times successfully to be able to move the blade attached to the servo motor back to unlock the device. There are LEDs that tell you if your knock was successfully registered or not. In terms of code, it is very straightforward. After writing code that triggers the servo from the switch to lock the object, you just take in the input from the piezo, see if it reaches the threshold, and then once three knocks are made, then move the piezo. It was actually pretty fun!

Here’s a video of me playing with the Knock-Lock!

If you’re interested in the code, here’s the code too!


//This includes the servo motor we're going to use!
#include <Servo.h>
Servo myServo;

//Declaring the Variables(LEDs,Piezo,Switches,etc.)
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=10;
const int loudKnock=100;

boolean locked=false;
int numberOfKnocks=0;

void setup(){
  //Set the inputs and outputs
  myServo.attach(9);
  pinMode(yellowLed,OUTPUT);
  pinMode(greenLed,OUTPUT);
  pinMode(redLed,OUTPUT);
  pinMode(switchPin,INPUT);

  //Start Serial Communication
  Serial.begin(9600);

  //Prepare the Arduino for the Start State
  digitalWrite(greenLed,HIGH);
  myServo.write(0);
  Serial.println("The box is unlocked!");
}

void loop(){
  //What to do when Unlocked
  if(locked==false){
    switchVal=digitalRead(switchPin);
      if (switchVal==HIGH){
        locked=true;
        digitalWrite(greenLed,LOW);
        digitalWrite(redLed,HIGH);
        myServo.write(90);
        Serial.println("The box is locked!");
        delay(1000);
      }
  }

  //What to do when locked
  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){
      locked=false;
      myServo.write(0);
      delay(20);
      digitalWrite(greenLed,HIGH);
      digitalWrite(redLed,LOW);
      Serial.println("The box is unlocked!");
      numberOfKnocks=0;
    }
  }
}

//Code that checks the validity of a knock; Used to unlock the box
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;
  }
}

So there you go, this is the knock-lock! The modern more-futuristic version of that old rusty Yale lock I had back in 11th grade. Of course, it can’t replace the brotherhood I had with that lock, but yea, this new way of locking and unlocking this seems way cooler.

The Green Light District: My First Arduino Project

Taking it apart was was such a hard thing to do. All of the beautifully wrapped elements felt like they weren’t for touching. It’s like when you get a new book you know, you don’t really want to crumple the pages or line a crease down the spine.

Eventually, I caved in!

And walla, here’s my first Andruino Project! Enjoy!

 

Let me know what you think in the comments!