Voici le code entier :
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Keypad.h>
#include <Wire.h>
#include "RotaryEncoder.h"
int signalPin = 12;
//alarm
int MIN = 5;
int SEC = 2;
//Mot de pass Alarm
#define Password_Length 6
char Data[Password_Length];
char Master[Password_Length] = "9521A";
char nul[Password_Length] = "";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
//config Digicode
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {22, 23, 24, 25};
byte colPins[COLS] = {26, 27, 28, 29};
//Minuteur
#define STEPS 1
#define POSMIN 0
#define POSMAX 30
//Config Rfid
#define SS_PIN 53
#define RST_PIN 49
//Config Entré Rfid
#define Max_Acces 3
byte Count_acces=0;
byte CodeVerif=0;
byte Code_Acces[4]={0xB7, 0x3B, 0xCD, 0xA7};
byte CodeVerif1=0;
byte Code_Acces1[4]={0xD7, 0x5A, 0x10, 0xAF};
//Bouton Entré/Sortir
#define OPEN A0
//Led/Aimant/Verouillage Porte
#define DOR 4
//led Rouge et Vert
#define AccesFlag_PIN 2
#define Gate_PIN 3
//+
byte ON=HIGH;
byte OFF=LOW;
//Config Librérie
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
LiquidCrystal_I2C lcd(0x27, 16, 2);
LiquidCrystal_I2C LCD(0x27, 20, 2);
RotaryEncoder encoder(4, 2);
// Init array that will store new NUID
byte nuidPICC[4];
int lastPos, newPos;
boolean buttonWasUp = true;
//Letre personalisé
byte customChar[8] = {
0b00010,
0b00100,
0b00000,
0b01110,
0b10001,
0b11111,
0b10000,
0b01110
};
byte customCharg[8] = {
0b00000,
0b00000,
0b01111, 0b10001,
0b10001,
0b01111,
0b00001,
0b01110
};
//Mot de pass Entré
#define Password_Length1 6
char Data1[Password_Length1];
char Master1[Password_Length1] = "9221A";
byte data_count1 = 0, master_count1 = 0;
#define Max_Acces1 5
byte Count_acces1=0;
//setup
void setup()
{
pinMode(6, OUTPUT);
encoder.setPosition(0 / STEPS);
pinMode(10, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(10, HIGH);
// Init RS232
Serial.begin(9600);
// LCD Init
lcd.init();
lcd.backlight();
lcd.clear();
lcd.createChar(0, customChar);
lcd.createChar(1, customCharg); // create a new custom character
lcd.setCursor(0,0);
verouillage(1);
// Init SPI bus
SPI.begin();
// Init MFRC522
rfid.PCD_Init();
// Init LEDs
pinMode(AccesFlag_PIN, OUTPUT);
pinMode(Gate_PIN, OUTPUT);
digitalWrite(AccesFlag_PIN, LOW);
digitalWrite(Gate_PIN, LOW);
pinMode(DOR, OUTPUT);
digitalWrite(DOR, ON);
pinMode(OPEN, INPUT);
}
void loop() {
customKey = customKeypad.getKey();
if (digitalRead(OPEN) == HIGH) {
// Affichage
Serial.println("L'interupteur est précé :");
Serial.println("Ouverture de la porte pendant:");
Serial.println("3 seconde.");
lcd.clear();
lcd.print(MIN);
lcd.print(" : ");
lcd.print(SEC);
delay(2000);
acces(1);
// Ouverture de la porte & Initialisation
}
else if (customKey){
if(customKey == '#'){
Serial.println("Code d'ouverture de la porte entré");
Serial.print("Le code utilisé est : ");
Serial.println(Data1);
}else{
Data1[data_count1] = customKey;
if (Data1[data_count1] == '*'){
clearData1();
verouillage(1);
Serial.println("Mot de pass D'ouverture éffacé");
}else{
Serial.println(Data1[data_count1]);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(Data1);
data_count1++;
}
}
}
if(data_count1 > Password_Length1-1){
clearData1();
verouillage(1);
Serial.println("Mot de passe d'ouverture trop long");
}
if(customKey == '#'){
if(!strcmp(Data1, Master1)){
Serial.println("Mot de passe d'ouverture correcte");
lcd.backlight();
acces(1);
clearData1();
Count_acces1=0;
}
else{
if(!strcmp(Data1, nul)){
Serial.println("Rien n'est entré");
clearData1();
Count_acces1=0;
}else{
Count_acces1+=1;
if(Count_acces1>=Max_Acces1){
lcd.clear();
// Dépassement des tentatives (clignotement infinie)
while(1){
if (digitalRead(OPEN) == HIGH){
lcd.backlight();
button();
break;
}
digitalWrite(AccesFlag_PIN, ON);
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Alarme! Alarme!");
if (digitalRead(OPEN) == HIGH){
lcd.backlight();
button();
break;
}
delay(200);
lcd.noBacklight();
digitalWrite(AccesFlag_PIN, OFF);
delay(200);
// Affichage
Serial.println("Alarme!");
if (digitalRead(OPEN) == HIGH){
lcd.backlight();
button();
break;
}
customKey = customKeypad.getKey();
if (customKey){
if(customKey == '#'){
Serial.println("Entré");
}else{
Data[data_count] = customKey;
if (Data[data_count] == '*'){
clearData();
lcd.setCursor(0,1);
lcd.print(" ");
Serial.println("clearData");
}else{
Serial.println(Data[data_count]);
lcd.setCursor(0,1);
lcd.print(Data);
data_count++;
}
}
}
if(data_count > Password_Length-1){
clearData();
lcd.setCursor(0,1);
lcd.print(" ");
Serial.println("Clé trop longue");
}
if(customKey == '#'){
if(!strcmp(Data, Master)){
Serial.println("Clé de deblockage correcte");
lcd.backlight();
verouillage(1);
clearData();
Count_acces=0;
break;
}
else{
if(!strcmp(Data, nul)){
Serial.println("Rien n'est entré");
}else{
Serial.println("Clé de deblockage incorrecte");
lcd.backlight();
alarmtime(300000);
lcd.setCursor(0,1);
lcd.print(" ");
}
}
clearData();
}
}
}
else{
Serial.println("Mot de passe d'ouverture incorrecte");
denny(1);
clearData1();
}
}
}
clearData();
}
else {
}
// Initialisé la boucle si aucun badge n'est présent
if ( !rfid.PICC_IsNewCardPresent())
return;
// Vérifier la présence d'un nouveau badge
if ( !rfid.PICC_ReadCardSerial())
return;
// Afffichage
Serial.println(F("Un badge est détecté"));
// Enregistrer l’ID du badge (4 octets)
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
// Vérification du code Rfid
CodeVerif= GetAccesState(Code_Acces,nuidPICC);
CodeVerif1= GetAccesState1(Code_Acces1,nuidPICC);
if (CodeVerif!=1)
{
if (CodeVerif1!=1)
{
Count_acces+=1;
if(Count_acces>=Max_Acces)
{
lcd.clear();
// Dépassement des tentatives (clignotement infinie)
while(1)
{
if (digitalRead(OPEN) == HIGH){
lcd.backlight();
button();
break;
}
digitalWrite(AccesFlag_PIN, ON);
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Alarme! Alarme!");
if (digitalRead(OPEN) == HIGH){
lcd.backlight();
button();
break;
}
delay(200);
lcd.noBacklight();
digitalWrite(AccesFlag_PIN, OFF);
delay(200);
// Affichage
Serial.println("Alarme!");
if (digitalRead(OPEN) == HIGH){
lcd.backlight();
button();
break;}
customKey = customKeypad.getKey();
if (customKey){
if(customKey == '#'){
Serial.println("Entré");
}else{
Data[data_count] = customKey;
if (Data[data_count] == '*'){
clearData();
lcd.setCursor(0,1);
lcd.print(" ");
Serial.println("clearData");
}else{
Serial.println(Data[data_count]);
lcd.setCursor(0,1);
lcd.print(Data);
data_count++;
}
}
}
if(data_count > Password_Length-1){
clearData();
lcd.setCursor(0,1);
lcd.print(" ");
Serial.println("Clé trop longue");
}
if(customKey == '#'){
if(!strcmp(Data, Master)){
Serial.println("Clé de deblockage correcte");
lcd.backlight();
verouillage(1);
clearData();
Count_acces=0;
break;
}
else{
if(!strcmp(Data, nul)){
Serial.println("Rien n'est entré");
}else{
Serial.println("Clé de deblockage incorrecte");
lcd.backlight();
alarmtime(300000);
lcd.setCursor(0,1);
lcd.print(" ");
}
}
clearData();
}
}
}
else
{
denny(1);
}
}
else
{
Serial.println("Le badge est le badge Bleu");
delay(1000);
acces(1);
}
}
else
{
Serial.println("Le badge est le badge Blanc");
delay(1000);
acces(1);
}
// Affichage de l'identifiant
Serial.println(" L'UID du tag est:");
for (byte i = 0; i < 4; i++)
{
Serial.print(nuidPICC[i], HEX);
Serial.print(" ");
}
Serial.println();
// Re-Init RFID
rfid.PICC_HaltA(); // Halt PICC
rfid.PCD_StopCrypto1(); // Stop encryption on PCD
}
byte GetAccesState(byte *CodeAcces,byte *NewCode)
{
byte StateAcces=0;
if ((CodeAcces[0]==NewCode[0])&&(CodeAcces[1]==NewCode[1])&&
(CodeAcces[2]==NewCode[2])&& (CodeAcces[3]==NewCode[3]))
return StateAcces=1;
else
return StateAcces=0;
}
byte GetAccesState1(byte *CodeAcces1,byte *NewCode)
{
byte StateAcces=0;
if ((CodeAcces1[0]==NewCode[0])&&(CodeAcces1[1]==NewCode[1])&&
(CodeAcces1[2]==NewCode[2])&& (CodeAcces1[3]==NewCode[3]))
return StateAcces=1;
else
return StateAcces=0;
}
//////////////////////////////////////////////////////////////////////////BUTTON///////////////////////////////////////////////////////////////////////////////////
void button(){
// Affichage
Serial.println("L'interupteur est précé :");
Serial.println("Ouverture de la porte pendant:");
Serial.println("3 seconde.");
// Ouverture de la porte & Initialisation
acces(1);
clearData();
clearData1();
Count_acces=0;
Count_acces1=0;
lcd.backlight();
}
//////////////////////////////////////////////////////////////////////////CLEARDATA///////////////////////////////////////////////////////////////////////////////////
void clearData(){
while(data_count !=0){
Data[data_count--] = 0;
}
return;
}
//////////////////////////////////////////////////////////////////////////CLEARDATA1///////////////////////////////////////////////////////////////////////////////////
void clearData1(){
while(data_count1 !=0){
Data1[data_count1--] = 0;
}
return;
}
////////////////////////////////////////////////////////////////////////////ACCES/////////////////////////////////////////////////////////////////////////////////////
void acces (int setDelay)
{
// Affichage
Serial.println("Ouverture de la porte");
// Ouverture de la porte & Initialisation
digitalWrite(DOR, OFF);
digitalWrite(Gate_PIN, ON);
lcd.clear();
lcd.print("Porte ouverte");
lcd.setCursor(0,1);
lcd.print("Bienvenue");
delay(3000);
digitalWrite(DOR, ON);
digitalWrite(Gate_PIN, OFF);
lcd.clear();
verouillage(1);
Count_acces=0;
}
////////////////////////////////////////////////////////////////////////////DENNY/////////////////////////////////////////////////////////////////////////////////////
void denny (int setDelay)
{
// Affichage
Serial.println("Code érroné");
lcd.clear();
lcd.print("Code ");
lcd.write((byte)0);
lcd.print("rron");
lcd.write((byte)0);
lcd.setCursor(0,1);
lcd.print("R");
lcd.write((byte)0);
lcd.print("essayer");
// Un seul clignotement: Code erroné
digitalWrite(AccesFlag_PIN, ON);
delay(5000);
lcd.clear();
verouillage(1);
digitalWrite(AccesFlag_PIN, OFF);
}
/////////////////////////////////////////////////////////////////////////VEROUILLAGE//////////////////////////////////////////////////////////////////////////////////
void verouillage (int setDelay)
{
lcd.setCursor(0,0);
lcd.print("Porte verouill");
lcd.write((byte)0);
lcd.setCursor(0,1);
lcd.print("Bad");
lcd.write((byte)1);
lcd.write((byte)0);
lcd.print(" pour entrer");
lcd.setCursor(0,0);
}
// digitalWrite(DOR, OFF);
// digitalWrite(Gate_PIN, ON);
// Count_acces=0;
// delay(3000);
// digitalWrite(DOR, ON);
// digitalWrite(Gate_PIN, OFF);
// delay(1000);
/////////////////////////////////////////////////////////////////////////VEROUILLAGE//////////////////////////////////////////////////////////////////////////////////
void alarm () {
if(Count_acces1>=Max_Acces1 || Count_acces>=Max_Acces){
lcd.clear();
// Dépassement des tentatives (clignotement infinie)
while(1){
if (digitalRead(OPEN) == HIGH){
lcd.backlight();
button();
break;
}
digitalWrite(AccesFlag_PIN, ON);
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Alarme! Alarme!");
if (digitalRead(OPEN) == HIGH){
lcd.backlight();
button();
break;
}
delay(200);
lcd.noBacklight();
digitalWrite(AccesFlag_PIN, OFF);
delay(200);
// Affichage
Serial.println("Alarme!");
if (digitalRead(OPEN) == HIGH){
lcd.backlight();
button();
break;
}
customKey = customKeypad.getKey();
if (customKey){
if(customKey == '#'){
Serial.println("Entré");
}else{
Data[data_count] = customKey;
if (Data[data_count] == '*'){
clearData();
lcd.setCursor(0,1);
lcd.print(" ");
Serial.println("clearData");
}else{
Serial.println(Data[data_count]);
lcd.setCursor(0,1);
lcd.print(Data);
data_count++;
}
}
}
if(data_count > Password_Length-1){
clearData();
lcd.setCursor(0,1);
lcd.print(" ");
Serial.println("Clé trop longue");
}
if(customKey == '#'){
if(!strcmp(Data, Master)){
Serial.println("Clé de deblockage correcte");
lcd.backlight();
verouillage(1);
clearData();
Count_acces=0;
break;
}else{
if(!strcmp(Data, nul)){
Serial.println("Rien n'est entré");
}else{
Serial.println("Clé de deblockage incorrecte");
lcd.backlight();
alarmtime(300000);
lcd.setCursor(0,1);
lcd.print(" ");
}
}
clearData();
}
}
}
}
/////////////////////////////////////////////////////////////////////////AlarmTime//////////////////////////////////////////////////////////////////////////////////
unsigned long timer;
byte w = 0;
void alarmtime (int setDelay){
//mise Du timer a 5min a chaque lancemant de alarmtime
int MIN = 5;
int SEC = 2;
LCD.backlight();
lcd.setCursor(0, 0);
LCD.print("Alarme! Alarme!");
lcd.setCursor(5, 1);
LCD.print(MIN);
LCD.print(" : ");
LCD.print(SEC);
encoder.tick();
newPos = encoder.getPosition() * STEPS;
if (newPos < POSMIN) {
encoder.setPosition(POSMIN / STEPS);
newPos = POSMIN;
}
else if (newPos > POSMAX) {
encoder.setPosition(POSMAX / STEPS);
newPos = POSMAX;
}
if (lastPos != newPos) {
MIN = newPos;
lastPos = newPos;
LCD.clear();
lcd.setCursor(0, 0);
LCD.print("Alarme! Alarme!");
lcd.setCursor(5, 1);
LCD.print(MIN);
LCD.print(" : ");
LCD.print(SEC);
}
boolean buttonIsUp = digitalRead(6);
if (buttonWasUp && !buttonIsUp && MIN > 0) {
delay(10);
buttonIsUp = digitalRead(6);
if (!buttonIsUp) {
if (SEC == 0) { SEC = 60; MIN = MIN - 1; }
if (MIN < 0 ) { MIN = 0; }
digitalWrite(10, LOW);
w = 1;
}
}
buttonWasUp = buttonIsUp;
while (w == 1 ) {
if (millis() - timer > 993) {
timer = millis();
SEC = SEC - 1;
if (SEC == 0 && MIN == 0) {
lastPos = 0; newPos = 0; MIN = 0; SEC = 0;
LCD.clear();
lcd.setCursor(0, 0);
LCD.print("Alarme! Alarme!");
lcd.setCursor(5, 1);
LCD.print(MIN);
LCD.print(" : ");
LCD.print(SEC);
digitalWrite(10, HIGH);
tone(12, 100);
delay(500);
noTone(12);
w = 0;
}
if (SEC == 0 && w==1) {
SEC = 59; MIN = MIN - 1;
if (MIN < 0 ) { MIN = 0; }
}
if (w == 1) {
LCD.clear();
lcd.setCursor(0, 0);
LCD.print("Alarme! Alarme!");
lcd.setCursor(5, 1);
LCD.print(MIN);
LCD.print(" : ");
LCD.print(SEC);
}
}
buttonIsUp = digitalRead(6);
if (buttonWasUp && !buttonIsUp) {
delay(10);
buttonIsUp = digitalRead(6);
if (!buttonIsUp) {
lastPos = 0; newPos = 0; MIN = 0; SEC = 0;
LCD.clear();
lcd.setCursor(0, 0);
LCD.print("Alarme! Alarme!");
lcd.setCursor(5, 1);
LCD.print(MIN);
LCD.print(" : ");
LCD.print(SEC);
digitalWrite(10, HIGH);
w = 0;
}
}
buttonWasUp = buttonIsUp;
}
}