Hi everyone.
I am new at the forum, but these days i spend great time with arduino. It is very interesting to me, so i hope that will be active and contribute to the community.
Now the "Problem" that i have.
I am willing to make arduino home security alarm with door sensor, keypad and display.
I have managed to connect all of these items to the ardunio board and (long story short) i have made THIS project. I have made some changes to the code to work with the I2C display instead the regular one (big thing for me as a beginner, and it was successfull
). It is working great with the buzzer alarming if the intruder gets into the house and don't enter the password, BUT i am willing to insert big 12v siren trough relay.
I have experience with connecting electronics but minimal with programming. So, for example, i would like to add relay on pin xx that will be activated at the "Alarmfunction" but i don't know how to do it.
Here is the code that is working, and needs to be "upgraded" with the relay option.
Can you help?
Since there is limitation on the length of the post, i will split the code into several posts
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <NewPing.h>
#include "NewTone.h"
/*-----------------------KEYPAD-----------------------*/
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
char keypressed;
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9, 8, 7, 6};//Rows 0 to 3
byte colPins[numCols] = {5, 4, 3, 2};//Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
/*--------------------------CONSTANTS-------------------------*/
const int buzzer = A1; //Buzzer/small speaker
const int doorMagSen = 10; //Door magnetic sensor
const int windowMagSen = 11; //Window magnetic sensors
LiquidCrystal_I2C lcd(0x27, 16, 2);
NewPing sonar(A0,A0,2000); // Trig Echo Max distance
/*--------------------------VARIABLES------------------------*/
String password="2580"; //Variable to store the current password
String tempPassword=""; //Variable to store the input password
int doublecheck;
boolean armed = false; //Variable for system state (armed:true / unarmed:false)
boolean input_pass; //Variable for input password (correct:true / wrong:false)
boolean storedPassword = true;
boolean changedPassword = false;
boolean checkPassword = false;
int distance;
int i = 1; //variable to index an array
/**********************************************************************************/
void setup() {
lcd.init();
lcd.backlight();
pinMode(doorMagSen,INPUT_PULLUP); //Set all magnetic sensors as input withn internal pullup resistor
pinMode(windowMagSen,INPUT_PULLUP);
}
void loop() { //Main loop
if (armed){
systemIsArmed(); //Run function to activate the system
}
else if (!armed){
systemIsUnarmed(); //Run fuction to de activate the system
}
}
/********************************FUNCTIONS************************************/
//While system is unarmed
void systemIsUnarmed(){
int screenMsg=0;
lcd.clear(); //Clear lcd
unsigned long previousMillis = 0; //To make a delay by using millis() function
const long interval = 5000; //delay will be 5 sec.
//every "page"-msg of lcd will change every 5 sec
while(!armed){ //While system is unarmed do...
unsigned long currentMillis = millis(); //Store the current run-time of the system (millis function)
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if(screenMsg==0){ //First page-message of lcd
lcd.setCursor(0,0);
lcd.print("SYSTEM ALARM OFF");
lcd.setCursor(0,1);
lcd.print("----------------");
screenMsg=1;
}
else{ //Second page-message of lcd
lcd.setCursor(0,0);
lcd.print("A to arm ");
lcd.setCursor(0,1);
lcd.print("B to change pass");
screenMsg=0;
}
}
keypressed = myKeypad.getKey(); //Read the pressed button
if (keypressed =='A'){ //If A is pressed, activate the system
NewTone(buzzer,500,200);
systemIsArmed(); //by calling the systemIsArmed function
}
else if (keypressed =='B'){//If B is pressed, change current password
doublecheck=0;
NewTone(buzzer,500,200);
storedPassword=false;
if(!changedPassword){ //By calling the changePassword function
changePassword();
}
}
}
}
