The motor of this code are not working

#include <Servo.h>
#include <NewPing.h>

Servo servo;
NewPing sonar(A5, A4); // Trigger pin, Echo pin
const int servoPin = 9; // Servo motor control pin
const int safeDistance = 10; // Safe distance in centimeters

const int leftMotor1 = 6; // Change to your desired pin for left motor control pin 1
const int leftMotor2 = 7; // Change to your desired pin for left motor control pin 2
const int rightMotor1 = 8; // Change to your desired pin for right motor control pin 1
const int rightMotor2 = 5; // Change to your desired pin for right motor control pin 2

int initialAngle = 180; // Initial servo angle

void setup() {
servo.attach(servoPin);
servo.write(initialAngle); // Start with the servo pointing straight ahead

// Set motor pins as outputs
pinMode(leftMotor1, OUTPUT);
pinMode(leftMotor2, OUTPUT);
pinMode(rightMotor1, OUTPUT);
pinMode(rightMotor2, OUTPUT);
}

void loop() {
int distance = sonar.ping_cm();

if (distance <= safeDistance) {
// If an object is detected within the safe distance, rotate the servo to find a clear path
int clearPathAngle = findClearPath();

// Rotate back to the initial position
servo.write(initialAngle);

// Implement motor control based on the clear path angle
if (clearPathAngle < 90) {
  // Turn right
  digitalWrite(leftMotor1, HIGH);
  digitalWrite(leftMotor2, LOW);
  digitalWrite(rightMotor1, LOW);
  digitalWrite(rightMotor2, LOW);
} else if (clearPathAngle > 90) {
  // Turn left
  digitalWrite(leftMotor1, LOW);
  digitalWrite(leftMotor2, LOW);
  digitalWrite(rightMotor1, HIGH);
  digitalWrite(rightMotor2, LOW);
} else {
  // Move forward
  digitalWrite(leftMotor1, HIGH);
  digitalWrite(leftMotor2, LOW);
  digitalWrite(rightMotor1, HIGH);
  digitalWrite(rightMotor2, LOW);
}

} else {
// If there is no object within the safe distance, move forward
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, HIGH);
digitalWrite(rightMotor2, LOW);
}
}

int findClearPath() {
int clearPathAngle = -1; // Initialize to an invalid value
int minDistance = 9999; // Initialize to a high value

for (int angle = 0; angle <= 180; angle += 90) {
servo.write(angle); // Rotate the servo in 90-degree intervals
delay(1000); // Delay for a moment to allow the servo to settle
int distance = sonar.ping_cm();

if (distance > safeDistance) {
  // If a clear path is found, record the clear path angle
  clearPathAngle = angle;
  break;  // Exit the loop when a clear path is found
} else if (distance < minDistance) {
  // If no clear path is found, record the minimum distance
  minDistance = distance;
}

}

if (clearPathAngle == -1) {
// If no clear path is found, return the servo to its initial position
clearPathAngle = initialAngle;
}

return clearPathAngle;
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Welcome to the forum.

The phrase "not working" is a worthless statement as it conveys no useful information.

What is the code supposed to do? What does the code actually do?

Please post a schematic or wiring diagram. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Please format your post and post the code properly. Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.

My projects name is object avoiding car, it will try to avoid an entity or object by going right or left, I have used the following components for it: Arduino UNO R3, L293D Motor Driver Shield, HC-SR04 Sonar Sensor, Servo motor, 4gear motors (bo motors), 218650 Li-on 3.7v 3800 mAh battery. In my code the final output is that the Sonar sensor is detecting and making the servo motor react to it (means the servo motor is working), but the problem is my 4 gear or bo motors aren't working.

Open the serial port and put in some serial prints to monitor program flow and variable values. Is what is actually happening what you expect?

May we see a schematic, please?

Please fix your code in the original post by formatting and posting as requested in the (linked) forum guidelines. I really does make it easier for us to help you if we can easily copy and paste your code to a text editor or the IDE for examination and/or verification.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.