Skip to content

Commit e0c684f

Browse files
authored
Create Jordan
#include <Servo.h> const int trigPin = 9; const int echoPin = 10; const int motorLeft = 5; const int motorRight = 6; const int servoPin = 3; const int angleCenter = 90; const int angleLeft = 150; const int angleRight = 30; Servo servo; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(motorLeft, OUTPUT); pinMode(motorRight, OUTPUT); Serial.begin(9600); servo.attach(servoPin); servo.write(angleCenter); } long getDistance() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); return pulseIn(echoPin, HIGH, 20000) * 0.034 / 2; // Timeout added } long getAverageDistance() { long total = 0; for (int i = 0; i < 5; i++) { total += getDistance(); delay(10); } return total / 5; } int scanAngle(int angle) { servo.write(angle); delay(300); // انتظار حتى يثبت السرفو return getAverageDistance(); } void loop() { long distance = getAverageDistance(); Serial.print("Center: "); Serial.println(distance); if (distance == 0 || distance > 300) { digitalWrite(motorLeft, LOW); digitalWrite(motorRight, LOW); return; } if (distance < 20) { // توقف مؤقت وتراجع بسيط digitalWrite(motorLeft, LOW); digitalWrite(motorRight, LOW); delay(200); // مسح الاتجاهات int left = scanAngle(angleLeft); int right = scanAngle(angleRight); servo.write(angleCenter); delay(200); // العودة للمنتصف Serial.print("Left: "); Serial.print(left); Serial.print(" | Right: "); Serial.println(right); // اتخاذ القرار if (left > right) { // انعطاف لليسار digitalWrite(motorLeft, LOW); digitalWrite(motorRight, HIGH); delay(500); } else { // انعطاف لليمين digitalWrite(motorLeft, HIGH); digitalWrite(motorRight, LOW); delay(500); } } else { // السير للأمام digitalWrite(motorLeft, HIGH); digitalWrite(motorRight, HIGH); } delay(50); }
1 parent afbbdad commit e0c684f

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

Jordan

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <Servo.h>
2+
3+
const int trigPin = 9;
4+
const int echoPin = 10;
5+
const int motorLeft = 5;
6+
const int motorRight = 6;
7+
const int servoPin = 3;
8+
9+
const int angleCenter = 90;
10+
const int angleLeft = 150;
11+
const int angleRight = 30;
12+
13+
Servo servo;
14+
15+
void setup() {
16+
pinMode(trigPin, OUTPUT);
17+
pinMode(echoPin, INPUT);
18+
pinMode(motorLeft, OUTPUT);
19+
pinMode(motorRight, OUTPUT);
20+
Serial.begin(9600);
21+
22+
servo.attach(servoPin);
23+
servo.write(angleCenter);
24+
}
25+
26+
long getDistance() {
27+
digitalWrite(trigPin, LOW);
28+
delayMicroseconds(2);
29+
digitalWrite(trigPin, HIGH);
30+
delayMicroseconds(10);
31+
digitalWrite(trigPin, LOW);
32+
return pulseIn(echoPin, HIGH, 20000) * 0.034 / 2; // Timeout added
33+
}
34+
35+
long getAverageDistance() {
36+
long total = 0;
37+
for (int i = 0; i < 5; i++) {
38+
total += getDistance();
39+
delay(10);
40+
}
41+
return total / 5;
42+
}
43+
44+
int scanAngle(int angle) {
45+
servo.write(angle);
46+
delay(300); // انتظار حتى يثبت السرفو
47+
return getAverageDistance();
48+
}
49+
50+
void loop() {
51+
long distance = getAverageDistance();
52+
Serial.print("Center: "); Serial.println(distance);
53+
54+
if (distance == 0 || distance > 300) {
55+
digitalWrite(motorLeft, LOW);
56+
digitalWrite(motorRight, LOW);
57+
return;
58+
}
59+
60+
if (distance < 20) {
61+
// توقف مؤقت وتراجع بسيط
62+
digitalWrite(motorLeft, LOW);
63+
digitalWrite(motorRight, LOW);
64+
delay(200);
65+
66+
// مسح الاتجاهات
67+
int left = scanAngle(angleLeft);
68+
int right = scanAngle(angleRight);
69+
servo.write(angleCenter);
70+
delay(200); // العودة للمنتصف
71+
72+
Serial.print("Left: "); Serial.print(left);
73+
Serial.print(" | Right: "); Serial.println(right);
74+
75+
// اتخاذ القرار
76+
if (left > right) {
77+
// انعطاف لليسار
78+
digitalWrite(motorLeft, LOW);
79+
digitalWrite(motorRight, HIGH);
80+
delay(500);
81+
} else {
82+
// انعطاف لليمين
83+
digitalWrite(motorLeft, HIGH);
84+
digitalWrite(motorRight, LOW);
85+
delay(500);
86+
}
87+
} else {
88+
// السير للأمام
89+
digitalWrite(motorLeft, HIGH);
90+
digitalWrite(motorRight, HIGH);
91+
}
92+
93+
delay(50);
94+
}

0 commit comments

Comments
 (0)