deadmouse93
Published

Modular alarm system with motion detection!

Build your own alarm system with Arduino board, PIR motion sensor, siren, 4x4 keypad and oled display, with possibility to add more modules.

BeginnerFull instructions provided1,061
Modular alarm system with motion detection!

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Hardware and wiring

Code

Arduino_AlarmSystemCODE

Arduino
Codo for alarm system with motion detection.
#include <Keypad.h>
#include <SoftwareSerial.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>

#define Password_Length 6

char Data[Password_Length];
char Master[Password_Length] = "B1234;
byte data_count = 0;
char customKey;

// *** KEYPAD CONFIGURATION ***
const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys [ROWS][COLS]={
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
  }; // Keys on keypad

byte rowPins[ROWS] = {9,8,7,6};
byte colPins[COLS] = {5,4,3,2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

unsigned long tiempo1, tiempo2;

int siren     = 10;
int signalPin = 11; // digitalni pin
int pirSensor = 12; // digitalni pin
int led_Alarm = 13; // digitalni pin

int alarmSystem = 1; //ALARM IS OFF (0) OR ON (1)

int senzorValue = 0;

int ledHigh = 0;

int counter = 0;

int state = 0;

U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); 


void setup() {

  Serial.begin(9600);
  u8g2.begin();
  delay(5000);
  Serial.println("Secutiry Alarm System");
  pinMode(signalPin, OUTPUT);
  pinMode(pirSensor, INPUT);
  pinMode(led_Alarm, OUTPUT);
  pinMode(siren, OUTPUT);
  Serial.print("Alarmsystem has status:");
  Serial.println(alarmSystem);
  Serial.println("Program started");
  delay(1999);  
}

void loop() {

  if (alarmSystem == 1){

    if (state == 0){
      digitalWrite(led_Alarm, LOW);
      digitalWrite(siren, LOW);
      u8g2.clearBuffer(); 
      u8g2.setFont(u8g2_font_courR10_tf);
      u8g2.drawStr(8,10, "Alarm is");
      u8g2.drawStr(8,24, "ON!");
      u8g2.sendBuffer();
      tiempo1 = millis(); // read the internal clock
      tiempo2 = millis(); // read the internal clock
      Serial.println("WE ARE IN STATE 0");  
      Event();
      senzorValue = digitalRead(pirSensor); // Sensor returns 1 - HIGH, if detects motion and 0 - LOW if does not detect motion
                                            // Senzor nam vrne 1 - HIGH e je zaznal gibanje in 0 - LOW e ni zaznal gibanja

      if (senzorValue == HIGH){
        state = 1;
        u8g2.clearBuffer(); 
        u8g2.setFont(u8g2_font_courR14_tf);
        u8g2.drawStr(16,12, "MOTION");
        u8g2.drawStr(16,28, "DETECTED!");
        u8g2.sendBuffer();

        while (tiempo1 < tiempo2 + 20000){
          customKey = customKeypad.getKey();
          if (customKey == '*'){
            Event2();
            tiempo1 = tiempo1 + 50000;
            //Serial.println("SMO TUKAJ 1");
            Serial.println(tiempo1);
            Serial.println(tiempo2);
            delay(2000);
          }
          else{tiempo1 = millis(); 
		  //Serial.println("SMO TUKAJ 2");
		  }
        }
      }
    }
    
    if (state == 1){  

      // ADD COUNT DOWN FOR TURN ON ALARM SYSTEM, IF PASSWORD IS   
      // DODAMO COUNT DOWN ZA PRIIG ALARMA, E V TEM ASU VNESE GESLO SE ALARM RESETIRA!
      
      tiempo2 = millis();
      unsigned long i = 5000;
      tiempo2 = tiempo2 + i;
      int x = 0;
      int y = 0;
  
      while (tiempo1 < tiempo2) // loop for counting down 30 seconds if password is correct
      {
        //Serial.println("SMO TUKAJ 3");      
        tiempo1 = millis();
        x = (tiempo2 - tiempo1) / 1000;
  
        // if is oply for print on the display!
        if ( y != x){
          u8g2.clearBuffer();          
          u8g2.setFont(u8g2_font_courR14_tf);
          u8g2.drawStr(16,12, "Countdown: ");
          u8g2.setCursor(40,32);
          u8g2.print(x);
          u8g2.sendBuffer();                              
        }
        y = x;          
      }        

      while (state != 0){
        //Serial.println("SMO TUKAJ 4");
        u8g2.clearBuffer();          
        u8g2.setFont(u8g2_font_courR14_tf);
        u8g2.drawStr(16,12, "Siren is");
        u8g2.drawStr(16,32, "turnedOn");
        u8g2.sendBuffer();   
        digitalWrite(led_Alarm, HIGH);
        digitalWrite(siren, HIGH);        
        Event();
      }
      digitalWrite(led_Alarm, LOW);
      digitalWrite(siren, LOW);  
    }    
  }

  // ALARM SYSTEM IS OFF (alarmSystem = 0) 
  else {

    //Serial.print("Counter je: ");
    //Serial.println(counter);
    //Serial.println("SMO TUKAJ 0");
        
    u8g2.clearBuffer(); 
    u8g2.setFont(u8g2_font_courR10_tf);
    u8g2.drawStr(8,10, "Alarm is");
    u8g2.drawStr(8,24, "turnedOff");
    u8g2.setCursor(96,24);
    u8g2.print(alarmSystem);
    u8g2.sendBuffer();    
    Event(); 
  }
}


//////// ******* Methods ******* ////////  

void PreverjanjeGesla(){

  u8g2.clearBuffer();

  if(!strcmp(Data, Master)){ // if password is correct, e je geslo pravilno 

    tiempo2 = millis();
    unsigned long i = 0;

    if (alarmSystem == 0){
    i = 30000; 	// by default here is 30 seconds time to move out from PIR sensor, 
				// po defaultu bo tukaj 30 sekund, da se lahko v miru umakne, ko bo alarmni sistem vkljuen 
    }
    else{
    i = 5000;
    }
    tiempo2 = tiempo2 + i;
    int x = 0;
    int y = 0;

    while (tiempo1 < tiempo2) 	//	loop for countdown of 10 seconds if password was typed correctly, 
								// 	zanka za odtevanje 10 sekund e je geslo PRAVILNO
    {      
      digitalWrite(signalPin, HIGH);

      tiempo1 = millis();
      x = (tiempo2 - tiempo1) / 1000;

      // if is only for output on display
	  // if je samo za izpis na zaslon!
      if ( y != x){
        u8g2.clearBuffer();          
        u8g2.setFont(u8g2_font_courR14_tf);
        u8g2.drawStr(16,12, "Countdown: ");
        u8g2.setCursor(40,32);
        u8g2.print(x);
        u8g2.sendBuffer();                              
      }
      y = x;          
    }
    
    digitalWrite(signalPin, LOW);
    u8g2.clearBuffer();
    
    if (alarmSystem == 1){
      alarmSystem = 0; // Alarm turns off
      state = 0;
    }
    else{
      alarmSystem = 1; // Alarm turns on
      state = 0;     
    }    
  }
  
  else{
    while ( ledHigh < 5 ){
      Serial.println("Password is incorect!");
      u8g2.setFont(u8g2_font_courR10_tf);
      u8g2.drawStr(24,14, "False");
      u8g2.drawStr(24,30, "Password!");
      u8g2.sendBuffer();
      digitalWrite(signalPin, HIGH);
      delay(500);
      digitalWrite(signalPin, LOW);
      delay(500);
      ledHigh++;
    }
    counter = 0;
    ledHigh = 0;
  }
  
  u8g2.clearBuffer();
  clearData();  
}

void Event(){
  customKey = customKeypad.getKey();
  if (customKey == '*'){
    u8g2.clearBuffer(); 
    u8g2.setFont(u8g2_font_courR10_tf);
    u8g2.drawStr(8,10, "*pressed" );    
    u8g2.drawStr(8,22, "Typepassword!");
    u8g2.sendBuffer(); 
    counter = 1;
    tiempo2 = millis();

    unsigned long i = 10000; 
    tiempo2 = tiempo2 + i; 
  }

  while (counter == 1){    
    tiempo1 = millis();
    customKey = customKeypad.getKey(); 
    if (customKey){
      Data[data_count] = customKey; 
      data_count++; 
    }
    if(tiempo1 > tiempo2) {
    // A sentence where we check whether we have entered the password within 10 seconds after pressing the * key. 
	// If the password has not been entered within 10 seconds, the counter is reset to 0.
    // This means that the alarm system is switched off.      
      counter = 0;
      Serial.print("Counter:");
      Serial.println(counter);
      Serial.println("tiempo1:");
      Serial.println(tiempo1);
      Serial.println("tiempo2:");
      Serial.println(tiempo2);           
    }
    if(data_count == Password_Length-1){PreverjanjeGesla();}
  }
}

void Event2(){

  u8g2.clearBuffer(); 
  u8g2.setFont(u8g2_font_courR10_tf);
  u8g2.drawStr(8,10, "*pressed");    
  u8g2.drawStr(8,22, "Typepassword!");
  u8g2.sendBuffer(); 
  counter = 1;
  tiempo2 = millis(); // in the variable we get the current time tiempo2

  unsigned long i = 10000; 
  tiempo2 = tiempo2 + i; // we add 10s to the tiempo, this time is later compared to tiempo1, whose time increases in the if statement (tiempo1> tiempo2)

  while (counter == 1){    
    tiempo1 = millis();
    customKey = customKeypad.getKey(); // When the * key has already been pressed, the following key presses up to length of six are recorded in Data.
    if (customKey){
      Data[data_count] = customKey; 
      data_count++; 
    }
    if(tiempo1 > tiempo2) {
    // A sentence where we check whether we have entered the password within 10 seconds after pressing the * key. If the password has not been entered within 10 seconds, the counter is reset to 0.
    // This means that the alarm system is switched off.  
      counter = 0;
      u8g2.clearBuffer(); 
      u8g2.setFont(u8g2_font_courR10_tf);
      u8g2.drawStr(2,10, "Passwordwasnot");    
      u8g2.drawStr(2,22, "typed in a timely manner");
      u8g2.sendBuffer();
      delay(3000);                      
    }
    if(data_count == Password_Length-1){PreverjanjeGesla();}
  }
} 

void clearData()
{
  while(data_count != 0)
  {
    Data[data_count--] = 0;
  }
  return;
}

Credits

deadmouse93
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.