Advertisement
Guest User

Untitled

a guest
Nov 9th, 2015
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <Key.h>
  2. #include <Keypad.h>
  3.  
  4. /*  Keypadtest.pde
  5.  *
  6.  *  The first step is to connect your keypad to the
  7.  *  Arduino  using the pin numbers listed below in
  8.  *  rowPins[] and colPins[]. If you want to use different
  9.  *  pins then  you  can  change  the  numbers below to
  10.  *  match your setup.
  11.  *
  12.  */
  13. #include <Keypad.h>
  14.  
  15. String code = "1235"; //The code
  16. String userInput = ""; //Log of what the user has pressed
  17. boolean blinking = false; //Check if Arduino should be blinking
  18. int delayTime = 125; //Delay between blinks
  19. int maxBlinks = 8; //Max amount of blinks
  20. int port; //Port in which it should blink. Port 4 is correct and 5 incorrect
  21.  
  22. const byte ROWS = 4; // Four rows
  23. const byte COLS = 3; // Three columns
  24. // Define the Keymap
  25. char keys[ROWS][COLS] = {
  26.   {'1','2','3'},
  27.   {'4','5','6'},
  28.   {'7','8','9'},
  29.   {'*','0','#'}
  30. };
  31. // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  32. byte rowPins[ROWS] = { 6, 7, 8, 9 };
  33. // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
  34. byte colPins[COLS] = { 10, 11, 12 };
  35.  
  36. // Create the Keypad
  37. Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  38.  
  39. #define ledpin 13
  40.  
  41. void setup(){
  42.   pinMode(ledpin,OUTPUT);
  43.   pinMode(4, OUTPUT);
  44.   pinMode(5, OUTPUT);
  45.   digitalWrite(ledpin, HIGH);
  46.   Serial.begin(9600);
  47.   Serial.print("Please enter the secret code: ");
  48. }
  49.  
  50. void loop(){
  51.   /*
  52.    * If all the characters has been pressed, it
  53.    * should be blinking.
  54.    */
  55.   if(blinking){
  56.     blinking = false;
  57.    
  58.     for(int timesBlinked = 0; timesBlinked < maxBlinks; timesBlinked++){
  59.       delay(delayTime);
  60.       digitalWrite(port, HIGH);
  61.       delay(delayTime);
  62.       digitalWrite(port, LOW);
  63.     }
  64.    
  65.     userInput = "";
  66.     Serial.print("Please enter the secret code: ");    
  67.   }
  68.   /*
  69.    * Waiting for keypresses
  70.    */
  71.   else{
  72.     char key = kpd.getKey();
  73.     if(key)  // Check for a valid key.
  74.     {
  75.       //Something something
  76.       if(key == '*'){
  77.        
  78.       }
  79.       //Resets the input
  80.       else if(key == '#'){
  81.         userInput = "";
  82.         Serial.println("");
  83.         Serial.print("Please enter the secret code: ");
  84.       }
  85.       //Logs the user input
  86.       else{
  87.         Serial.print(key);
  88.         userInput += key;
  89.       }
  90.  
  91.       //If the user has pressed the code
  92.       if(userInput.length() == code.length()){
  93.         Serial.println("");
  94.         //The code is correct
  95.         if(userInput.equals(code)){
  96.           Serial.println("Correct!");  
  97.           port = 4;
  98.         }
  99.         //The code is incorrect
  100.         else{
  101.           Serial.println("Wrong!");  
  102.           port = 5;
  103.         }
  104.         blinking = true;
  105.       }
  106.     }
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement