/******************************************************************
|| @name ELEGOO Keypad&RFID lock
|| @date 2021.09.03
|| @version 1.0
|| @author Giovanni Gentile
|| @contact www.projectg.it
||
|| @description
|| Open the door by unlocking the servo motor.
|| Digit the right code on the keypad or with RFID. Reset by using "*"
|| Connect to the USB for debugging (9600 baud)
||
|| @connections
|| Keypad pin fron 2 to 9
|| row: 9, 8, 7, 6
|| cols: 5, 4, 3, 2
|| LED Green: pin 10
|| LED Red: pin 11
|| LED Blue: pin 12
|| Servo pin 13
|| Buzzer: pin 22
|| RFID: rfid:GND
|| rfid:3.3V
|| rfid:RST
|| rfid:SDA
|| rfid:SCK
|| rfid:MOSI
|| rfid:MISO
||
*******************************************************************/
#include <Keypad.h>
#include <Servo.h>
#include <MFRC522.h>
#include <SPI.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
// Define the two-dimensional array on the buttons of the keypads
const char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
const byte rowPins[ROWS] = {9, 8, 7, 6}; // Row pinouts of the keypad
const byte colPins[COLS] = {5, 4, 3, 2}; // Column pinouts of the keypad
const char resetKey = '*';
Keypad keypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// PASSWORD MANAGER
const char* password = "123A";
const unsigned int passwordLength = 4; // Corresponding length
unsigned int passwordPosition = 0;
unsigned int numberPressedKeys = 0;
// LED and sound
const unsigned int led_green = 10;
const unsigned int led_red = 11;
const unsigned int led_blue = 12;
const unsigned int servoPin = 13;
const unsigned int buzzer = 22;
const unsigned int note = 800;
const unsigned int blinks = 3;
Servo servo;
const unsigned int servoInitCorrection = 0;
const unsigned int servoClose = servoInitCorrection + 0;
const unsigned int servoOpen = servoInitCorrection + 90;
bool lockStatus = false;
// RFID
const unsigned int SAD = 14;
const unsigned int RST = 5;
const byte doChecks = 3; // Bitwise: 1=LED, 2=Servo, 4=Buzzer
void soundSuccess() {
tone(buzzer, 1*note/4, 150);
delay(100);
tone(buzzer, 2*note/4, 150);
delay(100);
tone(buzzer, 3*note/4, 150);
delay(100);
tone(buzzer, 4*note/4, 150);
}
void soundFail() {
tone(buzzer, 4*note/4, 150);
delay(100);
tone(buzzer, 3*note/4, 150);
delay(100);
tone(buzzer, 2*note/4, 150);
delay(100);
tone(buzzer, 1*note/4, 150);
}
MFRC522 nfc(SAD, RST);
void setup() {
SPI.begin();
Serial.begin(9200);
nfc.begin();
// Init. and check pins
pinMode(led_green,OUTPUT);
pinMode(led_red, OUTPUT);
pinMode(led_blue, OUTPUT);
if ((doChecks & 1) == 1) {
digitalWrite(led_green, HIGH);
delay(500);
digitalWrite(led_red, HIGH);
delay(500);
digitalWrite(led_blue, HIGH);
delay(500);
digitalWrite(led_red, LOW);
digitalWrite(led_green, LOW);
digitalWrite(led_blue, LOW);
delay(500);
digitalWrite(led_green, HIGH);
digitalWrite(led_red, HIGH);
digitalWrite(led_blue, HIGH);
delay(250);
digitalWrite(led_red, LOW);
digitalWrite(led_green, LOW);
digitalWrite(led_blue, LOW);
Serial.println("LED checked");
delay(250);
}
// Init. and check servo
servo.attach(servoPin); // attaches the servo on pin of the servo object
servo.write(servoClose); lockStatus = false;
if ((doChecks & 2) == 2) {
delay(500);
servo.write(servoOpen); lockStatus = true;
delay(150);
servo.write(servoClose); lockStatus = false;
Serial.println("Servo checked");
delay(250);
}
if ((doChecks & 4) == 4) {
// Init. and check buzzer
soundSuccess();
Serial.println("Buzzer checked");
delay(100);
}
// Init. and check RFID
byte version = nfc.getFirmwareVersion();
if ((doChecks & 8) == 8) {
while (! version) {
// No MFRC522 board found
digitalWrite(led_red, HIGH);
delay(250);
digitalWrite(led_red, LOW);
delay(250);
}
digitalWrite(led_green, HIGH);
digitalWrite(led_blue, HIGH);
delay(250);
digitalWrite(led_green, LOW);
digitalWrite(led_blue, LOW);
delay(250);
Serial.println("RFID checked");
Serial.print("Found chip MFRC522, Firmware ver. 0x");
Serial.println(version, HEX);
delay(100);
}
}
void loop() {
char key = keypad.getKey();
unsigned long currentMillis = millis();
// Sound/Blink/Text on every key press
if (key) {
tone(buzzer, note, 20);
Serial.print("Button pressed: ");
Serial.print(key);
Serial.print(", ");
Serial.print("PW-Position: ");
Serial.print(passwordPosition);
Serial.print("/");
Serial.print(passwordLength-1);
Serial.print(", ");
Serial.print("NumberPressedKeys: ");
Serial.print(numberPressedKeys);
Serial.print("/");
Serial.println(passwordLength-1);
digitalWrite(led_blue, HIGH);
delay(100);
digitalWrite(led_blue, LOW);
numberPressedKeys++;
}
// Close lock
if (key == resetKey) {
passwordPosition = 0;
numberPressedKeys = 0;
servo.write(servoClose); lockStatus = false;
Serial.println("Lock: Close");
soundSuccess();
digitalWrite(led_red, HIGH);
delay(400);
digitalWrite(led_red, LOW);
}
if (key == password[passwordPosition]) {
passwordPosition ++;
Serial.println("Correct key");
}
// Open lock
if (passwordPosition == passwordLength) {
passwordPosition = 0;
numberPressedKeys = 0;
servo.write(servoOpen); lockStatus = true;
Serial.println("Lock: Open");
soundSuccess();
for (unsigned int i = 0; i < blinks; i++) {
digitalWrite(led_green, HIGH);
delay(200);
digitalWrite(led_green, LOW);
if (i < blinks-1) {
delay(200);
}
}
}
// Reset input
if (numberPressedKeys >= passwordLength) {
passwordPosition = 0;
numberPressedKeys = 0;
Serial.println("Wrong password");
soundFail();
for (unsigned int i = 0; i < blinks; i++) {
digitalWrite(led_green, HIGH);
digitalWrite(led_red, HIGH);
digitalWrite(led_blue, HIGH);
delay(500);
digitalWrite(led_green, LOW);
digitalWrite(led_red, LOW);
digitalWrite(led_blue, LOW);
if (i < blinks-1) {
delay(500);
}
}
}
// Handle RFID
byte status;
byte data[MAX_LEN];
byte serial[5];
// Send a general request out into the aether. If there is a tag in
// the area it will respond and the status will be MI_OK.
status = nfc.requestTag(MF1_REQIDL, data);
if (status == MI_OK) {
digitalWrite(led_blue, HIGH); delay(500); digitalWrite(led_blue, LOW);
Serial.println("Tag detected! Type: ");
Serial.print(data[0], HEX);
Serial.print(", ");
Serial.println(data[1], HEX);
// calculate the anti-collision value for the currently detected
// tag and write the serial into the data array.
status = nfc.antiCollision(data);
memcpy(serial, data, 5);
Serial.println("Tag Serial Number:");
for (unsigned int i = 0; i < 3; i++) {
Serial.print(serial[i], DEC);
Serial.print(", ");
}
Serial.println(serial[3], DEC);
nfc.selectTag(serial);
nfc.haltTag();
if (lockStatus) {
servo.write(servoClose); lockStatus = false;
} else {
servo.write(servoOpen); lockStatus = true;
}
}
delay(500);
}
Loading
ili9341-cap-touch
ili9341-cap-touch