forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJordan
More file actions
94 lines (79 loc) · 2.07 KB
/
Copy pathJordan
File metadata and controls
94 lines (79 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#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);
}