Advertisement
pongfactory

Arduino LCD 1

Feb 11th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  4.  
  5. String pass = "1234";
  6. const int debounceTime = 10;
  7. const byte pinLED = 6;
  8. const byte buzzer = A5;
  9. const byte ROWS = 4; // Four rows
  10. const byte COLS = 3; // Three columns
  11. // Define the Keymap
  12. const char keys[ROWS][COLS] = {
  13.   {'3','2','1'},
  14.   {'6','5','4'},
  15.   {'9','8','7'},
  16.   {'E','0','B'}
  17. };
  18. char password[4] = {'1','2','3','4'};
  19. char buff[4] = {'-','-','-','-'};
  20. boolean correct = true;
  21. int cur = 0;
  22.  
  23. // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  24. byte rowPins[ROWS] = { A1, A2, A3, A4 };//A1 error
  25. // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
  26. byte colPins[COLS] = { 7, 8, 9 };
  27.  
  28.  
  29. void setup()
  30. {
  31.   lcd.begin(16,2);
  32.   pinMode(buzzer,OUTPUT);
  33.   pinMode(pinLED,OUTPUT);
  34.   //Serial.begin(9600);
  35.    for (int row = 0; row < ROWS; row++)
  36.   {
  37.     pinMode(rowPins[row],INPUT);       // Set row pins as input
  38.     digitalWrite(rowPins[row],HIGH);   // turn on Pull-ups
  39.   }
  40.   for (int column = 0; column < COLS; column++)
  41.   {
  42.     pinMode(colPins[column],OUTPUT);     // Set column pins as outputs
  43.     digitalWrite(colPins[column],HIGH);  // Make all columns inactive
  44.   }
  45.    lcd.print("Enter Password");
  46.    lcd.setCursor(0, 1);
  47.    lcd.print("Pass : ");
  48.  
  49. }
  50.  
  51. char getKey()
  52. {
  53.   char key = 0;                                  // 0 indicates no key pressed
  54.  
  55.   for(int column = 0; column < COLS; column++)
  56.   {
  57.     digitalWrite(colPins[column],LOW);         // Activate the current column.
  58.     for(int row = 0; row < ROWS; row++)     // Scan all rows for
  59.                                                // a key press.
  60.     {
  61.       if(digitalRead(rowPins[row]) == LOW)     // Is a key pressed?
  62.       {
  63.         delay(debounceTime);                   // debounce
  64.         while(digitalRead(rowPins[row]) == LOW)
  65.             ;                                  // wait for key to be released
  66.         key = keys[row][column];             // Remember which key
  67.                                                // was pressed.
  68.       }
  69.     }
  70.     digitalWrite(colPins[column],HIGH);     // De-activate the current column.
  71.   }
  72.   return key;  // returns the key pressed or 0 if none
  73. }
  74.  
  75. void loop()
  76. {
  77.   char key = getKey();
  78.   if(key)  // Check for a valid key.
  79.   {
  80.     digitalWrite(buzzer,HIGH);
  81.     delay(50);
  82.     switch (key)
  83.     {
  84.       case 'E':
  85.         correct=true;
  86.         for(int i=0;i<4;i++)
  87.          {
  88.           if (buff[i] != password[i])
  89.            {
  90.              correct = false;
  91.            }
  92.          }
  93.         lcd.clear();
  94.         lcd.setCursor(0, 0);
  95.         if(correct==true)
  96.           {
  97.            
  98.             lcd.print("Correct!!");
  99.             Serial.println("Correct");
  100.             digitalWrite(pinLED,HIGH);
  101.           }
  102.         else
  103.           {
  104.             lcd.print("Incorrect!!");
  105.             Serial.println("Incorrect");
  106.           }
  107.           for(int i=0;i<4;i++)
  108.            {
  109.              buff[i]='-';
  110.            }
  111.           cur = 0;
  112.            delay(500);
  113.         Serial.println(key);
  114.         break;
  115.       case 'B':
  116.         if(cur>=1)
  117.           {
  118.            cur = cur -1;
  119.            buff[cur]='-';
  120.           }
  121.           Serial.println("Back");
  122.         break;
  123.       default:
  124.         if(cur<4)
  125.          {
  126.           buff[cur]=key;
  127.           cur=cur+1;
  128.           Serial.println(key);
  129.          }
  130.         else
  131.          {
  132.           Serial.println("Full") ;
  133.          }
  134.  
  135.       }
  136.      
  137.         for(int i=0;i<4;i++)
  138.         Serial.print(buff[i]);
  139.         Serial.print(" cur ");
  140.         Serial.println(cur);
  141.         lcd.setCursor(0, 0);
  142.         lcd.print("Enter Password");
  143.         lcd.setCursor(0, 1);
  144.         lcd.print("Pass : ");
  145.         for(int i=0;i<cur;i++)
  146.         {lcd.print('*');}
  147.         for(int i=cur;i<4;i++)
  148.         {lcd.print(' ');}
  149.   }
  150.   else
  151.    {
  152.         digitalWrite(pinLED,LOW);
  153.         digitalWrite(buzzer,LOW);
  154.    }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement