Arduino安全和警报系统项目
在这个项目中,我们将学习如何制作 Arduino 安全和警报系统。您可以观看以下视频或阅读下面的书面教程。
按下 A 按钮 10 秒后,警报启动。为了检测物体,它使用超声波传感器,一旦警报检测到某物,蜂鸣器就会开始发出声音。为了停止警报,我们需要输入一个 4 位数的密码。预设密码是1234,但我们也可以更改。
通过按下 B 按钮,我们进入密码更改菜单,首先我们需要输入当前密码才能继续,然后输入新的 4 位数密码。一旦密码更改,下次我们启动警报时,我们将只能通过输入新密码来停止警报。如果我们输入了错误的密码,我们将收到一条消息,我们需要重试。
现在让我们看看这个项目所需的组件。显然,我们需要一个 Arduino 板、一个超声波传感器、一个 LCD 显示器、一个蜂鸣器和一个 4×4 键盘。
您可以从以下链接获取此 Arduino 项目所需的组件:
这是电路原理图。
因此,对于蜂鸣器,我们只需要一个引脚,但需要一个支持 PWM 的引脚。 4×4 键盘有 8 个引脚,其中 4 个用于行,4 个用于键盘的列。每个按钮实际上是一个按钮开关,按下时会在一行和一列之间短路。
因此,例如,如果我们将第 1 行线设置为低,而将所有列线设置为高,当我们按下例如按钮 3 时,由于两行之间的短路,第 3 列线将下降到低所以在这种情况下,我们可以注册按钮 3 已被按下。
至于本项目中的其他两个组件,超声波传感器和液晶显示器,您可以查看我之前的详细教程,了解如何连接和使用它们。
接下来让我们看看 Arduino 代码。由于代码有点长,为了更好的理解,我将把程序的源代码分节贴出来,每个节都有说明。在本文的最后,我会发布完整的源代码。
因此,我们需要包含 LCD 的标准 LiquidCrystal 库和需要额外安装的 Keypad 库。然后我们需要定义蜂鸣器和超声波传感器的引脚,定义程序需要的一些变量,定义键盘的按键,以及创建键盘和LCD这两个对象。
在设置部分,我们只需要初始化 LCD 并定义蜂鸣器和超声波传感器的引脚模式即可。
在循环部分,首先我们检查警报是否被激活。因此,如果警报未激活,在 LCD 上我们将看到程序的主屏幕,其中提供两个选项,A 用于激活警报,B 用于更改密码。然后使用 myKeypad.getKey() 函数读取键盘上的哪个按钮被按下,如果是按钮 A,蜂鸣器将产生 200 毫秒的声音,并且 activateAlarm 变量将变为 true。
在这种情况下,我们将在 LCD 上打印消息“警报将被激活”,并使用 while 循环在警报激活之前进行 9 秒的倒计时。然后会出现“Alarm Activated”消息,我们将测量从我们的警报设备到它对面的物体的初始距离。
所以下一步就是超声波传感器会不断检查当前测量的距离是否小于初始距离,修正10cm的值,如果是,则说明传感器前面出现了物体并报警将被激活。 tune() 函数将激活蜂鸣器并调用 enterPassword() 自定义函数。
此自定义功能将打印一条消息,表明警报已激活,我们需要输入密码才能停止警报。因此,使用下一个 while 循环,我们不断检查是否按下了键盘上的按钮,并且每次按下按钮都会添加到 tempPassword 变量中。如果我们输入超过 4 位数字或按下锐键,之前输入的数字将被清除,以便我们从头开始重新输入。
另一方面,如果我们按下星号按钮,我们将检查当前输入的密码是否与最初设置的密码相同。如果这是真的,警报将被停用,蜂鸣器将停止发出声音,我们将返回主屏幕。但是,如果我们输入的密码错误,则会出现“错误!再试一次!”将出现,我们将不得不再次尝试输入正确的密码。
对于更改密码,我们使用类似的方法。这里首先我们需要输入当前密码才能设置新密码。
为了完成这个项目,我使用了一个塑料电箱,我在其中安装了所有组件并将它们联系在一起。
就是这样,我希望你喜欢这个项目,并随时在下面的评论部分提出任何问题。概览
必需组件
Arduino 报警系统电路原理图
Arduino报警系统源码
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#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
}
Code language: Arduino (arduino)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;
}
Code language: Arduino (arduino)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--;
tone(buzzer, 700, 100);
delay(1000);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm Activated!");
initialDistance = getDistance();
activateAlarm = false;
alarmActivated = true;
}
Code language: Arduino (arduino)if (alarmActivated == true){
currentDistance = getDistance() + 10;
if ( currentDistance < initialDistance) {
tone(buzzer, 1000); // Send 1KHz sound signal
lcd.clear();
enterPassword();
}
}
Code language: Arduino (arduino)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>");
}
}
}
}
Code language: Arduino (arduino)else if (keypressed =='B') {
lcd.clear();
int i=1;
tone(buzzer, 2000, 100);
tempPassword = "";
lcd.setCursor(0,0);
lcd.print("Current Password");
lcd.setCursor(0,1);
lcd.print(">");
passChangeMode = true;
passChanged = true;
while(passChanged) {
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(i,1);
lcd.print("*");
i++;
tone(buzzer, 2000, 100);
}
}
if (i > 5 || keypressed == '#') {
tempPassword = "";
i=1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Current Password");
lcd.setCursor(0,1);
lcd.print(">");
}
if ( keypressed == '*') {
i=1;
tone(buzzer, 2000, 100);
if (password == tempPassword) {
tempPassword="";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set New Password");
lcd.setCursor(0,1);
lcd.print(">");
while(passChangeMode) {
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(i,1);
lcd.print("*");
i++;
tone(buzzer, 2000, 100);
}
}
if (i > 5 || keypressed == '#') {
tempPassword = "";
i=1;
tone(buzzer, 2000, 100);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set New Password");
lcd.setCursor(0,1);
lcd.print(">");
}
if ( keypressed == '*') {
i=1;
tone(buzzer, 2000, 100);
password = tempPassword;
passChangeMode = false;
passChanged = false;
screenOffMsg = 0;
}
}
}
}
}
}
Code language: Arduino (arduino)这里是Arduino报警系统的完整源代码:
/*
* Arduino Security and Alarm System
*
* by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#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
}
void loop() {
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--;
tone(buzzer, 700, 100);
delay(1000);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm Activated!");
initialDistance = getDistance();
activateAlarm = false;
alarmActivated = true;
}
if (alarmActivated == true){
currentDistance = getDistance() + 10;
if ( currentDistance < initialDistance) {
tone(buzzer, 1000); // Send 1KHz sound signal
lcd.clear();
enterPassword();
}
}
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;
}
else if (keypressed =='B') {
lcd.clear();
int i=1;
tone(buzzer, 2000, 100);
tempPassword = "";
lcd.setCursor(0,0);
lcd.print("Current Password");
lcd.setCursor(0,1);
lcd.print(">");
passChangeMode = true;
passChanged = true;
while(passChanged) {
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(i,1);
lcd.print("*");
i++;
tone(buzzer, 2000, 100);
}
}
if (i > 5 || keypressed == '#') {
tempPassword = "";
i=1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Current Password");
lcd.setCursor(0,1);
lcd.print(">");
}
if ( keypressed == '*') {
i=1;
tone(buzzer, 2000, 100);
if (password == tempPassword) {
tempPassword="";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set New Password");
lcd.setCursor(0,1);
lcd.print(">");
while(passChangeMode) {
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(i,1);
lcd.print("*");
i++;
tone(buzzer, 2000, 100);
}
}
if (i > 5 || keypressed == '#') {
tempPassword = "";
i=1;
tone(buzzer, 2000, 100);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set New Password");
lcd.setCursor(0,1);
lcd.print(">");
}
if ( keypressed == '*') {
i=1;
tone(buzzer, 2000, 100);
password = tempPassword;
passChangeMode = false;
passChanged = false;
screenOffMsg = 0;
}
}
}
}
}
}
}
}
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>");
}
}
}
}
// Custom function for the Ultrasonic sensor
long getDistance(){
//int i=10;
//while( i<=10 ) {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration*0.034/2;
//sumDistance += distance;
//}
//int averageDistance= sumDistance/10;
return distance;
}
Code language: Arduino (arduino)最终触摸
制造工艺