Arduino Alarm System

Hello, in this application, we will make a Motion Sensor Alarm System application.

Necessary materials:

About this project

For this project, a motion detector alarm system based on an Arduino Uno was created. All of the components along with many more are available in the Complete Starter Kit for Arduino R3. The system has the following features:- PIR motion sensor HC-SR501 to detect movement in the proximity of the device.- LCD display to show alarm state and show input from the keypad.- Membrane switch keypad 4×4 to arm and disarm the system.- Alarm with audible alert from an active buzzer and visual alert via a red LED- System power indicator with a greed LED.

I undertook this project to gain more experience with coding more complex embedded projects that require the use of state machine programming. The keypad must be capable of sending input signals, the LCD screen must refresh and the LED indicator must switch off and on all without blocking. The usual hello world blinky LED code in this case is insufficient.

When the device is powered it is initialized in the disarmed state. An LCD with adjust brightness and contrast via a potary potentiometer indicates the state of the system and allows the user to see the input of the membrane keypad. On start up, the LCD indicates that to arm the system press the * key. When armed, the devise polls the output from the PIR sensor to detect movement. The alarm() function checks if the device is armed, when armed and when motion is detected the active buzzer is turned on and a red LED is lit. The handleBuzz() function acts to change the frequency of the buzzer which can be adjusted by changing the check value of the buzzCycleCounter. The buzzer volume is adjustable via a rotary potentiometer. To disable the system, a 4 digit key code is entered. The input is visible on the LCD. The key is hardcoded in a variable, but could be changed by the user in later enhancement iterations to the design. The # key is used to send the key for evaluation while the * key will reset the input. The design of the UI was modeled after keypad entry systems which generally do not have a way to view the code entered. The systems make the assumption that the user know to either choose the # button to signal the end the input and submit the code.

Here is the final bread board prototype:

There are few improvements on the todo list:- Use Eprom to allow user defined pass key. – add batter backup- Tune the PIR sensor to be most sensitive to movement in the field of view and distance from the sensor based on the location where the alarm will be installed. – Tune the time that the PIR sensor. Currently after movement is no longer detected there is a long delay before the PIR will alert again. This need to be reduced so that the alarm fires from the maximum amount of time possible while movement is detected.

//www.nedrinbarkinoren.com

/*
  LiquidCrystal Library - Hello World

  Demonstrates the use of a 16x2 LCD display.  The LiquidCrystal
  library works with all LCD displays that are compatible with the
  Hitachi HD44780 driver. There are many of them out there, and you
  can usually tell them by the 16-pin interface.

  This sketch prints "Hello World!" to the LCD
  and shows the time.

  The circuit:
   LCD RS pin to digital pin 7
   LCD Enable pin to digital pin 8
   LCD D4 pin to digital pin 9
   LCD D5 pin to digital pin 10
   LCD D6 pin to digital pin 11
   LCD D7 pin to digital pin 12
   LCD R/W pin to ground
   LCD VSS pin to ground
   LCD VCC pin to 5V
   10K resistor:
   ends to +5V and ground
   wiper to LCD VO pin (pin 3)

  Library originally added 18 Apr 2008
  by David A. Mellis
  Library modified 5 Jul 2009
  by Limor Fried (http://www.ladyada.net)
  Example added 9 Jul 2009
  by Tom Igoe
  Modified 22 Nov 2010
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/

#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
// LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
bool doInitilizeLCD = true;

// --KEY PAD START ----------------------------------------------------
/* @file CustomKeypad.pde
  || @version 1.0
  || @author Alexander Brevig
  || @contact alexanderbrevig@gmail.com
  ||
  || @description
  || | Demonstrates changing the keypad size and key values.
  || #
*/
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
char customKey;
char accessCode[] = "1111";
char keyPadInput[] = "zzzz";
unsigned char inputCounter = 0;
unsigned char inputMaxCount = 4;
// --KEY PAD END ------------------------------------------------------

int ledPin = 11;  // LED on Pin 11, lights up when PIR is active
int powerledPin = 13; // LED on Pin 13, indicates power on, always HIGH

int pirPin = 10; // Input for HC-S501
int pirValue; // Place to store read PIR Value

// --Active Buzzer START ----------------------------------------------
int buzzer = 12;//the pin of the active buzzer
boolean shouldBeAlerting = false;
// --Active Buzzer END ------------------------------------------------------

enum ArmedStates {ARMED, DISARMED};
ArmedStates armedState = DISARMED;
short armedTimerInterval = 5;
unsigned long armedTimer = millis();

bool debug = false;

void setup() {
  Serial.begin(9600);
  while (!Serial); // wait for Serial to connect
  Serial.println("ready");

  pinMode(buzzer, OUTPUT); // set up pin as output

  pinMode(ledPin, OUTPUT); // set up pin as output

  pinMode(powerledPin, OUTPUT); // set pin as output

  pinMode(pirPin, INPUT); // set pin as input

  lcd.begin(16, 2); // set up and register the LCD

}

void loop() {
  digitalWrite(powerledPin, HIGH);

  initilizeLCD();

  handleKeyPadInput();

  handlePIR();

  alert();

}

const char* getStateString(enum ArmedStates armedState) {
  switch (armedState)
  {
    case ARMED: return "ARMED";
    case DISARMED: return "DISARMED";
  }
}

//
void initilizeLCD() {
  if (doInitilizeLCD) {

    doInitilizeLCD = false;
    lcd.clear();
    String a = getStateString(armedState);

    if (debug) {
      Serial.println("armedState: " + a);
    }

    lcd.setCursor(0, 0);
    lcd.print(a);
    lcd.setCursor(0, 1);
    lcd.print("PRESS * to ARM.");
  }
}

void handleLCD(bool shouldClearLCD) {
  if (shouldClearLCD) {
    lcd.clear();
  }

  String a = getStateString(armedState);

  if (debug) {
    Serial.println("armedState: " + a);
  }

  lcd.setCursor(0, 0);
  lcd.print(a);
  lcd.setCursor(0, 1);


  lcd.print("DISARM KEY:");
  uint8_t cursorStart = 11;
  lcd.setCursor(cursorStart, 1);
  lcd.cursor();

  // overwrites the LCD screen positions with blank space or keypad input
  for (int i = 0; i < 4; i++) {
    if (keyPadInput[i] == 'z') {
      lcd.setCursor(cursorStart + i, 1);
      lcd.print(" ");
    } else {
      lcd.setCursor(cursorStart + i, 1);
      lcd.print(keyPadInput[i]);
    }
  }
}

void handleKeyPadInput() {
  customKey = customKeypad.getKey();
  if (customKey) {

    Serial.print("customKey: ");
    Serial.println(customKey);

    if (armedState == DISARMED ) {
      if (customKey == '*') {
        armedState = ARMED;
        handleLCD(true);
      }
    } else {
      if (customKey == '*') {
        resetCodeInput();
      } else if (customKey == '#') {
        if (strcmp(keyPadInput, accessCode) == 0 ) {
          Serial.println("Keypad input matches access code");
          // Disarm the system
          resetCodeInput();
          armedState = DISARMED;
          doInitilizeLCD = true;
          initilizeLCD();
        } else {
          resetCodeInput();
        }
      } else {
        if (inputCounter <= 3) {
          keyPadInput[inputCounter] = customKey;
          inputCounter++;
          handleLCD(true);
        } else {
          // do nothing
        }
      }
    }
  }
}


void resetCodeInput() {
  Serial.println("reseting keyPadInput");
  for (int i = 0; i < 4; i++) {
    keyPadInput[i] = 'z';
  }
  inputCounter = 0;
  handleLCD(true);
}

void handlePIR() {
  pirValue = digitalRead(pirPin);
}

void alert() {
  if (pirValue > 0 && armedState == ARMED ) {
    shouldBeAlerting = true;
  } else {
    shouldBeAlerting = false;
  }

  if (shouldBeAlerting) {
    Serial.println("shouldBeAlerting");
  }

  handleLed();

  handleBuzz();

}

void handleLed () {
  // handle the alarm LED
  if (shouldBeAlerting) {
    Serial.println("setting LED HIGH");
    digitalWrite(ledPin, HIGH);
  } else {
    // Serial.println("setting LED LOW");
    digitalWrite(ledPin, LOW);
  }
}

void handleBuzz() {
  if (shouldBeAlerting) {
    unsigned char i;
    for (i = 0; i < 10; i++)
    {
      digitalWrite(buzzer, HIGH);
      delay(1);//wait for 1ms
      digitalWrite(buzzer, LOW);
      delay(1);//wait for 1ms
    }
    //output another frequency
    for (i = 0; i < 20; i++)
    {
      digitalWrite(buzzer, HIGH);
      delay(2);//wait for 2ms
      digitalWrite(buzzer, LOW);
      delay(2);//wait for 2ms
    }
  }
}

See you at the next English training.

Nedrin Barkın ÖREN

Bir Cevap Yazın