Alarm System

By David Mayers

Description
This alarm system is similar to the ones that are used in houses. It uses a laser that sends a beam of light onto a photo resistor that is connected to the breadboard. If this beam is broken, then a piezo begins to make alarm noises and an LED flashes with a delay of 200 milliseconds. There is also a keypad numbered 1-8 made of push buttons, programmed with the code 1-2-3-4. If this code is entered, the alarm resets.

Pictures
(Click the pictures to enlarge them)

This picture shows my entire setup from a distance.


This is a picture of the setup closer up, excluding the laser portion.



This picture shows the laser, connected with alligator clips to negative power and pin 13.
The clip below it is simply used as support to keep the laser steadily aimed on the photo resistor.



This is a picture of the setup made using Fritz. (The Blue LED represents the laser)


Code Descriptions

This code was made to program my arduino board to set off an alarm when something passes through the laser. The particularly difficult portions were the using a variable to create a loop to make the alarm continue after initializing. To do this, I used the variable 'x'. The other part was the keypad, when I had to introduce the 8 different variables, A-H. They are used to produce a code that must be entered into the push buttons.

The Code

int ledPin = 12; //led
int laserPin = 11; //laser
int pRes = 2; //photo resistor.
int val = 0; //variable
int speaker = 13; //piezo.
byte x = 0; //variable used to save the state of the alarm.
byte z = 0; //variable used to delay the reading of the photo resistor so the laser can turn on first.
int pButton1 = 1; //these set the pin numbers for the 8 push buttons.
int pButton2 = 2;
int pButton3 = 3;
int pButton4 = 4;
int pButton5 = 5;
int pButton6 = 6;
int pButton7 = 7;
int pButton8 = 8;
byte a = 0; //these are variables used for the keypad.
byte b = 0;
byte c = 0;
byte d = 0;
byte e = 0;
byte f = 0;
byte g = 0;
byte h = 0;

void setup()
{
  pinMode(ledPin,OUTPUT); //the following just set the things as inputs or outputs.
  pinMode(laserPin,OUTPUT);
  pinMode(speaker,OUTPUT);
  pinMode(pButton1,INPUT);
  pinMode(pButton2,INPUT);
  pinMode(pButton3 ,INPUT);
  pinMode(pButton4 ,INPUT);
  pinMode(pButton5 ,INPUT);
  pinMode(pButton6 ,INPUT);
  pinMode(pButton7 ,INPUT);
  pinMode(pButton8 ,INPUT);
  Serial.begin(9600); // starts the serial monitor
}

void loop() {
digitalWrite(laserPin,HIGH); // turns on the laser
if (z == 0) //delays once at the beginning, then never delays again because z will always be 1.
{
  delay(1000); //this 1 second delay is needed so the alarm doesn't begin right when activated.
  z ++; //time is needed for the laser to turn on and contact the photo resistor.
}

val = analogRead(pRes); // sets the variable equal to the value of the photo resistor

if (val < 165) // if the laser's beam is broken, then x = 1.
{
  x = 1;
}

if (x == 1) // if x = 1, then the alarm begins.
{ // even if val goes back to being greater than 165, the x value doesn't change back.
 digitalWrite(ledPin,HIGH); //flashes the LED.
  delay(200);
  digitalWrite(ledPin,LOW);
  delay(200);
 int pitch = map(pRes, 100, 900, 200, 1000); //these two lines set the tone of the alarm together.
  tone(speaker,pitch,235);
}

 
if (digitalRead(pButton1)==HIGH) { //when the button is pressed, then a is 1.
  a = 1; }
 
if (digitalRead(pButton2)==HIGH) {
  b = 2; }
 
if (digitalRead(pButton3)==HIGH) {
  c = 3; }
 
if (digitalRead(pButton4)==HIGH) {
  d = 4; }
 
if (digitalRead(pButton5)==HIGH) {
  e = 5; }
 
if (digitalRead(pButton6)==HIGH) {
  f = 6; }
 
if (digitalRead(pButton7)==HIGH) {
  g = 7; }
 
if (digitalRead(pButton8)==HIGH) {
  h = 8; }
 
if (a == 1) { // if buttons 1-2-3-4 are pressed, and 5-6-7-8 have not been pressed, then the alarm resets.
  if (b == 2) {
    if (c == 3) {
      if (d == 4) {
        if (e != 5) {
          if (f != 6) {
            if (g != 7) {
              if (h != 8) {
                digitalWrite(ledPin,LOW);
                 int pitch = map(0,0,0,0,0); //these lines reset alarm and LED flash.
                 tone(speaker,pitch,235);
                 delay(10);
                 if (val > 165)
                 {
                    b = 0;
                    c = 0;
                    d = 0;
                    e = 0;
                    f = 0;
                    g = 0;
                    h = 0;
                    x = 0;e
                 }
              }
            }
          }
        }
      }
    }
  }
}
   
    Serial.println(val); // prints the value of the photo resistor to the serial monitor. Just a check of the value for the alarm to work.
}


Problems
Some problems I encountered while making this alarm include:
1. At first, I had trouble finding a suitable pitch for the piezo for an alarm system, but after I learned more about making sound, I was able to create a distinctive alarm-like sound.
2. I had a problem making a connection to the second push-button, but this was solved after I realized the ends of the wire connecting it to positive power were not long enough to make contact.
3. When I first started making this project, I was actually making a camera that detects movement with the laser sensor and catches cool images that couldn't be taken by hand. This proved to be too complicated         with the inside of the camera, so I ended that, but was left over with the laser sensor. That led to me making this alarm system.

Changes I Would Make
1. If I had more money, I would probably add something to make the alarm louder, so it is more similar to what is common in houses.
2. If I had more time, I would try to make a more organized push-button area for the keypad, labeled with numbers.
3. If I had more time, I would also find a box to make the alarm system more organized and user-friendly.
Comments