電子密碼鎖教程,你還在擔心找不到鑰匙嗎?

這應該是一個很酷且很容易的項目。我們將把從網上購買小型鍵盤並連接到一個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);

}
}
}


分享到:


相關文章: