Skip to content

Commit d9d7a0e

Browse files
authored
Create Jordan
#include <Servo.h> const int trigPin = 9; const int echoPin = 10; const int motorLeft = 5; const int motorRight = 6; Servo servo; int angleCenter = 90; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(motorLeft, OUTPUT); pinMode(motorRight, OUTPUT); Serial.begin(9600); servo.attach(3); servo.write(angleCenter); } long getDistance() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); return pulseIn(echoPin, HIGH) * 0.034 / 2; } 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.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(150); int right = scanAngle(30); servo.write(angleCenter); delay(100); if (left > right) { // انعطاف لليسار digitalWrite(motorLeft, HIGH); digitalWrite(motorRight, LOW); } else { // انعطاف لليمين digitalWrite(motorLeft, LOW); digitalWrite(motorRight, HIGH); } delay(500); // وقت الانعطاف } else { // حركة أمامية digitalWrite(motorLeft, HIGH); digitalWrite(motorRight, HIGH); } delay(50); // تخفيف الضغط على الحلقة }
1 parent 12995df commit d9d7a0e

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

Jordan

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

0 commit comments

Comments
 (0)