/**Questo programma permette di far inserire da un utente un codice con il KeyPad. Qualora il User_Pin inserito sia corretto, viene chiesto lo UserCode e se è cooretto, si apre la porta.
Per inviare il codice, una volta digitato interamente è necessario premere *. Mentre per spegnere il LED, oppure re-digitare il codice è necessario premere #
Autore Ciro Capelletta
Collegamento tastiera : {'1','2','3','A'} ,
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
-----------------
! ! ! ! ! ! ! !
9 8 7 6 5 4 3 2
Collegamento Display ---------------------------------
11- - - - GND --> 0V
- - - - VCC --> 5v
- - - SDA --> A4
- - - SCL --> A5
---------------------------------
COLLEGAMENTO RELAY PORTA PIN 10, OV, 5V
Collegamento servo motor servo1Pin = 12;
Versione 1.0
*/
#include <Servo.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define Contrasto_Display_pin 11
#define Relay_Door 10
Servo servo1;
LiquidCrystal_I2C lcd25 = LiquidCrystal_I2C(0x27, 16, 2);
String Lcd25_riga1 = "MA.R.S. ROBOTICS";
String Lcd25_riga2 = "Address 25";
int lcd25_ = 32;
int lcd27_ = 34;
const byte ROWS = 4; //quattro righe
const byte COLS = 4; //quattro colonne
char UserInsert[4];
char keyInsert[13];
// Queste variabili servono come verifica del corretto inserimento del codice
int i = 0;
int j = 0;
int s = 0;
int x = 0;
bool Ciro = false;
bool Helena = false;
bool TastoPremuto = false;
int servo1Pin = 12;
int minUs = 0;
int maxUs = 8000;
// Codice segreto
const char UserCiro[4] = {'1','2','3','4'};
const char UserHelena[4] = {'4','3','2','1'};
char UserInserted[4] = {'1','2','3','4'};
const char codeCiro[13] = {'0','0','3','4','6','0','1','3','6','7','0','1','0'};
const char codeHelena[4] = {'0','0','3','4'};
char Keys[ROWS][COLS] = //creo la matrice dei tasti della tastiera.
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte colPins[4] = {5, 4, 3, 2}; // Pin a cui sono connesse le colonne
byte rowPins[4] = {9, 8, 7, 6}; // Pin a cui sono connesse le righe
Keypad keypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
void setup() {
char UserCiro[4] = "";
char UserHelena[4] = "";
char codeCiro[13] = "";
char codeHelena[13] = "";
int angolo = 0;
Serial.begin(9600);
servo1.attach(servo1Pin);
lcd25.init();
lcd25.backlight();
analogWrite(Contrasto_Display_pin, 80);
pinMode(Relay_Door, OUTPUT);
pinMode(1,INPUT_PULLUP);
Ciro = false;
Helena = false;
init_lcd_25();
Write_Panel_MARS();
TastoPremuto = false;
ServoInit ();
delay (3000);
//servo1.detach();
}
void loop() {
int Pulsante_pin = digitalRead(1);
/**
Ciro Capelletta 10/02/2023
Apertura porta con user e password V1.0
*/
//analogWrite(Contrasto_Display_pin,1);
if (Pulsante_pin == LOW) {
digitalWrite(Relay_Door,HIGH);
}else{
digitalWrite(Relay_Door,LOW);
}
Proc_User();
}
void init_lcd_25() {
lcd25.clear();
lcd25.setCursor(0, 0);
}
void ServoInit() {
servo1.write(0);
}
void Write_Panel_MARS() {
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting(Lcd25_riga1, 25);
lcd25.setCursor(0, 1);
typewriting(Lcd25_riga2, 25);
delay(300);
}
void Proc_User() {
char key = keypad.getKey();
TastoPremuto == false;
if (i == 0) {
Serial.println("Insert User Pin to verify...");
lcd25.clear(); // Pulisco il Display
lcd25.setCursor(0, 0); // Imposto il cursore alla colonna 0 , linea 0
typewriting("Insert User Pin", 25);
analogWrite(Contrasto_Display_pin, 80);
i++;
}
lcd25.setCursor(0, 1); // Imposto il cursore alla colonna 0 , linea 1
if (key != NO_KEY && j < 4) {
analogWrite(Contrasto_Display_pin, 90);
lcd25.setCursor(j, 1); // Imposto il cursore alla colonna (Valore di J) , linea 1
typewriting("*" , 25);
Serial.print("*");
//Serial.print(key);
UserInsert[j] = key;
UserInserted[j] = key;
j++;
if (j == 4) {
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("Push * to continue or #", 25);
lcd25.setCursor(0, 1);
typewriting("Or * # to skip", 25);
}
}
if (key == '*') {
Serial.println();
Serial.println("Verifyng the User Pin...");
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("Verifyng Pin", 25);
delay(1000);
Ciro = false;
Helena = false;
for (s = 0; s < 4; s++) {
if (UserInsert[s] == UserCiro[s]) {
x++;
if (x == 4) {
Ciro = true;
}
}
}
if (x < 4) {
for (s = 0; s < 4; s++) {
if (UserInsert[s] == UserHelena[s]) {
x++;
if (x == 4) {
Helena = true;
}
}
}
}
if (x == 4) {
//TODO possibili ulteriori implementazioni
x = 0;
i = 0;
j = 0;
if (Helena == true) {
Serial.println("The User Pin is correct, Hello Helena");
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("User Pin is OK", 25);
lcd25.setCursor(0, 1);
typewriting("Hello Helena", 25);
delay(1000);
PasswordHelena();
}
if (Ciro == true) {
Serial.println("The User Pin is correct, Hello Ciro");
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("User Pin is OK", 25);
lcd25.setCursor(0, 1);
typewriting("Hello Ciro", 25);
delay(500);
PasswordCiro();
}
} else {
Serial.println("The User Pin is incorrect, please retry");
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("The Pin is KO", 25);
lcd25.setCursor(0, 1);
typewriting("Repite please", 25);
delay(2000);
x = 0;
i = 0;
j = 0;
}
}
if (key == '#') {
x = 0;
i = 0;
j = 0;
digitalWrite(Relay_Door, LOW);
}
}
void PasswordCiro() {// LUNGHEZZA CODE = 13 CHAR
do {
char key = keypad.getKey();
if (i == 0) {
Serial.println("Insert User code to verify...");
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("Insert User code", 25);
i++;
}
if (key != NO_KEY && j < 13) {
Serial.print("*");
lcd25.setCursor(j, 1);
typewriting("*", 25);
//Serial.println(key);
keyInsert[j] = key;
j++;
if (j == 13) {
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("Push * please", 25);
}
}
if (key == '*') {
Serial.println();
Serial.println("Verifyng the User code...");
delay(1000);
for (s = 0; s < 13; s++) {
if (keyInsert[s] == codeCiro[s]) {
x++;
}
}
if (x == 13) {
Serial.println("The User code is correct,The door is opened");
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("User Code is OK", 25);
lcd25.setCursor(0, 1);
typewriting("Door Opened", 25);
Serial.println("The door is Opened");
digitalWrite(Relay_Door, HIGH);
servo1.attach(servo1Pin);
servo1.write(180);
lcd25.clear();
lcd25.setCursor(0, 1);
typewriting("Door Opened", 25);
for (int t = 5; t >= 0 ; t--) {
lcd25.setCursor(0, 0);
String timeout = (String) t;
delay (1000);
Serial.println(t);
typewriting(timeout, 25);
}
digitalWrite(Relay_Door, LOW);
servo1.write(0);
Serial.println("The door is Closed" );
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("Door Closed", 25);
delay (3000);
servo1.detach();
lcd25.clear();
lcd25.setCursor(0, 0);
analogWrite(Contrasto_Display_pin, 30);
typewriting("Push # Please", 25);
} else {
Serial.println("The User code is incorrect, please retry");
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("User code NOK", 25);
lcd25.setCursor(0, 1);
typewriting("Repite please", 25);
delay(2000);
lcd25.clear();
x = 0;
i = 0;
j = 0;
}
}
if (key == '#') {
x = 0;
i = 0;
j = 0;
x = 13;
digitalWrite(Relay_Door, LOW);
}
} while (x != 13);
}
void PasswordHelena() {// LUNGHEZZA CODE = 4 CHAR
do {
char key = keypad.getKey();
if (i == 0) {
Serial.println("Insert User code to verify...");
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("Insert User code", 25);
i++;
}
if (key != NO_KEY && j < 4) {
Serial.print("*");
lcd25.setCursor(j, 1);
typewriting("*", 25);
Serial.println(key);
keyInsert[j] = key;
j++;
if (j == 4) {
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("Push * please", 25);
}
}
if (key == '*') {
Serial.println();
Serial.println("Verifyng the User code...");
delay(1000);
for (s = 0; s < 4; s++) {
if (keyInsert[s] == codeHelena[s]) {
x++;
}
}
if (x == 4) {
Serial.println("The User code is correct,The door is opened");
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("User Code is OK", 25);
lcd25.setCursor(0, 1);
typewriting("Door Opened", 25);
Serial.println("The door is Opened");
digitalWrite(Relay_Door, HIGH);
//servo1.attach(servo1Pin);
servo1.write(180);
lcd25.clear();
lcd25.setCursor(0, 1);
typewriting("Door Opened", 25);
for (int t = 5; t >= 0 ; t--) {
lcd25.setCursor(0, 0);
String timeout = (String) t;
delay (1000);
Serial.println(t);
typewriting(timeout, 25);
}
digitalWrite(Relay_Door, LOW);
servo1.write(0);
Serial.println("The door is Closed" );
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("Door Closed", 25);
delay (3000);
//servo1.detach();
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("Push # Please", 25);
analogWrite(Contrasto_Display_pin, 30);
} else {
Serial.println("The User code is incorrect, please retry");
lcd25.clear();
lcd25.setCursor(0, 0);
typewriting("User code NOK", 25);
lcd25.setCursor(0, 1);
typewriting("Repite please", 25);
delay(2000);
lcd25.clear();
x = 0;
i = 0;
j = 0;
}
}
if (key == '#') {
x = 0;
i = 0;
j = 0;
x = 13;
digitalWrite(Relay_Door, LOW);
}
} while (x != 4);
}
void typewriting(String messaggio, int address) {
int lunghezza = messaggio.length();
if (address == 25) {
for (int i = 0; i < lunghezza; i++) {
lcd25.print (messaggio[i]);
//analogWrite(Contrasto_Display_pin,i*10);
delay (10);
}
}
}