电子密码锁教程,你还在担心找不到钥匙吗?

这应该是一个很酷且很容易的项目。我们将把从网上购买小型键盘并连接到一个Arduino微控制器上。你可以使用你想要的任何Arduino版本。使用KeyPad库,我们将能够使用arduino的8个引脚读取该键盘所具有的16个按钮中任意一个。该库是非阻塞的,这意味着您可以一直按住该键,Arduino将继续处理其余代码。但是,应该考虑一下,当编写代码时,使用的每个延时函数都会占用键盘的处理时间。函数getKey()在按下键后立即返回键值,但不会自动重复。此外,当您释放按钮时,如果使用库的eventListener功能,则可以处理按钮的RELEASED事件。

电子密码锁教程,你还在担心找不到钥匙吗?


将此键盘连接到微控制器的数字引脚D2至D9。编写一个简单的代码,它将读取每个输入的字符以匹配我们的密码。如果密码正确,连接到引脚D10的伺服电机将旋转以滑动门锁。如果密码错误,则会出现错误,必须再试一次。将蜂鸣器连接到引脚11,以便发出错误信号声音。按钮将连接到数字引脚D12,此按钮将放置在房间内。此按钮将从内部打开门,以“紧急”退出。连接到针脚D13的另一个按钮将从房间内部关闭门。我们先来看看原理图。

电子密码锁教程,你还在担心找不到钥匙吗?

请记住,对于添加的任何按钮,必须给它一个上拉或下拉电阻。在这种情况下,想检测按钮的HIGH状态,这意味着按钮通常处于LOW状态。为此,在按钮输出引脚上添加一个1k欧姆的电阻到地。为对面引脚提供5伏电压。这样,每次按下按钮,我们都会检测出一个5伏电压,表示高数字读取。按照原理图中的说明连接键盘。还将LCD数据和时钟引脚连接到模拟引脚A4和A5。为伺服电机提供5V和GND,并将信号引脚连接到微控制器的D10。现在需要的是上传下面代码,将所有东西都放在一个盒子中并将系统安装到门上。记得,为了使用这个i2c LCD模块,你必须将i2c液晶库安装到Arduino IDE。

如果按一个键但是LCD输出另一个,这意味着您的ROW和COL引脚接反了...进行测试并编辑您的“hexaKeys [ROWS] [COLS]”以匹配KEYPAD。

所以,上传这段代码。“customKeypad.getKey()”函数将读取按下的键。该功能一次只能读取一个键,如果一直按着按钮,它将不会读取更多键值。您可以在代码中看到,首先我们使用“内部”和“外部”引脚连接的按钮打开/关闭门。然后读取按下的键并开始编辑密码“characters”。当输入所有4个字符时,检查它们是否与我们想要的字符相同。如果是,则驱动伺服电机开门。如果不是,我们会给出错误。

要更改密码,请转到下面照片中的行,然后更改四个Str值中的每一个。在此示例中,密码为3007.“A”字符为确认,“B” 字符为关闭。

电子密码锁教程,你还在担心找不到钥匙吗?

//LCD config
#include
#include
LiquidCrystal_I2C lcd(0x3f,20,4);
#include
#include
//Variables

int mot_min = 90; //min servo angle (set yours)
int mot_max = 180; //Max servo angle (set yours)
int character = 0;
int activated =0;
char Str[16] = {' ', ' ', ' ', ' ', ' ', ' ', '-', '*', '*', '*', ' ', ' ', ' ', ' ', ' ', ' '};
//Pins
Servo myservo;
int buzzer=11; //pin for the buzzer beep
int external = 12; //pin to inside open
int internal = 13; //pin to inside close
//Keypad config
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
myservo.attach(10); //attach the servo to pin D10
pinMode(buzzer,OUTPUT);
pinMode(external,INPUT);
pinMode(internal,INPUT);
//Init the screen and print the first text
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print(" PASSWORD");
lcd.setCursor(0,1);
lcd.print(" -*** ");
//put the servo in the close position first
myservo.write(mot_min);

}

void loop(){
///////////////EMERGENCY OPEN/CLOSE/////////
if (digitalRead(external))
{
myservo.write(mot_max);
lcd.clear();
lcd.setCursor(2,0);
lcd.print("INSIDE OPEN");
activated = 2;

analogWrite(buzzer,240);
delay(250);
analogWrite(buzzer,200);
delay(250);
analogWrite(buzzer,180);
delay(250);
analogWrite(buzzer,250);
delay(250);
analogWrite(buzzer,LOW);


lcd.clear();
lcd.setCursor(4,0);
lcd.print("WELLCOME");

lcd.setCursor(2,1);
lcd.print("ELECTRONOOBS");

lcd.clear();
lcd.setCursor(3,0);
lcd.print("DOOR OPEN");
lcd.setCursor(2,1);
lcd.print("ELECTRONOOBS");
delay(500);

}
if (digitalRead(internal))
{
myservo.write(mot_min);
activated = 0;
character=0;
Str[6]= '-';
Str[7]= '*';
Str[8]= '*';
Str[9]= '*';
Str[10]= ' ';
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" PASSWORD");
lcd.setCursor(0,1);
lcd.print(Str);
}

///////////////KEYPAD OPEN/CLOSE////////////
char customKey = customKeypad.getKey(); //this function reads the presed key

if (customKey){
if (character ==0)
{
Serial.println(customKey);

Str[6]= customKey;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" PASSWORD");
lcd.setCursor(0,1);
lcd.print(Str);

}
if (character ==1)
{
Serial.println(customKey);
Str[7]= customKey;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" PASSWORD");
lcd.setCursor(0,1);
lcd.print(Str);

}
if (character ==2)
{
Serial.println(customKey);
Str[8]= customKey;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" PASSWORD");
lcd.setCursor(0,1);
lcd.print(Str);

}
if (character ==3)
{
Serial.println(customKey);
Str[9]= customKey;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" PASSWORD");
lcd.setCursor(0,1);
lcd.print(Str);

}
if (character ==4)
{
Serial.println(customKey);
Str[10]= customKey;
activated=1;

}
character=character+1;
}

if (activated == 1)
{
/*Change your password below!!!
Change each of Str[6], Str[7], Str[8], Str[9]*/
if(Str[10]='A' && character==5 && Str[6]=='3' && Str[7]=='0' && Str[8]=='0' && Str[9]=='7' )
{
myservo.write(mot_max);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("ACCEPTED");
activated = 2;
analogWrite(buzzer,240);
delay(250);
analogWrite(buzzer,200);
delay(250);
analogWrite(buzzer,180);
delay(250);
analogWrite(buzzer,250);
delay(250);
analogWrite(buzzer,LOW);
delay(1000);

lcd.clear();
lcd.setCursor(4,0);
lcd.print("WELLCOME");
delay(500);
lcd.setCursor(2,1);
lcd.print("ELECTRONOOBS");
delay(1000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("DOOR OPEN");
lcd.setCursor(2,1);
lcd.print("ELECTRONOOBS");

}
else
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("PASSWORD ERROR");
lcd.setCursor(3,1);
lcd.print("TRY AGAIN");
analogWrite(buzzer,150);
delay(3000);
analogWrite(buzzer,LOW);
character=0;
Str[6]= '-';
Str[7]= '*';
Str[8]= '*';

Str[9]= '*';
Str[10]= ' ';
activated = 0;
lcd.clear();
lcd.setCursor(4,0);
lcd.print("PASSWORD");
lcd.setCursor(0,1);
lcd.print(Str);
}
}
if (activated == 2)
{
if(customKey == 'B' )
{
myservo.write(mot_min);
activated = 0;
character=0;
Str[6]= '-';
Str[7]= '*';
Str[8]= '*';
Str[9]= '*';
Str[10]= ' ';
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" PASSWORD");
lcd.setCursor(0,1);
lcd.print(Str);

}
}
}


分享到:


相關文章: