Hi! I am very new to Arduino and I am trying to build an object detecting car that changes direction once it perceives an object <20cm from it. The issue I am having is that the car is halting midway most of the times even though there is no object infront of it. Also, it doesn't seem to turn that much.
My code:
#include <AFMotor.h>
#include <Servo.h>
#define TRIG_PIN A4
#define ECHO_PIN A5
#define SERVO_PIN 10 // Servo 1 on L293D board
// Initialize motors (M4 = Right, M1 = Left)
AF_DCMotor rightMotor(4);
AF_DCMotor leftMotor(1);
Servo scanServo;
void setup() {
Serial.begin(9600);
// Initialize motors
rightMotor.setSpeed(150);
leftMotor.setSpeed(150);
// Ultrasonic sensor setup
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Servo setup
scanServo.attach(SERVO_PIN);
scanServo.write(90); // Start at front position
delay(500);
}
float getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure pulse duration
long duration = pulseIn(ECHO_PIN, HIGH, 30000); // 30ms timeout
if (duration == 0) {
return -1; // No echo received
}
// Calculate distance in cm
float distance = (duration * 0.0343) / 2;
// Validate distance range
if (distance < 2 || distance > 400) {
return -1; // Invalid distance
}
return distance;
}
void moveForward() {
rightMotor.setSpeed(100); // Set right motor speed
leftMotor.setSpeed(100); // Set left motor speed
rightMotor.run(FORWARD); // Move right motor forward
leftMotor.run(FORWARD); // Move left motor forward
}
void moveBackward(int duration) {
rightMotor.setSpeed(100); // Set right motor speed
leftMotor.setSpeed(100); // Set left motor speed
rightMotor.run(BACKWARD); // Move right motor backward
leftMotor.run(BACKWARD); // Move left motor backward
delay(duration); // Move backward for the specified duration
stopMotors(); // Stop the motors after moving
}
void turnLeft() {
rightMotor.setSpeed(70); // Set right motor speed
leftMotor.setSpeed(70); // Set left motor speed
rightMotor.run(FORWARD); // Move right motor backward
leftMotor.run(BACKWARD); // Move left motor forward
delay(500);
stopMotors(); // Stop the motors after turning
}
void turnRight() {
rightMotor.setSpeed(70); // Set right motor speed
leftMotor.setSpeed(70); // Set left motor speed
rightMotor.run(BACKWARD); // Move right motor forward
leftMotor.run(FORWARD); // Move left motor backward
delay(500);
stopMotors(); // Stop the motors after turning
}
void stopMotors() {
rightMotor.run(RELEASE);
leftMotor.run(RELEASE);
}
void loop() {
float frontDistance = getDistance();
if (frontDistance == -1) {
Serial.println("Error: Invalid front distance reading");
stopMotors();
return;
}
Serial.print("Front Distance: ");
Serial.println(frontDistance);
if (frontDistance > 20) {
moveForward();
} else {
stopMotors();
delay(500);
// Move backward slightly
moveBackward(300); // Move backward for 500 milliseconds
// Scan left and right
scanServo.write(0);
delay(500);
float leftDistance = getDistance();
scanServo.write(180);
delay(500);
float rightDistance = getDistance();
scanServo.write(90);
delay(500);
if (leftDistance != -1 && rightDistance != -1) {
if (leftDistance > rightDistance) {
turnLeft();
} else {
turnRight();
}
} else {
Serial.println("Error: Invalid distance reading during scan");
}
}
delay(100);
}
I am using an Arduino UNO board with a motor driver L293d shield. I tried to skip soldering so I have connected the L293d with the UNO through Jumper Wires except the Analog pins which I have used for the ultrasonic sensor. The servo is direcvtly connected to the servo1 of the L293d. I am using 3*3.7V power supply and 2 dc motors.