نادي التكنولوجيا

برمجة سيارة ذكية باستخدام مستشعر فوق صوتي وArduino

في عصر التكنولوجيا الحديثة، أصبحت السيارات الذكية جزءًا من حياتنا اليومية. يمكن للسيارات الذكية أن تتجنب العقبات، تتبع المسارات، وحتى تقود نفسها دون تدخل بشري. في هذا المقال، سنستعرض كيف يمكنك تصميم سيارة ذكية باستخدام لوحة Arduino ومُستشعر فوق صوتي (Ultrasonic Sensor).

المكونات المطلوبة:

  • لوحة Arduino Uno: اللوحة الرئيسية التي ستقوم بتنفيذ التعليمات البرمجية.
  • مستشعر فوق صوتي HC-SR04: يستخدم لإرسال وإستقبال الموجات الصوتية لتحديد المسافة بين السيارة والعقبات.
  • محركان DC: لتحريك السيارة للأمام وللخلف.
  • لوحة درايفر محرك L298N: لتزويد المحركات بالطاقة اللازمة للعمل.
  • بطارية 9V: لتوفير الطاقة للنظام.
  • عجلات صغيرة وشاسيه خشبي أو بلاستيكي: لتجميع السيارة.
  • أسلاك موصلة: لربط جميع المكونات معًا.


كيف يعمل النظام؟

يعمل النظام باستخدام مستشعر فوق صوتي لإرسال إشارات صوتية عالية التردد. عند اصطدام هذه الإشارات بجسم ما (مثل عقبة)، تعكس الإشارات وتصل مرة أخرى إلى المستشعر. باستخدام الزمن اللازم للإشارات للذهاب والإياب، يمكن حساب المسافة بين السيارة والعقبة باستخدام العلاقة الرياضية:

المسافة = (الزمن × سرعة الصوت) ÷ 2

حيث سرعة الصوت حوالي 343 متر/ثانية.

التوصيلات:

  • مستشعر فوق صوتي: ربط زين Trig بـ PIN 9 وزين Echo بـ PIN 10 في Arduino.
  • محركات DC ولوحة L298N: ربط أطراف المحركات مع مخارج IN1, IN2, IN3, IN4 في لوحة L298N.

الكود البرمجي:


// Defining Pins
const int trigPin = 9;   // Trigger pin for ultrasonic sensor
const int echoPin = 10;  // Echo pin for ultrasonic sensor

const int leftMotorSpeed = 5;  // PWM speed control for left motor
const int rightMotorSpeed = 6; // PWM speed control for right motor

const int leftMotorDir1 = 7;  // Direction control for left motor
const int leftMotorDir2 = 8;

const int rightMotorDir1 = 3;  // Direction control for right motor
const int rightMotorDir2 = 4;

void setup() {
  Serial.begin(9600); // Start serial communication
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Motor pins setup
  pinMode(leftMotorSpeed, OUTPUT);
  pinMode(rightMotorSpeed, OUTPUT);
  pinMode(leftMotorDir1, OUTPUT);
  pinMode(leftMotorDir2, OUTPUT);
  pinMode(rightMotorDir1, OUTPUT);
  pinMode(rightMotorDir2, OUTPUT);
}

void loop() {
  long duration, distance;

  // Send a 10 microsecond pulse to trigger the ultrasonic sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure the duration of the echo pulse
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance based on sound speed
  distance = duration * 0.034 / 2;

  if (distance < 20) { // If an obstacle is detected within 20 cm
    stopCar(); // Stop the car
    delay(1000); // Wait for 1 second
    reverseCar(); // Reverse the car
    delay(1000); // Wait for 1 second
    turnRight(); // Turn right to avoid the obstacle
    delay(500); // Wait for half a second
  } else {
    moveForward(); // Move forward if no obstacle is detected
  }

  delay(100); // Small delay between measurements
}

// Function to move the car forward
void moveForward() {
  analogWrite(leftMotorSpeed, 200); // Set left motor speed
  analogWrite(rightMotorSpeed, 200); // Set right motor speed
  digitalWrite(leftMotorDir1, HIGH);
  digitalWrite(leftMotorDir2, LOW);
  digitalWrite(rightMotorDir1, HIGH);
  digitalWrite(rightMotorDir2, LOW);
}

// Function to stop the car
void stopCar() {
  analogWrite(leftMotorSpeed, 0);
  analogWrite(rightMotorSpeed, 0);
}

// Function to reverse the car
void reverseCar() {
  analogWrite(leftMotorSpeed, 200);
  analogWrite(rightMotorSpeed, 200);
  digitalWrite(leftMotorDir1, LOW);
  digitalWrite(leftMotorDir2, HIGH);
  digitalWrite(rightMotorDir1, LOW);
  digitalWrite(rightMotorDir2, HIGH);
}

// Function to turn the car right
void turnRight() {
  analogWrite(leftMotorSpeed, 200);
  analogWrite(rightMotorSpeed, 0);
  digitalWrite(leftMotorDir1, HIGH);
  digitalWrite(leftMotorDir2, LOW);
  digitalWrite(rightMotorDir1, LOW);
  digitalWrite(rightMotorDir2, LOW);
}

شرح الكود:

  • قياس المسافة: يتم استخدام دالة pulseIn لقياس الزمن الذي تستغرقه الموجة الصوتية للذهاب والإياب.
  • التحكم في المحركات: يتم تحديد سرعة واتجاه المحركات باستخدام الدوال analogWrite وdigitalWrite.
  • التعامل مع العقبات: إذا كانت المسافة أقل من 20 سم، فإن السيارة تتوقف، ثم تتحرك إلى الخلف، وبعد ذلك تدور لليمين لتجنب العقبة.
إرسال تعليق (0)
أحدث أقدم