Advertisement
Guest User

Alarm Pt.4

a guest
May 1st, 2014
2,607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.67 KB | None | 0 0
  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. //click down arrow icon (top right) > New Tab
  29. //paste in code from bottom of: http://arduino.cc/en/Tutorial/Tone
  30. #include "pitches.h"
  31.  
  32. // state the password:
  33. Password password = Password( "3570" );
  34.  
  35. const byte ROWS = 4; //four rows
  36. const byte COLS = 3; //three columns
  37. char keys[ROWS][COLS] = {
  38.   {'1','2','3'},
  39.   {'4','5','6'},
  40.   {'7','8','9'},
  41.   {'*','0','#'}
  42. };
  43. //  keypad1 > pin13
  44. //  keypad2 > pin10
  45. //  keypad3 > pin9
  46. //  keypad4 > pin8
  47. //  keypad5 > pin7
  48. //  keypad6 > pin6
  49. //  keypad7 > pin0
  50. byte rowPins[ROWS] = {10, 0, 6, 8}; //connect to the row pinouts of the keypad
  51. byte colPins[COLS] = {9, 13, 7}; //connect to the column pinouts of the keypad
  52.  
  53. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  54.  
  55. // initialize the library with the numbers of the interface pins
  56. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  57.  
  58. const int armedLed = A0;      //red led pin
  59. int armed = 0;                //system armed/not
  60.  
  61. const int laserSense = A1;            // the LDR
  62. int sensorHit = 0;                    // how bright laser is on LDR
  63. const int buzzer = A2;                // buzzer is on pin A2
  64. int notes[]={                         // possible notes to play
  65.     NOTE_A4, NOTE_B4, NOTE_C3 };
  66.  
  67. void setup() {
  68.   Serial.begin (9600);
  69.  
  70.   // set up the LCD's number of columns and rows:
  71.   lcd.begin(16, 2);
  72.   keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  73.   keypad.setDebounceTime(100);  //less bounce is more responsive
  74.  
  75.   pinMode (armedLed, OUTPUT);        //red led
  76.   pinMode (laserSense, INPUT);       //the LDR
  77.   pinMode (buzzer, OUTPUT);          //the buzzer
  78.   analogWrite(armedLed, 0);          //start as off
  79.  
  80.   // Print a message to the LCD to start things off:
  81.   lcd.print("Code to arm:");
  82.  
  83. }
  84.  
  85. void loop() {
  86.   // set the cursor to column 0, line 1
  87.   // line 0 is the first row, line 1 is the second row
  88.   lcd.setCursor(0, 1);
  89.  
  90.   sensorHit=analogRead(laserSense);
  91.   Serial.println (sensorHit);            // <700 no laser
  92.   if (sensorHit < 700 && armed == 1){    // only beep when armed and beam broken
  93.     tone(buzzer,notes[3],200);           //beam broken play note 3
  94.   }
  95.   keypad.getKey();                       // take keypad input
  96. }
  97.  
  98. //
  99.  
  100. //take care of some special events
  101. void keypadEvent(KeypadEvent eKey){
  102.   switch (keypad.getState()){
  103.       case PRESSED:
  104.       lcd.print(eKey);  // print key to screen
  105.     switch (eKey){
  106.       case '#': guessPassword(); break;  // # is the 'enter' key
  107.       default:
  108.       password.append(eKey);
  109.     }
  110.   }  
  111. }
  112.  
  113. void guessPassword(){
  114.   if (password.evaluate()){       //if code is correct:
  115.     lcd.clear();                  //clear LCD
  116.     lcd.print("VALID PASSWORD");  //print message
  117.     password.reset();             //resets password after correct entry
  118.     delay(1500);                  //wait...
  119.     lcd.clear();                  //clear
  120.     if (armed==0){                //if system is off (ie: disarmed)
  121.       lcd.print("ARMED!");         //display message
  122.       analogWrite(armedLed, 255);  //led on
  123.       armed=1;                      //system armed
  124.       delay(2000);                  //wait
  125.       lcd.clear();                  //clear
  126.       lcd.setCursor(0, 0);          //return to top left of LCD
  127.       lcd.print("Code to disarm:"); //back to where we began
  128.     }
  129.     else{
  130.       lcd.print("DISARMED!");         //display message
  131.       analogWrite(armedLed, 0);      //led off
  132.       armed=0;                       //system off
  133.       delay(2000);                  //wait
  134.       lcd.clear();                  //clear
  135.       lcd.setCursor(0, 0);          //return to top left of LCD
  136.       lcd.print("Code to arm:");     //back to where we began
  137.     }
  138.   }
  139.   else{                            //if password is incorrect:
  140.     lcd.clear();
  141.     lcd.print("INVALID PASSWORD");
  142.     password.reset();             //resets password after INCORRECT entry
  143.     tone(buzzer,notes[1],200);    //boop! wrong code.
  144.     delay(2000);
  145.     lcd.clear();
  146.     lcd.setCursor(0, 0);
  147.     lcd.print("Retry Code:");
  148.   }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement