Friday, 13 March 2020

Arduino - PIR MOTION ALARM

Hello world! Today i made a simple and very cheap to build alarm System with a keypad, pir sensor, relay and a very loud alarm strobe. You have 30 seconds time to leave the room after activating the alarm system, of course this can be changed in the code to less or more seconds. Now it has been set to 30000 milliseconds.
You can change it at this line in the code:
const int timeDelay = 30000; //give yourself time to leave the room

Follow the next step for the Schematic, Code & Parts list.



Simply wire it up like the schematic, Upload the code and it should work.

Make sure you have these libraries installed, download them below.
Parts list:

Arduino uno, I used the RobotDyn uno. Which has 2 extra analog pins
Potentiometer
LCD 1602
Pir motion sensor DSN FIR800
Ky-019 Relay module
1 x Blue Led
1 x Red led
2 x 1 k Resistor.
4x4 Keypad
12v Alarm Strobe


gizlenmis kod

#include //http://playground.arduino.cc/Main/LiquidCrystal
#include //http://playground.arduino.cc/Code/Keypad
#include //http://playground.arduino.cc/Code/Password

int passwd_pos = 7;
Password password = Password("1589");

LiquidCrystal lcd(5, 4, 3, 2, 1, 0);

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 11, 10};
byte colPins[COLS] = {9, 8, 7, 6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

const int pirPin = A0;
const int Relay = A1;
const int redPin = A2;
const int bluePin = A3;
const int ledError = 50;
const int timeDelay = 30000; //give yourself time to leave the room
int alarmStatus = 0;
int alarmActive = 0;

void setup(){
  lcd.begin(16,2);
  pinMode(Relay, OUTPUT);
  pinMode(pirPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  keypad.addEventListener(keypadEvent);
  lcd.setCursor(0,0);
  lcd.print("Motion Alarm");
  lcd.setCursor(0,1);
  lcd.print("BlueCore Tech");
  delay(1600);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Created by");
  lcd.setCursor(0,1);
  lcd.print("D.Van Den Brande");
  delay(2000);
  digitalWrite(bluePin, HIGH);
  displayCodeBlock();



}

void loop(){
  keypad.getKey();
  if (alarmActive == 1){
    if (digitalRead(pirPin) == HIGH){
      alarmTriggered();

    }
  }
}

void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
    if (passwd_pos - 15 >= 5){
      return ;
    }
    lcd.setCursor((passwd_pos++),0);
    switch (eKey){
    case '#':
    passwd_pos  = 7;
    checkPassword();
    break;
    case '*':               
    password.reset();
    passwd_pos = 7;
    break;
    default:
    password.append(eKey);
    lcd.print("*");
   }
  }
}

void alarmTriggered(){
  password.reset();
  alarmStatus = 0;
  digitalWrite(Relay, HIGH);
  digitalWrite(redPin, HIGH);
  digitalWrite(bluePin, LOW);
  lcd.clear();
    displayCodeUnBlock(); //modified
    StrokeLight();{
 
  }


void StrokeLight(){
  for (int i=0; i<=10; i++){
    if(i%2){
    digitalWrite(redPin, HIGH);
    }
    else{
    digitalWrite(redPin, LOW);
    }
    delay(ledError);
  }
  for (int i=0; i<=10; i++){
    if(i%2){
      digitalWrite(bluePin, HIGH);
    }
    else{
      digitalWrite(bluePin, LOW);
    }
    delay(ledError);
  }
  digitalWrite(redPin, HIGH);
  delay(100);

}                                     

void checkPassword(){
  if (password.evaluate()){
    if(alarmActive == 0 && alarmStatus == 0){
      activate();
    }
    else if( alarmActive == 1 || alarmStatus == 1){
      deactivate();
    }
  }
  else{
  invalidCode();
 }
}

void invalidCode(){
  digitalWrite(bluePin, LOW);
  password.reset();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Toegang geweigerd");
  ledInvalid();
}

void ledInvalid(){
  for (int i=0; i<=10; i++){
    if(i%2){
    digitalWrite(redPin, HIGH);
    }
    else{
    digitalWrite(redPin, LOW);
    }
    delay(ledError);
  }
  if(alarmActive == 0 && alarmStatus == 0){
    digitalWrite(Relay, LOW);
    digitalWrite(bluePin, HIGH);
    digitalWrite(redPin, LOW);
    delay(200);
    displayCodeBlock();
  }
  else if( alarmActive == 1 || alarmStatus == 1){
    digitalWrite(Relay, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(bluePin, LOW);
    delay(200);
  }
}

void activate(){
  alarmActive = 1;
  password.reset();
  digitalWrite(bluePin, LOW);
  digitalWrite(redPin, HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Systeem actief");
  delay(timeDelay);
}

void deactivate(){
  alarmStatus = 0;
  alarmActive = 0;
  password.reset();
  digitalWrite(Relay, LOW);
  digitalWrite(bluePin, HIGH);
  digitalWrite(redPin, LOW);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Gedeactiveerd");
  delay(1000);
  displayCodeBlock();
}

void displayCodeUnBlock(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Code:");
  lcd.setCursor(0,1);
  lcd.print("Deactiveren");
}

void displayCodeBlock(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Code:");
  lcd.setCursor(0,1);
  lcd.print("Activeren");
}


https://kulogluelektronik.blogspot.com

No comments: