Mini Project
เปิดปิดประตูด้วย Keycard + Keypad
อุปกรณ์
- Arduino UNO R3
- LCD I2C 16*2
- Keypad 4*4
- Buzzer
- Rc522
- สายไฟ
Wiring diagram
Block
LCD 16*2
Gnd - Gnd
Vcc - 5v
SDA - A4
SCL - A5
Keypad
1 - 5
2 - 6
3 - 7
4 - 8
5 - 1
6 - 2
7 - 3
8 - 5
Gnd - Gnd
Vcc - 5v
SDA - A4
SCL - A5
Keypad
1 - 5
2 - 6
3 - 7
4 - 8
5 - 1
6 - 2
7 - 3
8 - 5
RC522
SDA - 10
SCK - 13
MOSI - 11
MISO - 12
IRQ - ไม่ต่อ
GND - GND
RST - 9
3.3v - 3.3v
BUZZER
Vcc - A3
GND - GND
SDA - 10
SCK - 13
MOSI - 11
MISO - 12
IRQ - ไม่ต่อ
GND - GND
RST - 9
3.3v - 3.3v
BUZZER
Vcc - A3
GND - GND
Code & Libraries
#include <Keypad.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
unsigned long uidDec, uidDecTemp;
LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display
Servo myservo;
#define Password_Lenght 7 // Give enough room for six chars + NULL char
int pos = 0; // variable to store the servo position
char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
char Master[Password_Lenght] = "310843";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
bool door = true;
bool Rc = true;
byte rowPins[ROWS] = {4, 3, 2, 1}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6, 5}; //connect to the column pinouts of the keypad
Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
void setup()
{
// Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
myservo.attach(0);
ServoClose();
lcd.begin(16, 2);
lcd.setBacklight(255);
lcd.print(" Arduino Door");
lcd.setCursor(0, 1);
lcd.print("--Look project--");
delay(3000);
lcd.clear();
analogWrite(A0,0);
}
void loop()
{
Serial.println(door);
Serial.println(Rc);
if (door == 0 && Rc == 0)
{
lcd.clear();
lcd.print(" Door is Open");
//analogWrite(A0,255);
customKey = customKeypad.getKey();
if (customKey == '#')
{
lcd.clear();
ServoClose();
lcd.print(" Door is close");
//delay(3000);
load();
door = 1;
Rc = 1;
lcd.clear();
}
}
if (Rc == 1) Rc522(); delay(100); //analogWrite(A0,0);
if (door == 1) Open(); delay(100); //analogWrite(A0,0);
if(Rc==0&&door==0)analogWrite(A0,255);
if(Rc==1||door==1)analogWrite(A0,0);
}
void clearData()
{
while (data_count != 0)
{
Data[data_count--] = 0;
}
return;
}
void ServoOpen()
{
// for (pos = 180; pos >= 0; pos -= 5) {
// // in steps of 1 degree
// myservo.write(pos);
// delay(15);
// }
analogWrite(A3, 255);
delay(100);
analogWrite(A3, 0);
delay(100);
}
void ServoClose()
{
// for (pos = 0; pos <= 180; pos += 5) {
// myservo.write(pos);
// delay(15);
// }
analogWrite(A3, 255);
delay(100);
analogWrite(A3, 0);
delay(100);
analogWrite(A3, 255);
delay(100);
analogWrite(A3, 0);
delay(100);
}
void Open()
{
lcd.setCursor(0, 1);
lcd.print("Password :");
customKey = customKeypad.getKey();
if (customKey)
{
Data[data_count] = customKey;
lcd.setCursor(10+data_count, 1);
lcd.print("*");
data_count++;
}
if (data_count == Password_Lenght - 1)
{
if (!strcmp(Data, Master))
{
//lcd.clear();
ServoOpen();
lcd.setCursor(0, 1);
lcd.print(" --> Pass <-- ");
door = 0;
}
else
{
lcd.clear();
lcd.print(" Wrong Password");
analogWrite(A3, 255);
delay(1000);
analogWrite(A3, 0);
door = 1;
Rc = 1;
}
clearData();
}
}
void Rc522(){
lcd.setCursor(0, 0);
lcd.print(" Touch Keycrad ");
if ( ! mfrc522.PICC_IsNewCardPresent()) { //Look for new cards.
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Select one of the cards.
return;
}
for (byte i = 0; i < mfrc522.uid.size; i++) { // Issuance of the serial number of the card "UID".
uidDecTemp = mfrc522.uid.uidByte[i];
uidDec = uidDec * 256 + uidDecTemp;
Serial.println(uidDec);
}
if (uidDec == 3350538559 || uidDec == 3039379399 || uidDec == 692045749 || uidDec == 1070052649) {
Rc = 0;
lcd.setCursor(0, 0);
lcd.print(" --> Pass <-- ");
analogWrite(A3, 255); delay(100);
analogWrite(A3, 0); delay(100);
}
else
{
Rc = 1;
door = 1;
lcd.clear();
lcd.print(" Wrong Password");
analogWrite(A3, 255); delay(1000);
analogWrite(A3, 0); delay(1000);
}
}
void load(){
lcd.setCursor(0, 1);
lcd.print(" Load. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load..... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load...... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load....... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load........ "); delay(100);
lcd.setCursor(0, 1);
lcd.setCursor(0, 1);
lcd.print(" Load. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load..... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load...... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load....... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load........ "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load..... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load...... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load....... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load........ "); delay(100);
}
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
unsigned long uidDec, uidDecTemp;
LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display
Servo myservo;
#define Password_Lenght 7 // Give enough room for six chars + NULL char
int pos = 0; // variable to store the servo position
char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
char Master[Password_Lenght] = "310843";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
bool door = true;
bool Rc = true;
byte rowPins[ROWS] = {4, 3, 2, 1}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6, 5}; //connect to the column pinouts of the keypad
Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
void setup()
{
// Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
myservo.attach(0);
ServoClose();
lcd.begin(16, 2);
lcd.setBacklight(255);
lcd.print(" Arduino Door");
lcd.setCursor(0, 1);
lcd.print("--Look project--");
delay(3000);
lcd.clear();
analogWrite(A0,0);
}
void loop()
{
Serial.println(door);
Serial.println(Rc);
if (door == 0 && Rc == 0)
{
lcd.clear();
lcd.print(" Door is Open");
//analogWrite(A0,255);
customKey = customKeypad.getKey();
if (customKey == '#')
{
lcd.clear();
ServoClose();
lcd.print(" Door is close");
//delay(3000);
load();
door = 1;
Rc = 1;
lcd.clear();
}
}
if (Rc == 1) Rc522(); delay(100); //analogWrite(A0,0);
if (door == 1) Open(); delay(100); //analogWrite(A0,0);
if(Rc==0&&door==0)analogWrite(A0,255);
if(Rc==1||door==1)analogWrite(A0,0);
}
void clearData()
{
while (data_count != 0)
{
Data[data_count--] = 0;
}
return;
}
void ServoOpen()
{
// for (pos = 180; pos >= 0; pos -= 5) {
// // in steps of 1 degree
// myservo.write(pos);
// delay(15);
// }
analogWrite(A3, 255);
delay(100);
analogWrite(A3, 0);
delay(100);
}
void ServoClose()
{
// for (pos = 0; pos <= 180; pos += 5) {
// myservo.write(pos);
// delay(15);
// }
analogWrite(A3, 255);
delay(100);
analogWrite(A3, 0);
delay(100);
analogWrite(A3, 255);
delay(100);
analogWrite(A3, 0);
delay(100);
}
void Open()
{
lcd.setCursor(0, 1);
lcd.print("Password :");
customKey = customKeypad.getKey();
if (customKey)
{
Data[data_count] = customKey;
lcd.setCursor(10+data_count, 1);
lcd.print("*");
data_count++;
}
if (data_count == Password_Lenght - 1)
{
if (!strcmp(Data, Master))
{
//lcd.clear();
ServoOpen();
lcd.setCursor(0, 1);
lcd.print(" --> Pass <-- ");
door = 0;
}
else
{
lcd.clear();
lcd.print(" Wrong Password");
analogWrite(A3, 255);
delay(1000);
analogWrite(A3, 0);
door = 1;
Rc = 1;
}
clearData();
}
}
void Rc522(){
lcd.setCursor(0, 0);
lcd.print(" Touch Keycrad ");
if ( ! mfrc522.PICC_IsNewCardPresent()) { //Look for new cards.
return;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Select one of the cards.
return;
}
for (byte i = 0; i < mfrc522.uid.size; i++) { // Issuance of the serial number of the card "UID".
uidDecTemp = mfrc522.uid.uidByte[i];
uidDec = uidDec * 256 + uidDecTemp;
Serial.println(uidDec);
}
if (uidDec == 3350538559 || uidDec == 3039379399 || uidDec == 692045749 || uidDec == 1070052649) {
Rc = 0;
lcd.setCursor(0, 0);
lcd.print(" --> Pass <-- ");
analogWrite(A3, 255); delay(100);
analogWrite(A3, 0); delay(100);
}
else
{
Rc = 1;
door = 1;
lcd.clear();
lcd.print(" Wrong Password");
analogWrite(A3, 255); delay(1000);
analogWrite(A3, 0); delay(1000);
}
}
void load(){
lcd.setCursor(0, 1);
lcd.print(" Load. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load..... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load...... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load....... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load........ "); delay(100);
lcd.setCursor(0, 1);
lcd.setCursor(0, 1);
lcd.print(" Load. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load..... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load...... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load....... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load........ "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.. "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load.... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load..... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load...... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load....... "); delay(100);
lcd.setCursor(0, 1);
lcd.print(" Load........ "); delay(100);
}
โหลดโค้ดและ LBR = https://drive.google.com/open?id=16ueB6bbXCsxJDPLhxZD_QFT4J5uirqiq
การทำงาน
เมื่อ Run code ที่จอ LCD อักษรขึ้นว่า Arduino projec
แล้วจะมีเสียงดังขึ้นสองครั้ง แสดงว่าพร้องในการใส่รหัส เมื่อเราใส่รหัสและแตะบัตร ผ่านจะขึ้นว่า -- Pass -- แล้วจะมีไฟออกที่ Output ทำให้ประดูถูกเปิดจอ LCD จะขึ้นว่า Door is Open หากเราต้องการจะปิดประตูใหนกด # ของ Keypad จอLCDจะถึงว่า Door is Close ไฟที่จ่าย Output จะไม่มีและประตูจะทำการล็อก รอประมาณ 3 วินาที ถึงจะไปสู่หน้าแรก ถ้ารหัสผิดจะมีเสียงดังยาว ประมาณ 1 วินาที
ต่อวงจรServo ยังไงครับ
ตอบลบ