SLIDER

UNIKL BMI  LASER POINTER PHOTORESISTOR ARDUINO UNO R3

WEEK 10 FYP 2

Date: 17/11/2014 (Monday)
Title:  Combine All Code for project.

All the coding for the project were constructed one by one according to the step of the project runnin. All of the coding will be program in the arduino to be display as a complete code .

Examples of source code for the project:



/*
 Project Title: Laser Home Security System
 Author: AHMAD SHAZRUL AMIN BIN MOHD ZULKIFLI
 SEMESTER: 7
 Term: JAN 2014
 Started Date: 2/1/2014
 Due date: 23/12/2014
 Version: 1.0
 Revision Date: 2/1/2014,
*/

////////////////////////////////////////////////////////

#include <LiquidCrystal.h>
#include <Wire.h>
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip

int passwd_pos = 15;
//Password
Password password = Password( "1234" );

//Keypad
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

char keys[ROWS][COLS] = { // Define the Keymap
  {
    '1','2','3','A'      }
  ,
  {
    '4','5','6','B'      }
  ,
  {
    '7','8','9','C'      }
  ,
  {
    '*','0','#','D'      }
};

byte rowPins[ROWS] = {
  23, 25, 27, 29};     //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  31, 33, 35, 37};     //connect to the column pinouts of the keypad
  
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); // Assignign arduino pins to LCD display module

//constants for LEDs, inputs and outputs
int greenLED =6 ;
int WhiteLED = 7;
int speakerPin = 4; 
int Laser =3;
int Sensor1 = 0;
int redPin = 5;

//Variables
int alarmStatus = 0;
int zone = 0;
int alarmActive = 0;

void setup(){
  lcd.begin(20,4);
  Wire.begin();
  Serial.begin(9600);
  
  displayCodeEntryScreen();
  //LED alarm setup and turn off both LEDs
  pinMode(WhiteLED,OUTPUT);
  pinMode(Laser,OUTPUT);
  pinMode(greenLED,OUTPUT);
  pinMode(speakerPin,OUTPUT);
  pinMode(redPin,OUTPUT);
  pinMode(Sensor1,INPUT);
  
  digitalWrite(Laser,LOW);
  digitalWrite(WhiteLED, LOW);
  digitalWrite(greenLED, HIGH);
  digitalWrite(speakerPin, LOW);
  
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop(){
  keypad.getKey();
  int lightVal= analogRead(Sensor1);
  if (alarmActive == 1){ 
   if (lightVal < 200 && lightVal > 0)
   //if (digitalRead(Sensor1) == HIGH)
    {
      zone = 0;
      alarmTriggered();
    }
  
   }
}
  
  
///////////////////////////  Functions  /////////////////////////////////
//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
  case PRESSED:
    if (passwd_pos - 15 >= 5) { 
      return ;
    }
    lcd.setCursor((passwd_pos++),0);
    switch (eKey){
    case '#':                 //# is to validate password 
      passwd_pos  = 15;
      checkPassword(); 
      break;
    case '*':                 //* is to reset password attempt
      password.reset();
      //lcd.clear(); 
      passwd_pos = 15;
     //TODO: clear the screen output 
      break;
    default: 
      password.append(eKey);
      lcd.print("*");
    }
  }
}
void checkPassword(){                  // To check if PIN is corrected, if not, retry!
  if (password.evaluate())
  {  
    if(alarmActive == 0 && alarmStatus == 0)
    {
      activate();
    } 
    else if( alarmActive == 1 || alarmStatus == 1) {
      deactivate();
    }
  } 
  else {
    invalidCode();
  }
}  
void activate()      // Activate the system if correct PIN entered and display message on the screen
{
  int lightVal= analogRead(Sensor1);
  Serial.println(analogRead(Sensor1));
  delay(500);
  digitalWrite(Laser,HIGH);
  delay(2000);
  if (lightVal < 800 && lightVal > 201){
 // if (lightVal > 0){
 // if (lightVal < 200 && lightVal > 0){
    //if (alarmStatus = 1){
    //analogRead(Sensor1,HIGH);
    //if(analogRead(Sensor1)==HIGH) {
    //if(analogRead(Sensor1)== LOW){
    digitalWrite(WhiteLED, HIGH);
    digitalWrite(greenLED, LOW);
    //digitalWrite(Laser,HIGH);
    digitalWrite(redPin,HIGH);
    digitalWrite(2, LOW);
    lcd.setCursor(0,0);
    lcd.print("SYSTEM ACTIVE!"); 
    alarmActive = 1;
    password.reset();
    delay(2000);
    }
  else {
    alarmTriggered();
    //deactivate();   // if PIN not corrected, run "deactivate" loop
  }
    }  

 

 void invalidCode()    // display meaasge when a invalid is entered
{
  password.reset();
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("INVALID CODE! LOL!");
  lcd.setCursor(5,2);
  lcd.print("TRY AGAIN!");
  
  //digitalWrite(speakerPin,HIGH);
 
  digitalWrite(greenLED, LOW);
  digitalWrite(WhiteLED, HIGH);
  digitalWrite(redPin,HIGH);
  digitalWrite(Laser,LOW);
  
  lcd.noDisplay();
  delay(500);
  lcd.display();
  delay(500);
  lcd.noDisplay();
  delay(500);
  lcd.display();
  delay(500);
  lcd.noDisplay();
  delay(500);
  lcd.display();
  delay(500);
  
  delay(2000);
  digitalWrite(WhiteLED, LOW);
  delay(500);
  //digitalWrite(speakerPin,LOW);
  digitalWrite(redPin,LOW);
  digitalWrite(greenLED,HIGH);
  displayCodeEntryScreen();
  Tone();
 
void deactivate()
{
  alarmStatus = 0;
  digitalWrite(WhiteLED, LOW);
  digitalWrite(greenLED, HIGH);
 // digitalWrite(Laser,LOW);
  digitalWrite(redPin,LOW);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" SYSTEM DEACTIVATED!");
  digitalWrite(speakerPin, LOW);
  alarmActive = 0;
  password.reset();
  delay(5000);

  displayCodeEntryScreen();
}
void alarmTriggered(){
  
  int incr;
 // int lightVal =analogRead(Sensor1);
  delay(500);
  digitalWrite(speakerPin, HIGH);
  digitalWrite(Laser,LOW);
  digitalWrite(redPin, HIGH);
//
   password.reset();
  alarmStatus = 1;
  //alarmActive = 0;
  lcd.clear();
  lcd.setCursor(0,2);
  lcd.print("  SYSTEM TRIGGERED  ");
  lcd.setCursor(0,4);
 
   if(zone == 0){
    lcd.print("  SENSOR DETECTED!! ");
    delay(1000);
  }
  //if (val < 60  ) {
  //   incr = 1;
  // } else {
  //   incr = -1;
  // }
}
 //
void Tone()
{
tone(7, 494, 500);
delay(1000);
}
void t1()
{
  tone(7, 494, 500);
  delay(2000);
//take care of some special events
void Detected()
{
  int val = analogRead(Sensor1);
  Serial.println(analogRead(Sensor1));
  delay(500);
  
  if(val <60) digitalWrite(redPin,HIGH);
  else digitalWrite(redPin,LOW);
 
}
void displayCodeEntryScreen()    // Dispalying start screen for users to enter PIN
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Enter 4-PIN:");
  lcd.setCursor(0,1);
  lcd.print("51211112225");
  lcd.setCursor(0,2);
  lcd.print("Laser Security Home");
  lcd.setCursor(0,3);
  lcd.print("By Shazrul, UNIKLBMI");
}