#include "Ultrasonic.h"
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
Servo myservo;
Ultrasonic ultrasonic(13,12);
int distance;
const int buzzerPin = 11;
int Led_red=2 ;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adresse I2C et dimensions du LCD (changez selon votre écran)
void setup() {
Serial.begin(9600);
pinMode(Led_red ,OUTPUT);
myservo.attach(9);
lcd.begin(16,2);
}
void loop() {
// put your main code here, to run repeatedly:
distance = ultrasonic.read(CM);
Serial.print("Distance in CM:");
Serial.println(distance);
delay(1000);
if(distance < 100){
lcd.clear();
tone(buzzerPin, 400, 500);
digitalWrite(Led_red,HIGH);
lcd.print("OPEN");
lcd.setCursor(0, 1);
unlockDoor();
}
else {
lcd.clear();
noTone(buzzerPin); // Arrête le son
digitalWrite(Led_red,LOW);
lcd.print("Close");
lcd.setCursor(0, 1);
}
}
void unlockDoor() {
myservo.write(90); // Déverrouiller la porte
delay(5000);
myservo.write(0); // Revenir à la position initiale
delay(2000);
}