#include <LiquidCrystal.h> //needed to control LCD displays
#include <Keypad.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
int buzzer_pin = 6;
int motion_InPin = 5;
int mot_state = LOW; //initialized to no motion detected
int val = 0; //variable for reading the pin status
const unsigned long timeDelay = 1000; //1000 for 1s
int alarmActive = 0; // suggestion: use bool instead of int
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[COLS][ROWS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 14, 15, 16, 17 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 4, 3, 2, 1 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void activate() {
alarmActive = 1;
// password.reset();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarm Activated in");
lcd.setCursor(0, 1);
lcd.print("10 seconds");
int countDown = 9;
//for (i == 0; i < 10; i++) {
while (countDown != 0) {
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.print(countDown);
countDown--;
delay(700); //timeDelay
}
if (countDown == 0) {
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.print(countDown);
alarm(true);
//delay(3000);
//alarm(false);
}
}
void alarm(bool alarmStatus) {
while(alarmStatus) {
digitalWrite(buzzer_pin, HIGH);
tone(6, 700, 200);
delay(350);
}
}
void setup() {
lcd.begin(16, 2);
pinMode(buzzer_pin, OUTPUT);
pinMode(motion_InPin, INPUT);
Serial.begin(9600);
}
char password = "5678";
char user_input[4];
int num_key = 0;
void loop() {
//check_password();
val = digitalRead(motion_InPin); // read input value
if (val == HIGH) { // check if the input is HIGH
//digitalWrite(ledPin, HIGH); // turn LED ON
if (mot_state == LOW) {
// we have just turned on
Serial.println("Motion detected!");
mot_state = HIGH;
activate();
}
} else {
//digitalWrite(ledPin, LOW); // turn LED OFF
if (mot_state == HIGH) {
// we have just turned off
Serial.println("Motion ended!");
mot_state = LOW;
}
}
}
void check_password() {
while (num_key < 4) {
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key);
user_input[num_key] = key;
num_key++;
}
}
Serial.println(user_input);
Serial.println(password);
if( password == user_input) {
Serial.println("CORRECT PASSWORD");
}
}
/*
int trigger_pin = 2;
int echo_pin = 3;
int buzzer_pin = 1;
int time;
int distance;
void setup() {
Serial.begin(9600);
pinMode(trigger_pin, OUTPUT);
pinMode(echo_pin, INPUT);
pinMode(buzzer_pin, OUTPUT);
}
void loop() {
digitalWrite(trigger_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trigger_pin, LOW);
time = pulseIn(echo_pin, HIGH);
distance = (time * 0.034) /2;
if (distance <= 100) {
Serial.println("Too close, alarm triggered ");
Serial.print(" Distance: ");
Serial.println(distance);
digitalWrite(buzzer_pin, HIGH);
tone(6, 700, 200);
delay(250);
digitalWrite(buzzer_pin, HIGH);
tone(6, 250, 200);
delay(250);
}
else {
Serial.println("Still quite far ");
Serial.print(" Distance: ");
Serial.println(distance);
digitalWrite(buzzer_pin, LOW);
delay(1000);
}
}
*/