#include <Servo.h>
#include <Ultrasonic.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const int ledv=6, ledj=7,ledr=8,buzz=12,motorBar=11,SG=9,SD=10,trig1=2,echo1=3,trig2=4,echo2=5;
Ultrasonic ultrasonic1(trig1, echo1);
Ultrasonic ultrasonic2(trig2,echo2);
Servo Sg;
Servo Sd;
Servo Barr;
int distance1,distance2;
float getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2; // حساب المسافة بالسنتيمترات
}
void setup() {
Serial.begin(9600);
pinMode(ledv,OUTPUT);
pinMode(ledj,OUTPUT);
pinMode(ledr,OUTPUT);
pinMode(buzz,OUTPUT);
Sd.attach(SD);
Sd.write(180);
Sg.attach(SG);
Sg.write(85);
Barr.attach(motorBar);
Barr.write(90);
pinMode(trig1, OUTPUT);
pinMode(echo1, INPUT);
pinMode(trig2, OUTPUT);
pinMode(echo2, INPUT);
lcd.init();
lcd.backlight();
lcd.print("DIB:salem alicom");
}
void loop() {
digitalWrite(ledv,HIGH);
digitalWrite(ledr,LOW);
distance1 = ultrasonic1.read();
distance2 = ultrasonic2.read();
Serial.print("distance 1 = ");
Serial.print(distance1);
Serial.print("\n");
Serial.print("distance 2 = ");
Serial.print(distance2);
Serial.print("\n");
float distance1 = getDistance(trig1, echo1); // قراءة المسافة من الحساس 1
float distance2 = getDistance(trig2, echo2); // قراءة المسافة من الحساس 2
lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print(distance1);
lcd.print(distance2);
lcd.print(" cm");
if (distance1<10 && distance1 >3){
for(int i =0 ; i<10; i++){
tone(buzz, 1000); // Send 1KHz sound signal...
delay(500); // ...for 1 sec
noTone(buzz); // Stop sound...
delay(100);
}
digitalWrite(ledr,HIGH);
digitalWrite(ledv,LOW);
delay(2000);
Barr.write(0);
delay(5000);
Sd.attach(SD);
Sg.attach(SG);
for (int pos = 85; pos >= 0; pos -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
Sg.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 15 ms for the servo to reach the position
}
delay(2000);
for (int pos = 180; pos >= 90; pos -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
Sd.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 15 ms for the servo to reach the position
}
delay(10000);
for (int pos = 90; pos <= 180; pos += 1) { // goes from 180 degrees to 0 degrees
Sd.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 15 ms for the servo to reach the position
}
delay(2000);
for (int pos = 0; pos <= 85; pos += 1) { // goes from 180 degrees to 0 degrees
Sg.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 15 ms for the servo to reach the position
}
Barr.write(90);
delay (2000);
digitalWrite(ledv,HIGH);
digitalWrite(ledr,LOW);
}
else {
Sd.detach();
Sg.detach();
}
if (distance2<10 && distance2 >3){
for(int i =0 ; i<10; i++){
tone(buzz, 1000); // Send 1KHz sound signal...
delay(500); // ...for 1 sec
noTone(buzz); // Stop sound...
delay(100);
}
digitalWrite(ledv,LOW);
digitalWrite(ledr,HIGH);
delay(2000);
Barr.write(0);
delay(5000);
Sd.attach(SD);
Sg.attach(SG);
for (int pos = 85; pos >= 0; pos -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
Sg.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 15 ms for the servo to reach the position
}
delay(2000);
for (int pos = 180; pos >= 90; pos -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
Sd.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 15 ms for the servo to reach the position
}
delay(10000);
for (int pos = 90; pos <= 180; pos += 1) { // goes from 180 degrees to 0 degrees
Sd.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 15 ms for the servo to reach the position
}
delay(2000);
for (int pos = 0; pos <= 85; pos += 1) { // goes from 180 degrees to 0 degrees
Sg.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 15 ms for the servo to reach the position
}
Barr.write(90);
delay(2000);
digitalWrite(ledv,HIGH);
digitalWrite(ledr,LOW);
}
else {
Sd.detach();
Sg.detach();
}
}