Hi, everyone I am new to Arduino and programing. I am working on a small project. I am making small security system consistPreformátovaný text
of arduino mega 2560, lcd display, keypad, pir and door sensor, and some LEDs and buzzer. Everything works quite fine but I have one problem.
I would like to enter password to disable the system after motion detection while countdown before buzzer occur, but I dont know how to code it. Is there anybody who could help me figure it out?
Here is my code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
int PIR = 10;
int MagKont = 24;
int buzzer = 12;
int ledR = 22;
int ledG = 23;
const byte ROWS = 4;
const byte COLS = 4;
char keypressed;
char keyMap[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad myKeypad = Keypad(makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
const int en = 2, rw = 1, rs = 0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3;
const int i2c_addr = 0x27;
LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);
String password = "2580";
String tempPassword;
int screenOffMsg =0;
boolean activated = false;
boolean activateAlarm = false;
boolean alarmActivated = false;
boolean enteredPassword;
void setup() {
lcd.begin(16, 2);
pinMode(PIR, INPUT);
pinMode(MagKont, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
}
void loop() {
if (activateAlarm) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm bude");
lcd.setCursor(0,1);
lcd.print("aktivovany za:");
int countdown = 9;
while (countdown !=0) {
lcd.setCursor(15,1);
lcd.print(countdown);
countdown--;
tone(buzzer, 700, 100);
delay(1000);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm aktivovany");
lcd.setCursor(7,1);
lcd.print("!!!!");
digitalWrite(ledG, HIGH);
activateAlarm = false;
alarmActivated = true;
}
if (alarmActivated == true) {
int pir = digitalRead(PIR);
int doorSensor = digitalRead(MagKont);
if (pir == HIGH) {
lcd.clear();
enterPassword();
}
else if (doorSensor == HIGH) {
lcd.clear();
enterPassword();
}
}
if (!alarmActivated) {
if (screenOffMsg == 0) {
lcd.clear();
lcd.setCursor(5,0);
lcd.print("Vitajte!");
lcd.setCursor(0,1);
lcd.print("Pre aktivaciu= A");
screenOffMsg = 1;
}
keypressed = myKeypad.getKey();
if (keypressed == 'A') {
tone(buzzer, 1000, 200);
activateAlarm = true;
}
}
}
void enterPassword() {
int k=10;
tempPassword = "";
activated = true;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm za:");
lcd.setCursor(0,1);
lcd.print("Zadaj pin:");
int countdown = 9;
while (countdown !=0) {
lcd.setCursor(15,0);
lcd.print(countdown);
countdown--;
tone(buzzer, 700, 100);
delay(1000);
}
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Alarm!");
lcd.setCursor(0,1);
lcd.print("Zadaj pin:");
digitalWrite(buzzer, HIGH);
digitalWrite(ledG, LOW);
digitalWrite(ledR, HIGH);
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 > 14 || keypressed == '#') {
tempPassword = "";
k=10;
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Alarm!");
lcd.setCursor(0,1);
lcd.print("Zadaj pin:");
}
if (keypressed == '*') {
if (tempPassword == password) {
activated = false;
alarmActivated = false;
digitalWrite(ledR, LOW);
digitalWrite(buzzer, LOW);
digitalWrite(ledG, HIGH);
screenOffMsg = 0;
}
else if (tempPassword != password) {
lcd.setCursor(0,0);
lcd.print("Zly pin!");
lcd.setCursor(0,1);
lcd.print("Zadaj znova!");
delay(2000);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Alarm!");
lcd.setCursor(0,1);
lcd.print("Zadaj pin:");
}
}
}
}