#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Ultrasonic.h>
//const int trigPin = 6;
//const int echoPin = A4;
Ultrasonic ultrasonic=(A4,6);
const int ledPin = 3;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const byte ROWS = 4;
const byte COLS = 4;
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {2, 4, 5, 13};
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const String correctPassword = "1234";
String inputPassword = "";
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("System Ready");
delay(2000);
lcd.clear();
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
//pinMode(trigPin, OUTPUT);
//pinMode(echoPin, INPUT);
}
/*float measureDistance() {
float distance=
return distance;
}*/
void loop() {
float distance = ultrasonic.read();
// Afficher la distance
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
delay(500);
lcd.setCursor(0, 0);
if (distance <= 50 ) {
lcd.clear();
lcd.print("Enter Code:");
while (inputPassword.length() <= 4) {
char key = keypad.getKey();
if (key) {
if (key == '*') {
inputPassword = "";
lcd.clear();
lcd.print("Cleared");
delay(1000);
lcd.clear();
lcd.print("Enter Code:");
} else if (key == '#') {
lcd.clear();
if (inputPassword == correctPassword) {
lcd.print("Code Correct !");
digitalWrite(ledPin, HIGH);
delay(3000);
digitalWrite(ledPin, LOW);
lcd.clear();
} else {
lcd.print("Code Incorrect");
digitalWrite(ledPin, LOW);
delay(2000);
lcd.clear();
lcd.print("Enter Code:");
}
inputPassword = "";
} else if (isDigit(key) && inputPassword.length() < 4) {
inputPassword += key;
lcd.print("*");
delay(300);
}
}
}
}else {lcd.print("trop loin");}
}