Guest User

Alarm Pt.3

a guest
Apr 27th, 2014
663
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* The LCD circuit:
  2.  * LCD pin 1 to ground
  3.  * LCD pin 2 to 5V
  4.  * LCD RS pin 4 to digital pin 12  
  5.  * LCD R/W pin 5 to ground
  6.  * LCD Enable pin 6 to digital pin 11
  7.  * LCD D4 pin 11 to digital pin 5
  8.  * LCD D5 pin 12 to digital pin 4
  9.  * LCD D6 pin 13 to digital pin 3
  10.  * LCD D7 pin 14 to digital pin 2
  11.  * LCD pin 15 to 5v with 220ohm resistor
  12.  * LCD pin 16 to ground
  13.  *
  14.  * 10K pot:
  15.  *   one end to +5V and the other to ground
  16.  *   wiper (middle pin) to LCD pin 3
  17.  */
  18.  
  19. //include the password library:
  20. #include <Password.h>
  21.  
  22. // include the library code:
  23. #include <LiquidCrystal.h>
  24.  
  25. // include the keypad code:
  26. #include <Keypad.h>
  27.  
  28. // state the password:
  29. Password password = Password( "3570" );
  30.  
  31. const byte ROWS = 4; //four rows
  32. const byte COLS = 3; //three columns
  33. char keys[ROWS][COLS] = {
  34.   {'1','2','3'},
  35.   {'4','5','6'},
  36.   {'7','8','9'},
  37.   {'*','0','#'}
  38. };
  39. //  keypad1 > pin13
  40. //  keypad2 > pin10
  41. //  keypad3 > pin9
  42. //  keypad4 > pin8
  43. //  keypad5 > pin7
  44. //  keypad6 > pin6
  45. //  keypad7 > pin0
  46. byte rowPins[ROWS] = {10, 0, 6, 8}; //connect to the row pinouts of the keypad
  47. byte colPins[COLS] = {9, 13, 7}; //connect to the column pinouts of the keypad
  48.  
  49. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  50.  
  51. // initialize the library with the numbers of the interface pins
  52. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  53.  
  54. const int armedLed = A0;      //red led pin
  55. int armed = 0;                //system armed/not
  56.  
  57. const int laserSense = A1;
  58. int sensorHit = 0;
  59.  
  60. void setup() {
  61.   Serial.begin (9600);
  62.  
  63.   // set up the LCD's number of columns and rows:
  64.   lcd.begin(16, 2);
  65.   keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  66.   keypad.setDebounceTime(100);  //less bounce is more responsive
  67.  
  68.   pinMode (armedLed, OUTPUT);  //red led
  69.   pinMode (laserSense, INPUT);
  70.   analogWrite(armedLed, 0);    //start as off
  71.  
  72.   // Print a message to the LCD to start things off:
  73.   lcd.print("Code to arm:");
  74.  
  75. //  sensorHit=analogRead(laserSense);
  76. //  Serial.println (sensorHit);
  77. }
  78.  
  79. void loop() {
  80.   // set the cursor to column 0, line 1
  81.   // line 0 is the first row, line 1 is the second row
  82.   lcd.setCursor(0, 1);
  83.  
  84.   sensorHit=analogRead(laserSense);
  85.   Serial.println (sensorHit);  // <900 no laser >900 is laser
  86.  
  87.   keypad.getKey();  // take keypad input
  88. }
  89.  
  90. //
  91.  
  92. //take care of some special events
  93. void keypadEvent(KeypadEvent eKey){
  94.   switch (keypad.getState()){
  95.       case PRESSED:
  96.       lcd.print(eKey);  // print key to screen
  97.     switch (eKey){
  98.       case '#': guessPassword(); break;  // # is the 'enter' key
  99.       default:
  100.       password.append(eKey);
  101.     }
  102.   }  
  103. }
  104.  
  105. void guessPassword(){
  106.   if (password.evaluate()){       //if code is correct:
  107.     lcd.clear();                  //clear LCD
  108.     lcd.print("VALID PASSWORD");  //print message
  109.     password.reset();             //resets password after correct entry
  110.     delay(1500);                  //wait...
  111.     lcd.clear();                  //clear
  112.     if (armed==0){                //if system is off (ie: disarmed)
  113.       lcd.print("ARMED!");         //display message
  114.       analogWrite(armedLed, 255);  //led on
  115.       armed=1;                      //system armed
  116.       delay(2000);                  //wait
  117.       lcd.clear();                  //clear
  118.       lcd.setCursor(0, 0);          //return to top left of LCD
  119.       lcd.print("Code to disarm:"); //back to where we began
  120.     }
  121.     else{
  122.       lcd.print("DISARMED!");         //display message
  123.       analogWrite(armedLed, 0);      //led off
  124.       armed=0;                       //system off
  125.       delay(2000);                  //wait
  126.       lcd.clear();                  //clear
  127.       lcd.setCursor(0, 0);          //return to top left of LCD
  128.       lcd.print("Code to arm:");     //back to where we began
  129.     }
  130.   }
  131.   else{                            //if password is incorrect:
  132.     lcd.clear();
  133.     lcd.print("INVALID PASSWORD");
  134.     password.reset();             //resets password after INCORRECT entry
  135.     delay(2000);
  136.     lcd.clear();
  137.     lcd.setCursor(0, 0);
  138.     lcd.print("Retry Code:");
  139.   }
  140. }
RAW Paste Data