#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define TRIG_PIN 12
#define ECHO_PIN 13
#define BUZZER_PIN 3
#define LED_PIN 4
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define SAFE_DISTANCE 50 // الحد الأدنى للمسافة الآمنة (بالسنتيمتر)
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
lcd.init();
lcd.backlight();
welcomeMessage(); // تشغيل الرسالة الترحيبية
welcomeTone(); // تشغيل النغمة الترحيبية
}
void loop() {
int distance = getDistance();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Distance: “);
lcd.print(distance);
lcd.print(” cm”);
if (distance < SAFE_DISTANCE) {
lcd.setCursor(0, 1);
lcd.print(“TOO CLOSE!”); // تنبيه اقتراب
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 2000);
delay(500);
noTone(BUZZER_PIN);
} else {
lcd.setCursor(0, 1);
lcd.print(“Safe Distance.”); // المسافة آمنة
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
}
delay(2000);
}
// حساب المسافة باستخدام حساس الموجات فوق الصوتية
int getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
int distance = duration * 0.034 / 2;
return distance;
}
// عرض رسالة ترحيبية مع تمرير النص
void welcomeMessage() {
String message = “Welcome! Infection Prevention System “;
for (int i = 0; i < message.length() – 15; i++) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message.substring(i, i + 16));
delay(250);
}
delay(1000);
}
// تشغيل نغمة ترحيبية محسنة
void welcomeTone() {
tone(BUZZER_PIN, 800);
delay(300);
noTone(BUZZER_PIN);
delay(100);
tone(BUZZER_PIN, 1000);
delay(300);
noTone(BUZZER_PIN);
delay(100);
tone(BUZZER_PIN, 1200);
delay(300);
noTone(BUZZER_PIN);
delay(100);
}