Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- String pass = "1234";
- const int debounceTime = 10;
- const byte pinLED = 6;
- const byte buzzer = A5;
- const byte ROWS = 4; // Four rows
- const byte COLS = 3; // Three columns
- // Define the Keymap
- const char keys[ROWS][COLS] = {
- {'3','2','1'},
- {'6','5','4'},
- {'9','8','7'},
- {'E','0','B'}
- };
- char password[4] = {'1','2','3','4'};
- char buff[4] = {'-','-','-','-'};
- boolean correct = true;
- int cur = 0;
- // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
- byte rowPins[ROWS] = { A1, A2, A3, A4 };//A1 error
- // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
- byte colPins[COLS] = { 7, 8, 9 };
- void setup()
- {
- lcd.begin(16,2);
- pinMode(buzzer,OUTPUT);
- pinMode(pinLED,OUTPUT);
- //Serial.begin(9600);
- for (int row = 0; row < ROWS; row++)
- {
- pinMode(rowPins[row],INPUT); // Set row pins as input
- digitalWrite(rowPins[row],HIGH); // turn on Pull-ups
- }
- for (int column = 0; column < COLS; column++)
- {
- pinMode(colPins[column],OUTPUT); // Set column pins as outputs
- digitalWrite(colPins[column],HIGH); // Make all columns inactive
- }
- lcd.print("Enter Password");
- lcd.setCursor(0, 1);
- lcd.print("Pass : ");
- }
- char getKey()
- {
- char key = 0; // 0 indicates no key pressed
- for(int column = 0; column < COLS; column++)
- {
- digitalWrite(colPins[column],LOW); // Activate the current column.
- for(int row = 0; row < ROWS; row++) // Scan all rows for
- // a key press.
- {
- if(digitalRead(rowPins[row]) == LOW) // Is a key pressed?
- {
- delay(debounceTime); // debounce
- while(digitalRead(rowPins[row]) == LOW)
- ; // wait for key to be released
- key = keys[row][column]; // Remember which key
- // was pressed.
- }
- }
- digitalWrite(colPins[column],HIGH); // De-activate the current column.
- }
- return key; // returns the key pressed or 0 if none
- }
- void loop()
- {
- char key = getKey();
- if(key) // Check for a valid key.
- {
- digitalWrite(buzzer,HIGH);
- delay(50);
- switch (key)
- {
- case 'E':
- correct=true;
- for(int i=0;i<4;i++)
- {
- if (buff[i] != password[i])
- {
- correct = false;
- }
- }
- lcd.clear();
- lcd.setCursor(0, 0);
- if(correct==true)
- {
- lcd.print("Correct!!");
- Serial.println("Correct");
- digitalWrite(pinLED,HIGH);
- }
- else
- {
- lcd.print("Incorrect!!");
- Serial.println("Incorrect");
- }
- for(int i=0;i<4;i++)
- {
- buff[i]='-';
- }
- cur = 0;
- delay(500);
- Serial.println(key);
- break;
- case 'B':
- if(cur>=1)
- {
- cur = cur -1;
- buff[cur]='-';
- }
- Serial.println("Back");
- break;
- default:
- if(cur<4)
- {
- buff[cur]=key;
- cur=cur+1;
- Serial.println(key);
- }
- else
- {
- Serial.println("Full") ;
- }
- }
- for(int i=0;i<4;i++)
- Serial.print(buff[i]);
- Serial.print(" cur ");
- Serial.println(cur);
- lcd.setCursor(0, 0);
- lcd.print("Enter Password");
- lcd.setCursor(0, 1);
- lcd.print("Pass : ");
- for(int i=0;i<cur;i++)
- {lcd.print('*');}
- for(int i=cur;i<4;i++)
- {lcd.print(' ');}
- }
- else
- {
- digitalWrite(pinLED,LOW);
- digitalWrite(buzzer,LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement