#include <LCD_I2C.h>
#include <Keypad.h>
int led_alarm = 10;
int trigPin = 11;
int echoPin = 12;
int code = 1234;
int sensitivity = 10;
int mode = 0;
int alarmTrigs = 0;
int badPins = 0;
int pinlength = 0;
int temp;
long czas;
const byte ROWS = 4;
const byte COLS = 4;
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};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LCD_I2C lcd(0x27, 16, 2);
uint8_t padlockClosed[8] =
{
0b01110,
0b10001,
0b10001,
0b10001,
0b11111,
0b11111,
0b11111,
0b00000
};
uint8_t padlockOpen[8] =
{
0b01110,
0b10001,
0b10000,
0b10000,
0b11111,
0b11111,
0b11111,
0b00000
};
uint8_t settings[8] =
{
0b00000,
0b10101,
0b01110,
0b11011,
0b01110,
0b10101,
0b00000,
0b00000
};
uint8_t bell[8] =
{
0b00100,
0b01010,
0b01010,
0b10001,
0b10001,
0b11111,
0b00100,
0b00000
};
uint8_t key[8] =
{
0b01110,
0b01010,
0b01110,
0b00100,
0b00100,
0b00110,
0b00100,
0b00110
};
uint8_t excl[8] =
{
0b11111,
0b11111,
0b01110,
0b01110,
0b00100,
0b00100,
0b00000,
0b00100
};
uint8_t arrow[8] =
{
0b00000,
0b00100,
0b00110,
0b11111,
0b00110,
0b00100,
0b00000,
0b00000
};
uint8_t info[8] =
{
0b00000,
0b01110,
0b11011,
0b11111,
0b11011,
0b11011,
0b01110,
0b00000
};
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led_alarm, OUTPUT);
lcd.begin();
lcd.backlight();
lcd.createChar(0, padlockClosed);
lcd.createChar(1, padlockOpen);
lcd.createChar(2, settings);
lcd.createChar(3, bell);
lcd.createChar(4, key);
lcd.createChar(5, excl);
lcd.createChar(6, arrow);
lcd.createChar(7, info);
idle();
}
void idle() {
mode = 0;
digitalWrite(led_alarm, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(5);
lcd.print(" SYSTEM ALARMU");
lcd.setCursor(0, 1);
lcd.print("A");
lcd.write(6);
lcd.write(1);
lcd.print(" B");
lcd.write(6);
lcd.write(7);
}
void disarmed() {
mode = 2;
lcd.clear();
lcd.print("Alarm rozbrojony");
lcd.setCursor(0, 1);
lcd.print("A");
lcd.write(6);
lcd.write(0);
lcd.print(" B");
lcd.write(6);
lcd.write(4);
lcd.print(" C");
lcd.write(6);
lcd.write(2);
lcd.print(" D");
lcd.write(6);
lcd.write(7);
digitalWrite(led_alarm, HIGH);
}
void pin() {
mode = 1;
lcd.clear();
char customKey = customKeypad.getKey();
lcd.setCursor(0, 0);
lcd.print("Podaj PIN:");
lcd.setCursor(0, 1);
}
void pinEntered() {
lcd.clear();
lcd.setCursor(0, 0);
if(temp == code) {
disarm();
} else {
badPins++;
lcd.print("Bledny pin!");
for(int i=0; i<20; i++) {
digitalWrite(led_alarm, HIGH);
delay(200);
digitalWrite(led_alarm, LOW);
delay(200);
}
idle();
}
temp = 0;
}
void alarm() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarm!!!!!");
alarmTrigs++;
for(int i=0; i<20; i++) {
digitalWrite(led_alarm, HIGH);
delay(200);
digitalWrite(led_alarm, LOW);
delay(200);
}
idle();
}
void about() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Projekt w ramach");
lcd.setCursor(0, 1);
lcd.print("przedmiotu SW");
delay(2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Autorzy: Michal");
lcd.setCursor(0, 1);
lcd.print("Pasierbski");
delay(2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("oraz Natalia");
lcd.setCursor(0, 1);
lcd.print("Matyszczyk");
delay(2500);
idle();
}
void arm() {
alarmTrigs = 0;
badPins = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Aktywowano alarm");
lcd.setCursor(0, 1);
lcd.write(1);
for(int i=3; i>=0; i--) {
delay(1000);
lcd.setCursor(2, 1);
lcd.print(i);
}
lcd.setCursor(0, 1);
lcd.write(0);
digitalWrite(led_alarm, LOW);
delay(1000);
idle();
}
void disarm() {
lcd.clear();
digitalWrite(led_alarm, HIGH);
lcd.setCursor(0, 0);
lcd.write(7);
lcd.print(" Rozbrojono");
delay(2500);
disarmed();
}
void newPin() {
mode = 3;
lcd.clear();
char customKey = customKeypad.getKey();
lcd.setCursor(0, 0);
lcd.print("Podaj nowy PIN:");
lcd.setCursor(0, 1);
}
void pinSet() {
code = temp;
temp = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(7);
lcd.print(" Zmieniono PIN");
delay(2000);
disarmed();
}
void settingsMode() {
mode = 4;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ustaw czulosc cm");
lcd.setCursor(0, 1);
lcd.print("[005-390]: ");
}
void stats() {
lcd.clear();
lcd.setCursor(0, 0);
char text[50] = " Statystyki od ostatniego rozbrojenia: ";
lcd.setCursor(0, 1);
lcd.print("Wywolan: ");
lcd.print(alarmTrigs);
for(int i=0; i<25; i++) {
lcd.setCursor(0, 0);
for(int j=0; j<16; j++) {
lcd.print(text[j+i]);
}
delay(300);
if(i == 13) {
lcd.setCursor(0, 1);
lcd.print("Blednych PIN: ");
lcd.print(badPins);
}
}
disarmed();
}
void sensitivitySet() {
if(temp < 5 || temp > 390) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(7);
lcd.print(" Nieprawidlowa");
lcd.setCursor(2, 1);
lcd.print("wartosc");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Podaj wartosc z");
lcd.setCursor(0, 1);
lcd.print("zakresu 005-390");
delay(2000);
temp = 0;
settingsMode();
} else {
sensitivity = temp;
temp = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(7);
lcd.print(" Ustawiono");
lcd.setCursor(2, 1);
lcd.print("czulosc alarmu");
delay(2000);
disarmed();
}
}
void loop()
{
char customKey = customKeypad.getKey();
if (customKey){
if((mode == 1 || mode == 3 || mode == 4) && (customKey >= '0' && customKey <= '9')) {
int requiredLength = 4;
if (mode == 4) {
requiredLength = 3;
}
lcd.print(customKey);
temp = temp * 10 + ((int)customKey - '0');
pinlength++;
if (pinlength == requiredLength) {
delay(500);
pinlength = 0;
if (mode == 1) pinEntered();
if (mode == 3) pinSet();
if (mode == 4) sensitivitySet();
}
}
if(mode == 0) {
if(customKey == 'A') pin();
if(customKey == 'B') about();
}
if(mode == 2) {
if(customKey == 'A') arm();
if(customKey == 'B') newPin();
if(customKey == 'C') settingsMode();
if(customKey == 'D') stats();
}
}
if (mode <= 1) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
czas = pulseIn(echoPin, HIGH);
if (czas < (sensitivity * 60)) {
alarm();
}
}
}