hi,
I am working on a door security system.
I need help in combining keypad code and pir sensor code,what i want to do is that when the correct password is entered the servo will turn 179 to 0 and the sensor will be activated and will turn servo 179 to 0 at a certain time, then the password will be entered again.
here is the keypad code
#include <Keypad.h> //Library for the matrix keypad available from http://playground.arduino.cc/code/Keypad
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> //You must use F. Malpartida's new LCD library for Serial LCD https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
#include <Servo.h>
#include "RTClib.h"
#define I2C_ADDR 0x3F // Define I2C Address for the LCD. This varies by device, see the data sheet for yours
// Set up serial to parallel configuration for LCD
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
// define a servo instance
Servo myservo;
RTC_Millis rtc;
// define an LCD instance
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
// define keypad mapping
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap. Note the position of # and * alter between models
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 (top to bottom) to these Arduino pins.
byte rowPins[ROWS] = {
8, 7, 6, 9 };
// Connect keypad COL0, COL1 and COL2 (left to right) to these Arduino pins.
byte colPins[COLS] = {
5, 4, 3, 2 };
// Create the Keypad instance
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
pinMode(10, OUTPUT); // pin for buzzer output
pinMode(12, OUTPUT); // pin for green LED
pinMode(13, OUTPUT); // pin for red LED
myservo.attach(11); //Servo signal attached to pin 3
lcd.begin (20,4); // initialise LCD
myservo.write(179); // set servo position to one extent
rtc.begin(DateTime(__DATE__, __TIME__));
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
// clear the LCD
lcd.clear();
// move the cursor top left
lcd.home();
// print a message on the LCD to enter the code
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print(' ');
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.setCursor(0, 1);
lcd.print("Enter(9 digit) Pass:");
lcd.setCursor(0, 3);
lcd.print("Press(#)Cancel Pass");
// move the cursor to a blank line for code entry
lcd.setCursor(0, 2);
}
void loop(){
String mastercode = "123456"; // the password
String enteredcode = ""; // the code you enter
String reason = " INCORRECT CODE "; // the default reason why entry failed
// read a 6 digit code (you can have any length)
for (int i = 0; i < 6; i++){
char key = kpd.waitForKey();
if(key == '#'){ // # is used to cancel
i = 6; //skip to the end
reason = " Cancelled "; // set the other reason why entry failed. You pressed # to cancel if you pressed a wrong number
}
if(key != NO_KEY){ // if you aren't reading no keypress
lcd.print("*"); // print a # to the LCD so people don't see the number but you know you pressed a button
enteredcode += key; // append the keypress to the end of the entered code string
}
}
if (enteredcode == mastercode) // if the code is correct
{
lcd.setCursor (0, 2);
lcd.print(" ");
lcd.setCursor (0, 2);
lcd.print(" CODE CORRECT "); // print a success message
digitalWrite(12, HIGH); // turn on the green LED
myservo.write(0); // move the servo 180 degrees and open the door lock
// make 3 beeps
buzz(10, 2500, 100);
delay(200);
buzz(10, 2500, 100);
delay(200);
buzz(10, 2500, 100);
delay (3000); // wait 3 seconds while the door is opened
digitalWrite(12, LOW); // turn off the LED
myservo.write(179); // close the door lock
//reset the LCD for the next user
lcd.clear();
lcd.home();
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print(' ');
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.setCursor(0, 1);
lcd.print("Enter(9 digit) Pass:");
lcd.setCursor(0, 3);
lcd.print("Press(#)Cancel Pass");
lcd.setCursor(0, 2);
}
else { // if the code is wrong or cancelled
// tell the user what happened
lcd.setCursor (0, 2);
lcd.print(" ");
lcd.setCursor (0, 2);
lcd.print(reason); // display the reason for failure. Incorrect code by default unless cancel is pressed
digitalWrite(13, HIGH); // turn on the red LED
buzz(10, 2500, 500); // make a long fail beep
delay (3000); // wait 3 seconds before allowing a retry
digitalWrite(13, LOW); // turn off LED
// reset the LCD for the next try
lcd.clear();
lcd.home();
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print(' ');
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.setCursor(0, 1);
lcd.print("Enter(9 digit) Pass:");
lcd.setCursor(0, 3);
lcd.print("Press(#)Cancel Pass");
lcd.setCursor(0, 2);
}
}
// function for making beeps written by Rob Faludi - http://www.faludi.com/2007/04/23/buzzer-arduino-example-code/
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
}
here is the PIR sensor code
/*
Arduino with PIR motion sensor
For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
Modified by Rui Santos based on PIR sensor by Limor Fried
*/
int led = 13; // the pin that the LED is atteched to
int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
void setup() {
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(1000); // delay 100 milliseconds
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}