Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The LCD circuit:
- * LCD pin 1 to ground
- * LCD pin 2 to 5V
- * LCD RS pin 4 to digital pin 12
- * LCD R/W pin 5 to ground
- * LCD Enable pin 6 to digital pin 11
- * LCD D4 pin 11 to digital pin 5
- * LCD D5 pin 12 to digital pin 4
- * LCD D6 pin 13 to digital pin 3
- * LCD D7 pin 14 to digital pin 2
- * LCD pin 15 to 5v with 220ohm resistor
- * LCD pin 16 to ground
- *
- * 10K pot:
- * one end to +5V and the other to ground
- * wiper (middle pin) to LCD pin 3
- */
- //include the password library:
- #include <Password.h>
- // include the library code:
- #include <LiquidCrystal.h>
- // include the keypad code:
- #include <Keypad.h>
- //click down arrow icon (top right) > New Tab
- //paste in code from bottom of: http://arduino.cc/en/Tutorial/Tone
- #include "pitches.h"
- // state the password:
- Password password = Password( "3570" );
- const byte ROWS = 4; //four rows
- const byte COLS = 3; //three columns
- char keys[ROWS][COLS] = {
- {'1','2','3'},
- {'4','5','6'},
- {'7','8','9'},
- {'*','0','#'}
- };
- // keypad1 > pin13
- // keypad2 > pin10
- // keypad3 > pin9
- // keypad4 > pin8
- // keypad5 > pin7
- // keypad6 > pin6
- // keypad7 > pin0
- byte rowPins[ROWS] = {10, 0, 6, 8}; //connect to the row pinouts of the keypad
- byte colPins[COLS] = {9, 13, 7}; //connect to the column pinouts of the keypad
- Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- const int armedLed = A0; //red led pin
- int armed = 0; //system armed/not
- const int laserSense = A1; // the LDR
- int sensorHit = 0; // how bright laser is on LDR
- const int buzzer = A2; // buzzer is on pin A2
- int notes[]={ // possible notes to play
- NOTE_A4, NOTE_B4, NOTE_C3 };
- void setup() {
- Serial.begin (9600);
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- keypad.addEventListener(keypadEvent); //add an event listener for this keypad
- keypad.setDebounceTime(100); //less bounce is more responsive
- pinMode (armedLed, OUTPUT); //red led
- pinMode (laserSense, INPUT); //the LDR
- pinMode (buzzer, OUTPUT); //the buzzer
- analogWrite(armedLed, 0); //start as off
- // Print a message to the LCD to start things off:
- lcd.print("Code to arm:");
- }
- void loop() {
- // set the cursor to column 0, line 1
- // line 0 is the first row, line 1 is the second row
- lcd.setCursor(0, 1);
- sensorHit=analogRead(laserSense);
- Serial.println (sensorHit); // <700 no laser
- if (sensorHit < 700 && armed == 1){ // only beep when armed and beam broken
- tone(buzzer,notes[3],200); //beam broken play note 3
- }
- keypad.getKey(); // take keypad input
- }
- //
- //take care of some special events
- void keypadEvent(KeypadEvent eKey){
- switch (keypad.getState()){
- case PRESSED:
- lcd.print(eKey); // print key to screen
- switch (eKey){
- case '#': guessPassword(); break; // # is the 'enter' key
- default:
- password.append(eKey);
- }
- }
- }
- void guessPassword(){
- if (password.evaluate()){ //if code is correct:
- lcd.clear(); //clear LCD
- lcd.print("VALID PASSWORD"); //print message
- password.reset(); //resets password after correct entry
- delay(1500); //wait...
- lcd.clear(); //clear
- if (armed==0){ //if system is off (ie: disarmed)
- lcd.print("ARMED!"); //display message
- analogWrite(armedLed, 255); //led on
- armed=1; //system armed
- delay(2000); //wait
- lcd.clear(); //clear
- lcd.setCursor(0, 0); //return to top left of LCD
- lcd.print("Code to disarm:"); //back to where we began
- }
- else{
- lcd.print("DISARMED!"); //display message
- analogWrite(armedLed, 0); //led off
- armed=0; //system off
- delay(2000); //wait
- lcd.clear(); //clear
- lcd.setCursor(0, 0); //return to top left of LCD
- lcd.print("Code to arm:"); //back to where we began
- }
- }
- else{ //if password is incorrect:
- lcd.clear();
- lcd.print("INVALID PASSWORD");
- password.reset(); //resets password after INCORRECT entry
- tone(buzzer,notes[1],200); //boop! wrong code.
- delay(2000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Retry Code:");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement