Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#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);
} else {
// السير للأمام
digitalWrite(motorLeft, HIGH);
digitalWrite(motorRight, HIGH);
}
delay(50);
}
At the moment we are not accepting contributions to the repository.