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 Food Cue

Screen Shot 2014-10-09 at 5.03.55 PM

So one of the things that I always find myself confused in when I have meals in the dining hall is where exactly I should go to bus my trays. There were times that for lunch, trays were supposed to be bussed on the East side, and for dinner, it has to be bussed on the West side.

So, what I decided to come up with, was using pretty much the same code and technology as the Mood Cue, I created a food cure, where you are told where to bus your trays! It’s really cool!

Check the video below!

The Mood Cue

Ever wanted to tell people what your mood is electronically without having them bother you by knocking? Then the mood cue is here for you! You can easily turn a knob and tell people whether or not you want them to visit you! Isn’t this amazing?

How this project works is that using a servo motor a hand cues at something, points to something. The servo motor actually comes prepared with an entire library that can be used to control the motors in almost any way you want. The basics though are that, there is a pin that can move from 0 to 179 degrees, and that just pretty much lays the foundations for a very good way to signal things (think of like old-fashioned gas station pumps that have full/empty or just your car’s petrol gauge).

So, what I did was that using a knob, I could move the servo motor’s hands to whatever direction within 179 degrees I wanted to. And from that, I could tell people what my mood is currently.

See the video I made with the Mood Cue!

Also, check out the code that makes this thing work!


//Incldues the Servo Library
#include <Servo.h>

//Declares the Servo Motor (so I can just call this!)

Servo myServo;

//Variables
const int potPin=A0;
int potVal=0;
int angle=0;

//Begin Comms
void setup(){
myServo.attach(9);
Serial.begin(9600);

}

//The Magic
void loop(){
//Measures how much the knob is turned.
potVal=analogRead(potPin);
Serial.print("The Pot Value: ");
Serial.print(potVal);

//Translates that to the Servo motor!
angle=map(potVal,0,1023,0,179);
Serial.print(", angle: ");
Serial.println(angle);

myServo.write(angle);
delay(15);

}

So, that’s pretty much what I did! It’s a really cool thing to create! As I’ve mentioned in the video, some problems that I had with this project was that the potentiometer(knob) as well as the servo motor has a weird female head that even with the adapter doesn’t fit at all on the Arduino. I hope this can be sorted out because as you can see what I did in the video, I had to manually hold both apparatus down.

Other than that, it was a really cool project! And in fact, I had so much fun working on this that my extended project worked on using this technology to solve a problem here at NYUAD. Stay tuned for that!