Arduino Keypad
Arduino Keypad
1. Test
Linh kiện: Arduino Uno, Keypad-SmallCalc.
Thư viện: Keypad.
Proteus:
Arduino IDE:
#include <Keypad.h>
const byte hang = 4;
const byte cot = 4;
char keys[hang][cot] ={
{'7','8','9',':'},
{'4','5','6','x'},
{'1','2','3','-'},
{'c','0','=','+'},
};
byte cacHang[hang] = {2, 3, 4, 5};
byte cacCot[cot] = {6, 7, 8, 9};
Keypad kp = Keypad(makeKeymap(keys), cacHang, cacCot, hang, cot);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
char key = kp.getKey();
if(key){
Serial.print(key);
}
}
2. Hiện với LCD
Thư viện: LiquidCrystal I2C.
Proteus:
Arduino IDE:
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20, 16, 2);
const byte hang = 4;
const byte cot = 4;
char keys[hang][cot] ={
{'7','8','9',':'},
{'4','5','6','x'},
{'1','2','3','-'},
{'c','0','=','+'},
};
byte cacHang[hang] = {2, 3, 4, 5};
byte cacCot[cot] = {6, 7, 8, 9};
Keypad kp = Keypad(makeKeymap(keys), cacHang, cacCot, hang, cot);
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
lcd.print("Keypad + LCD");
lcd.setCursor(0, 1);
}
void loop() {
// put your main code here, to run repeatedly:
char key = kp.getKey();
if(key){
lcd.print(key);
}
}
3. Khóa điện tử:
Proteus:
Arduino IDE:
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20, 16, 2);
int relay = 13;
char Matkhau[4] = {'2','4','6','8'}; // Mật khẩu: 2468
int state = 0; // state = 0: Không nhấn
// state = 1: Nhấn xong nhả
// state = 2: Nhấn giữ
int n = 3; // số trạng thái có thể có của việc nhấn phím
char str[4] = {' ',' ',' ', ' '}; // Xâu rỗng để lưu các phím được nhấn
const byte hang = 4;
const byte cot = 4;
char keys[hang][cot] ={
{'7','8','9',':'},
{'4','5','6','x'},
{'1','2','3','-'},
{'c','0','=','+'},
};
byte cacHang[hang] = {2, 3, 4, 5};
byte cacCot[cot] = {6, 7, 8, 9};
char key = 0;
Keypad kp = Keypad(makeKeymap(keys), cacHang, cacCot, hang, cot);
int i;
bool Check = 0;
void setup() {
// put your setup code here, to run once:
pinMode(relay, OUTPUT);
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
lcd.setCursor(1, 0);
lcd.print("Nhap mat khau: ");
}
// PRESSED: Nút được nhấn
// HOLD: Nút được nhấn nhưng chưa được nhả
// REALEASED: Nút đã được nhả sau khi nhấn
// IDLE: Không có hành động nào được thực hiện
void loop() {
// put your main code here, to run repeatedly:
char temp = kp.getKey();
if((int)kp.getState() == PRESSED){
if(temp != 0 ){
key = temp;
}
}
if((int)kp.getState() == HOLD){
state++;
state = constrain(state, 1, n-1);
delay(50);
}
if((int)kp.getState() == RELEASED){
key+= state;
state = 0;
if(i==0){
str[0] = key;
lcd.setCursor(6, 1);
lcd.print(str[0]);
delay(10);
lcd.setCursor(6,1);
lcd.print("*");
}
if(i==1){
str[1] = key;
lcd.setCursor(7, 1);
lcd.print(str[1]);
delay(10);
lcd.setCursor(7,1);
lcd.print("*");
}
if(i==2){
str[2] = key;
lcd.setCursor(8, 1);
lcd.print(str[2]);
delay(10);
lcd.setCursor(8,1);
lcd.print("*");
}
if(i==3){
str[3] = key;
lcd.setCursor(9, 1);
lcd.print(str[3]);
delay(10);
lcd.setCursor(9,1);
lcd.print("*");
Check = 1;
}
i = i + 1;
}
if(Check == 1){
if(str[0] == Matkhau[0] && str[1] == Matkhau[1] && str[2] == Matkhau[2] && str[3] == Matkhau[3]){
lcd.clear();
lcd.setCursor(2,1);
lcd.print("Mat khau dung");
lcd.setCursor(3,1);
lcd.print("Xin moi vao");
digitalWrite(relay, HIGH);
delay(1000);
digitalWrite(relay, LOW);
i = 0;
Check = 0;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Nhap mat khau: ");
}else{
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Mat khau sai");
lcd.setCursor(0,1);
lcd.print("Thu lai");
delay(100);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Nhap mat khau: ");
i = 0;
Check = 0;
}
}
delay(10);
}