來做一個Arduino安全和警報系統

大家週末好呀,今天我們來做一個Arduino安全和警報系統,您可以運用到車庫或者簡易保險室等。話不多說,我們直接進入正題。

項目概述

這個系統我們設計的是,按下A按鈕後10秒內將激活警報。為了檢測物體,它使用了超聲波傳感器,一旦警報器檢測到某種東西,蜂鳴器就會開始發出聲音。為了停止警報,我們需要輸入4位數字的密碼。預設密碼是1234,但我們也可以更改它。

通過按B按鈕,我們進入密碼更改菜單,首先我們需要輸入當前密碼才能解除警報,然後輸入新的4位數字密碼。更改密碼後,下次啟動警報時,我們只能通過輸入新密碼來停止警報。如果輸入了錯誤的密碼,我們將收到一條消息,需要再次嘗試。

來做一個Arduino安全和警報系統

主要組件

現在,讓我們看看該項目所需的組件。顯然,我們需要一個Arduino開發板,一個超聲波傳感器,一個LCD顯示器,一個蜂鳴器和一個4×4鍵盤。

來做一個Arduino安全和警報系統

電路原理圖如下:

來做一個Arduino安全和警報系統

對於蜂鳴器,我們只需要一個引腳即可,但需要一個支持PWM的引腳。4×4鍵盤有8個引腳,其中4個用於行,而4個用於列。每個按鈕實際上是一個按鈕開關,按下該按鈕可使行和列之間短路。

來做一個Arduino安全和警報系統

因此,例如,如果我們將第1行的行設置為低,而將所有列的行設置為高,則當我們按下按鈕3時,由於兩行之間的短路,列3的行將降為低在這種情況下,我們可以註冊按鈕3已被按下。

Arduino報警系統代碼

接下來便是最重要Arduino代碼部分了。由於代碼較長,為了更好地理解,我們分開各節程序的源代碼,並對各個個部分加以說明說明。

因此,我們需要包括用於LCD的標準LiquidCrystal庫和需要額外安裝的鍵盤庫。然後,我們需要定義蜂鳴器和超聲波傳感器的引腳,定義程序所需的一些變量,定義鍵盤的鍵,以及為鍵盤和LCD創建兩個對象。

#include <liquidcrystal.h> // includes the LiquidCrystal Library/<liquidcrystal.h>

#include <keypad.h>

#define buzzer 8

#define trigPin 9

#define echoPin 10

long duration;

int distance, initialDistance, currentDistance, i;

int screenOffMsg =0;

String password="1234";

String tempPassword;

boolean activated = false; // State of the alarm boolean isActivated;

boolean activateAlarm = false;

boolean alarmActivated = false;

boolean enteredPassword; // State of the entered password to stop the alarm

boolean passChangeMode = false;

boolean passChanged = false;

const byte ROWS = 4; //four rows

const byte COLS = 4; //four columns

char keypressed;

//define the cymbols on the buttons of the keypads

char keyMap[ROWS][COLS] = {

{'1','2','3','A'},

{'4','5','6','B'},

{'7','8','9','C'},

{'*','0','#','D'}

};

byte rowPins[ROWS] = {14, 15, 16, 17}; //Row pinouts of the keypad

byte colPins[COLS] = {18, 19, 20, 21}; //Column pinouts of the keypad

Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);

LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

void setup() {

lcd.begin(16,2);

pinMode(buzzer, OUTPUT); // Set buzzer as an output

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

}

在設置部分,我們只需要初始化LCD並定義蜂鳴器和超聲傳感器的引腳模式。

在循環部分中,首先我們檢查警報是否已激活。因此,如果未激活警報,則LCD上將顯示程序的主屏幕,該程序提供兩個選項:A用於激活警報,B用於更改密碼。然後使用myKeypad.getKey()函數,讀取鍵盤上的哪個按鈕被按下,如果是按鈕A,則蜂鳴器將發出200毫秒的聲音,activateAlarm變量將變為true。

if (!alarmActivated) {

if (screenOffMsg == 0 ){

lcd.clear();

lcd.setCursor(0,0);

lcd.print("A - Activate");

lcd.setCursor(0,1);

lcd.print("B - Change Pass");

screenOffMsg = 1;

}keypressed = myKeypad.getKey();

if (keypressed =='A'){ //If A is pressed, activate the alarm

tone(buzzer, 1000, 200);

activateAlarm = true;

}

在這種情況下,我們將在LCD上顯示“將激活警報”消息,並使用while循環將在激活警報之前倒數9秒鐘。然後將顯示消息“已激活警報”,我們將測量從警報設備到與之相對的對象的初始距離。

if (activateAlarm) {

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Alarm will be");

lcd.setCursor(0,1);

lcd.print("activated in");

int countdown = 9; // 9 seconds count down before activating the alarm while (countdown != 0) {

lcd.setCursor(13,1);

lcd.print(countdown);

countdown--; one(buzzer, 700, 100); delay(1000);

}

lcd.clear(); lcd.setCursor(0,0);

lcd.print("Alarm Activated!");

initialDistance = getDistance(); activateAlarm = false;

alarmActivated = true;

}

因此,下一步是超聲波傳感器將不斷檢查當前測量的距離是否小於初始距離,並已校正了10 cms的值,如果是,這意味著在傳感器和警報器之前出現了一個物體將被激活。tune()函數將激活蜂鳴器,然後將調用enterPassword()自定義函數。

if (alarmActivated == true){

currentDistance = getDistance() + 10;

if ( currentDistance < initialDistance) {

tone(buzzer, 1000); // Send 1KHz sound signal lcd.clear();

enterPassword();

}

}

此自定義功能將打印一條消息,提示已激活警報,並且我們需要輸入密碼才能停止警報。因此,使用下一個while循環,我們將不斷檢查是否已按下鍵盤上的一個按鈕,並且每次按下按鈕都會添加到tempPassword變量中。如果我們輸入的數字多於4位或按快捷按鈕,則先前輸入的數字將被清除,因此我們可以從頭開始再次輸入。

void enterPassword() {

int k=5;

tempPassword = ""; activated = true; lcd.clear(); lcd.setCursor(0,0); lcd.print(" *** ALARM *** ");lcd.setCursor(0,1);

lcd.print("Pass>");while(activated) {

keypressed=myKeypad.getKey();

if (keypressed != NO_KEY){

if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||keypressed == '8' || keypressed == '9' ) {tempPassword += keypressed;

lcd.setCursor(k,1);lcd.print("*");

k++;

}

}

if (k > 9 || keypressed == '#') {

tempPassword = "";k=5; lcd.clear();lcd.setCursor(0,0);

lcd.print(" *** ALARM *** ");lcd.setCursor(0,1); lcd.print("Pass>");

}

if ( keypressed == '*') {

if ( tempPassword == password ) {

activated = false;alarmActivated = false;noTone(buzzer); screenOffMsg = 0;

}

else if (tempPassword != password) {

lcd.setCursor(0,1); lcd.print("Wrong! Try Again"); delay(2000);lcd.clear(); lcd.setCursor(0,0); lcd.print(" *** ALARM *** ");lcd.setCursor(0,1); lcd.print("Pass>");

}

}

}

}

另一方面,如果按星號按鈕,將檢查當前輸入的密碼是否與最初設置的密碼相同。如果是這樣,警報將被禁用,蜂鳴器將停止發出聲音,然後我們將返回主屏幕。但是,如果我們輸入的密碼錯誤,則會顯示“錯誤!再試一次!” 將會出現,我們將不得不再次嘗試輸入正確的密碼。

要更改密碼,我們使用類似的方法即可。

在編寫完代碼後,我們使用一個塑料電箱將組件都連接到一起即可。

來做一個Arduino安全和警報系統

更多詳細內容,可以觀看下一個視頻

關注風火輪,技術之路常相伴,我們下期見!


分享到:


相關文章: