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.

Midterm: The Arduino Piezo Piano Christmas Concert

Christmas is coming! So, in line with the festive season, I built a Arduino Piano Keyboard that had all the 7 basic notes–CDEFGAB, and of course, played Christmas songs with it! Yes, it has been a while since I last played an instrument, but the excitement of the Christmas season just made things a lot easier.

Basically, to build this Piezo Piano, I obviously used the Arduino, just made a simple circuit with different resistors and buttons. The different resistors differentiated the different buttons, and once I received that input into the computer, I played different notes through the piezo (it’s the black circular device which makes sounds)!

Given that I didn’t really have a good background in music, I had to learn the staff, the different notes and how to play these notes on the Arduino, and as well as program these notes. It was very rewarding though, because after the initial struggles playing, I was actually able to play three Christmas songs decently: Jolly Old Saint Nicholas, Ode to Joy and Jingle Bells!

So, without further ado, here is my Arduino Piezo Piano Christmas Concert!

I hope you enjoyed it! If you’d want to build a piano like mine with the same pitch (correct me if this term is wrong), then the code is right over here for you to look at! You can build one of these awesome piano keyboards too!


//This array contains the different notes, CDEFGAB
int notes[]={261.63,293.66,329.63,349.23,392.00,440,480};

//The remnants of the last play function.
int lastplay[]={};

void setup(){
  //This begins communication with the Arduino.
  Serial.begin(9600);
}

void loop(){
  //This is so we find out which button is pressed!
  int keyVal=analogRead(A0);
  //And this prints it out!
  Serial.println(keyVal);
     // Button1, C
     if(keyVal<=1023 && keyVal>=1013){
       tone(8,notes[0]);
     // Button2, D
     }else if(keyVal>=998 && keyVal<=1112){
       tone(8,notes[1]);
     // Button3, E
     }else if(keyVal>=501 && keyVal<=522){
       tone(8,notes[2]);
     // Button4, F
     }else if(keyVal>=1 && keyVal<=16){
       tone(8,notes[3]);
     // Button5, G
     }else if(keyVal>=680 && keyVal<=705){
       tone(8,notes[4]);
    // Button6, A
     }else if(keyVal>=960 && keyVal<=980){
       tone(8,notes[5]);
    // Button1, B
     }else if(keyVal>=920 && keyVal<=940){
       tone(8,notes[6]);
     }else{
    //If no button is pressed, no sound is made.
       noTone(8);
     }
}

I sincerely hoped you liked this project! Season’s greetings everyone! It’s now time to sing some Christmas carols! Jingle bells, jingle bells, jingle all the way!

Light Theremin

Another project I was working on over the break was the light theremin. What it does is simple: just like in sci-fi movies, move your hands over a certain region, and depending on the movement, you can create music with it! (Warning: The tones that the piezo creates is very eerie though!)

Anyway, how it works is that a light sensor is present in the region where you move your hands, and depending on the amount of light that reaches the sensor, a certain tone is mapped onto the piezo! You can take a look at the code that I have below.

But before that, here’s a video of what I did!

And here’s the super short code that makes all this happen!


//Declaring Variables

int sensorValue;
int sensorLow=1023;
int sensorHigh=0;

const int ledPin=13;

void setup(){
pinMode(ledPin,OUTPUT);
digitalWrite(ledPin,HIGH);

//Calibration of Light in the Room
while(millis()<5000){
sensorValue=analogRead(A0);
if(sensorValue<sensorLow){
sensorLow=sensorValue;
}
if(sensorValue>sensorHigh){
sensorHigh=sensorValue;
}
digitalWrite(ledPin,LOW);
}
}

//Mapping the Light to the Tone

void loop(){
sensorValue=analogRead(A0);
int pitch=map(sensorValue,sensorLow,sensorHigh,0,4000);
tone(8,pitch,20);
delay(10);
}

So, it’s a pretty cool invention, and once I get a good beat on this, I’ll upload another video! But for now, enjoy the short sequence I got up there!