// Librairies à utiliser
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//#include "pitches.h"
const byte ROWS = 4;
const byte COLS = 4;
//Paramétrage du clavier
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //three columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {14, 27, 26, 25}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {33, 32, 18, 19}; //connect to the column pinouts of the keypad
uint8_t LCD_CursorPosition = 0;
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
const int ledvertePin = 15;
const int ledrougePin = 13;
const String password = "1605"; // change your password here----------------------------------------------------------------------------------------password-------------------------------
String input_password;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
pinMode(ledrougePin, OUTPUT);
pinMode(ledvertePin, OUTPUT);
// Lancement de la connexion série avec l'ESP32
Serial.begin(115200);
input_password.reserve(32); // maximum input characters is 33, change if needed
}
//===================================================================================================sous-routines
void good() { //routine si code exact
digitalWrite(ledvertePin, HIGH);
digitalWrite(ledrougePin, LOW);
lcd.setCursor(0, 1); // (ligne, colonne)
// print message
lcd.clear();
lcd.print(" Code Valide ");
lcd.setCursor(1, 1);
lcd.print(" Action OK ");
delay (2000);
delay(5000);
digitalWrite(ledvertePin, LOW);
lcd.setCursor(3, 1);
// print message
lcd.print(" ");
}
//================================================================================================================
void loop() { // Boucle d'attente de frappe des touches et de validation du code
// set cursor to first column, first row
lcd.setCursor(1, 0);
// print message
lcd.print("Entrez le Code");
lcd.setCursor(0, 1);
// print message
lcd.print(" Validez par #");
char key = keypad.getKey();
if (key) {
Serial.println(key);
digitalWrite(ledrougePin, HIGH);
//digitalWrite(12, HIGH);
tone(4, 200, 700);// tonalite grave (4=d4, 200hz, 700ms)
delay(500);
digitalWrite(ledrougePin, LOW);
if (key == '*') {
input_password = ""; // clear input password
} else if (key == '#') {
if (password == input_password) {
Serial.println("password is correct");
// DO YOUR WORK HERE
good(); // appel de la fonction good()si le code est bon
} else {
Serial.println("password is incorrect, try again");
digitalWrite(ledrougePin, HIGH);
}
input_password = ""; // clear input password
} else {
input_password += key; // append new character to input password string
}
}
}