Skip to content

Instantly share code, notes, and snippets.

@sj82516
Created July 30, 2014 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sj82516/7d95b81fe2655f9b0230 to your computer and use it in GitHub Desktop.
Save sj82516/7d95b81fe2655f9b0230 to your computer and use it in GitHub Desktop.
Arduin0_keypad input password and shows on LCD
// 引用 Keypad library
#include <Keypad.h>
#include <Password.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
char *pwd={"123"};
Password password = Password(pwd);
// 3x4 Keypad
const byte ROWS = 4; // 4 Rows
const byte COLS = 4; // 3 Columns
// 定義 Keypad 的按鍵
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// 定義 Keypad 連到 Arduino 的接腳
byte rowPins[ROWS] = { 9 , 8 , 7 , 6}; // 連到 Keypad 的 4 個 Rows: Row0, Row1, Row2, Row3
byte colPins[COLS] = {10, 11 , 12, 13}; // 連到 Keypad 的 3 個 Columns: Column0, Column1, Column2
// 建立 Keypad 物件
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
delay(200);
pinMode(2, OUTPUT); //green light
pinMode(3, OUTPUT); //red light
lcd.init();
lcd.backlight();
lcd.print(pwd);
lcd.setCursor(0, 1);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
void loop(){
keypad.getKey();
}
//take care of some special events
int lcdp=0;
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
lcd.setCursor(lcdp, 1);
delay(10);
switch (eKey){
case 'A':
checkPassword();
delay(1);
break;
case 'B':
password.reset();
lcd.clear();
lcdp=0;
lcd.setCursor(0, 0);
lcd.print(pwd);
delay(1);
break;
default:
password.append(eKey);
lcd.print(eKey);
lcdp++;
delay(1);
}
}
}
void checkPassword(){
if (password.evaluate()){
lcd.print("Accepted");
delay(10);
digitalWrite(2, HIGH);//turn on
delay(2000); //wait 5 seconds
digitalWrite(2, LOW);// turn off
lcd.clear();
lcdp=0;
lcd.setCursor(0, 0);
lcd.print(pwd);
}else{
lcd.print("Denied");
delay(10);
digitalWrite(3, HIGH); //turn on
delay(2000); //wait 5 seconds
digitalWrite(3, LOW);//turn off
lcd.clear();
lcdp=0;
lcd.setCursor(0, 0);
lcd.print(pwd);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment