This is the code I am using for my security system. It might not be pretty but it works and I'm a beginner. I somehow have to implement a code to use the ethernet with what I already have.
/*
|| Simple Password Entry Using Matrix Keypad
|| 4/5/2012 Updates Nathan Sobieck: Nathan@Sobisource.co
|| Burlar Alarm
*/
//* is to validate password
//# is to reset password attempt
/////////////////////////////////////////////////////////////////
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
#include <Timer.h>
Password password = Password( "1234" ); // my passcode
const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = { 2, 7, 6, 4 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 3, 1, 5 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int redPin = 9; // represents alarm being off (at home) LED
int bluePin = 15 ; // represents alarm being on (not home) LED
int checkRedLight = 8; // checks to see if alarm is off
int motionSensorsOn = 16; // Infared sensors turned on
int doorSensorsOn = 17; // Turns door sensors on
int checkDoorSensors = 18; // checks to see if door contacts have been broken or not
int checkMotionSensors = 14; // checks to see if the infared sensors have picked up any movement
int triggerAlarm = 19;// Sets off the burglar alarm
int iCanSeeYou = 0;
int val_2 = 0;
int val_3 = 0;
int val_4 = 0;
int val_5 = 0;
int i;
int armedCheck; // function used to monitor all sensors
int setAlarmOff; // function used to set burglar alarm off
int turnCamerasOn; // function to turn on cameras
Timer t;
void setup(){
pinMode (redPin, OUTPUT);
pinMode (bluePin, OUTPUT);
pinMode (motionSensorsOn, OUTPUT);
pinMode (doorSensorsOn, OUTPUT);
pinMode (triggerAlarm, OUTPUT);
pinMode (iCanSeeYou, OUTPUT);
pinMode (checkDoorSensors, INPUT);
pinMode (checkRedLight, INPUT);
pinMode (checkMotionSensors, INPUT);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
void loop(){
keypad.getKey();
t.update();
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
switch (eKey){
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
}
void checkPassword(){ // function to check the passcode and arm or disarm the system
if (password.evaluate()){
digitalWrite (bluePin, LOW);
digitalWrite (redPin, HIGH);
digitalWrite (motionSensorsOn, LOW);
digitalWrite (doorSensorsOn, LOW);
digitalWrite (triggerAlarm, LOW);
digitalWrite (iCanSeeYou, LOW);
t.stop (setAlarmOff);
t.stop (armedCheck);
} else{
digitalWrite (redPin, LOW);
for ( i = 0; i <= 60; i++) { // allows me 60 sec to get out of my house before all sensors are activated
digitalWrite (bluePin, HIGH);
delay (500);
digitalWrite (bluePin, LOW);
delay(500);
}
digitalWrite (bluePin, HIGH);
int check = t.after(1000, setAlarm); // jumps to setAlarm function after said seconds }
}
void setAlarm() { // function used to check if the system is armed and to begin the countdown to turn all sensors on
digitalWrite (motionSensorsOn, HIGH);
digitalWrite (doorSensorsOn, HIGH);
delay(10000);
armedCheck = t.every(1000, checkSensors);
} //jumps to checkSensors function every second
void checkSensors () { // function used to check all sensors every second
val_2 = digitalRead (checkDoorSensors);
val_4 = digitalRead (checkMotionSensors);
if (val_4 == HIGH || val_2 == HIGH) {
setAlarmOff = t.after(50000, alarmOn); // if any sensors have been triggered then it jumps to alarmOn function
turnCamerasOn = t.after (15000, camerasOn); // if any sensors have been triggered then it jumps to cameras on
t.stop (armedCheck);
}
}
void alarmOn() {
val_3 = digitalRead (checkRedLight);
if (val_3 == LOW) {
digitalWrite (triggerAlarm, HIGH);
int doIt = t.after (15000, youCaughtBitch); }
else { digitalWrite (triggerAlarm, LOW);
t.stop (armedCheck);
}
}
void camerasOn () {
val_5 = digitalRead (checkRedLight);
if (val_5 == LOW ) {
digitalWrite (iCanSeeYou, HIGH);
}
else { digitalWrite (iCanSeeYou, LOW);
t.stop (armedCheck);
}
}
void youCaughtBitch () {
digitalWrite (triggerAlarm, LOW);
}