#include <Servo.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//---------------- LCD ----------------
LiquidCrystal_I2C lcd(0x27, 16, 2);
//---------------- Servo ----------------
Servo serrure;
const int servoPin = 11;
//---------------- Buzzer ----------------
const int buzzer = 12;
//---------------- Clavier 4x4 ----------------
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9,8,7,6};
byte colPins[COLS] = {5,4,3,2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//---------------- Mot de passe ----------------
String password = "2580";
String input = "";
int essais = 0;
void setup() {
lcd.init();
lcd.backlight();
serrure.attach(11);
serrure.write(0); // Porte verrouillée
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
lcd.setCursor(0,0);
lcd.print("Serrure");
lcd.setCursor(0,1);
lcd.print("Entrez Code");
}
void loop() {
char key = keypad.getKey();
if(key){
// Effacer
if(key=='C'){
input="";
lcd.clear();
lcd.print("Code efface");
delay(1000);
lcd.clear();
lcd.print("Entrez Code");
lcd.setCursor(0,1);
}
// Validation
else if(key=='D'){
if(input==password){
lcd.clear();
lcd.print("Acces Autorise");
serrure.write(90);
delay(5000);
serrure.write(0);
lcd.clear();
lcd.print("Porte Fermee");
essais=0;
}
else{
essais++;
lcd.clear();
lcd.print("Code Incorrect");
tone(buzzer,1000,300);
delay(1500);
if(essais>=3){
lcd.clear();
lcd.print("!!! ALARME !!!");
for(int i=0;i<15;i++){
tone(buzzer,1200);
delay(200);
noTone(buzzer);
delay(200);
}
essais=0;
}
}
input="";
delay(1000);
lcd.clear();
lcd.print("Entrez Code");
}
// Saisie des chiffres
else{
input += key;
lcd.setCursor(0,1);
for(int i=0;i<input.length();i++)
lcd.print("*");
}
}
}