Arduino object avoiding car

Hello everyone this is my first time using guard do you know and I'm making objects, avoidance, robotic car, so I plugged on everything
The car is going for 40 or 50 cm and then stops then it goes forward then stops again. Also it doesn't stop when is there is a object in front of it. I think there is an issue related to programming codes or the battery because motors are not going so fast and car is not gone for what constantly stops and moves every 40 or 50 cm so there is no constantly moving
attached you the code and design and it's also four motors car I would appreciate it if you can help me solve like this issue, thank you very much

code:

/*Obstacle avoidance robot with three ultrasonic sensors
 * http://srituhobby.com
 */
//Include the motor driver library
#include <AFMotor.h>
//Define the sensor pins
#define S1Trig A0
#define S2Trig A1
#define S3Trig A2
#define S1Echo A3
#define S2Echo A4
#define S3Echo A5
//Set the speed of the motors
#define Speed 160

//Create objects for the motors
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

void setup() {
  Serial.begin(9600);
  //Set the Trig pins as output pins
  pinMode(S1Trig, OUTPUT);
  pinMode(S2Trig, OUTPUT);
  pinMode(S3Trig, OUTPUT);
  //Set the Echo pins as input pins
  pinMode(S1Echo, INPUT);
  pinMode(S2Echo, INPUT);
  pinMode(S3Echo, INPUT);
  //Set the speed of the motors
  motor1.setSpeed(Speed);
  motor2.setSpeed(Speed);
  motor3.setSpeed(Speed);
  motor4.setSpeed(Speed);
}

void loop() {
  int centerSensor = sensorTwo();
  int leftSensor = sensorOne();
  int rightSensor = sensorThree();
// Check the distance using the IF condition
  if (8 >= centerSensor) {
    Stop();
    Serial.println("Stop");
    delay(1000);
    if (leftSensor > rightSensor) {
      left();
      Serial.println("Left");
      delay(500);
    } else {
      right();
      Serial.println("Right");
      delay(500);
    }
  }
  Serial.println("Forward");
  forward();
}

//Get the sensor values
int sensorOne() {
  //pulse output
  digitalWrite(S1Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(S1Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S1Trig, LOW);

  long t = pulseIn(S1Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}

//Get the sensor values
int sensorTwo() {
  //pulse output
  digitalWrite(S2Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(S2Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S2Trig, LOW);

  long t = pulseIn(S2Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}

//Get the sensor values
int sensorThree() {
  //pulse output
  digitalWrite(S3Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(S3Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S3Trig, LOW);

  long t = pulseIn(S3Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}

/*******************Motor functions**********************/
void forward() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void left() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void right() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}
void Stop() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

except for the schematic, please post that.

Did you do any testing of individual subsections of the device with test sketches before putting it all together into one big sketch? For example, test one motor, test one sensor by themselves and no additional code?

I did test only motors they are working individually, but I didn't test other parts individually because as I know they need new programming if I try to test each ultrasonic sensor and I am not very good at it. I mean I cannot program to test each one the main problem is that the car is stopping and then going forward and I'm not sure what's this issue related to and also the sensors sometimes working and sometimes not working I think there is a problem with the programming codes
I hope someone can help me program four motors with a three ultrasonic sensors for the design shown

current code is

/*Obstacle avoidance robot with three ultrasonic sensors
 * http://srituhobby.com
 */
//Include the motor driver library
#include <AFMotor.h>
//Define the sensor pins
#define S1Trig A0
#define S2Trig A1
#define S3Trig A2
#define S1Echo A3
#define S2Echo A4
#define S3Echo A5
//Set the speed of the motors
#define Speed 160

//Create objects for the motors
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

void setup() {
  Serial.begin(9600);
  //Set the Trig pins as output pins
  pinMode(S1Trig, OUTPUT);
  pinMode(S2Trig, OUTPUT);
  pinMode(S3Trig, OUTPUT);
  //Set the Echo pins as input pins
  pinMode(S1Echo, INPUT);
  pinMode(S2Echo, INPUT);
  pinMode(S3Echo, INPUT);
  //Set the speed of the motors
  motor1.setSpeed(Speed);
  motor2.setSpeed(Speed);
  motor3.setSpeed(Speed);
  motor4.setSpeed(Speed);
}

void loop() {
  int centerSensor = sensorTwo();
  int leftSensor = sensorOne();
  int rightSensor = sensorThree();
// Check the distance using the IF condition
  if (8 >= centerSensor) {
    Stop();
    Serial.println("Stop");
    delay(1000);
    if (leftSensor > rightSensor) {
      left();
      Serial.println("Left");
      delay(500);
    } else {
      right();
      Serial.println("Right");
      delay(500);
    }
  }
  Serial.println("Forward");
  forward();
}

//Get the sensor values
int sensorOne() {
  //pulse output
  digitalWrite(S1Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(S1Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S1Trig, LOW);

  long t = pulseIn(S1Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}

//Get the sensor values
int sensorTwo() {
  //pulse output
  digitalWrite(S2Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(S2Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S2Trig, LOW);

  long t = pulseIn(S2Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}

//Get the sensor values
int sensorThree() {
  //pulse output
  digitalWrite(S3Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(S3Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S3Trig, LOW);

  long t = pulseIn(S3Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}

/*******************Motor functions**********************/
void forward() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void left() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void right() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}
void Stop() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

Again, you did not post any wiring diagram or schematic.

You say you lack the skill to even test a sub part of the device. Does that mean you want someone to code for you? That is not what this forum is for. It's a self help forum, we give advice and support to people who are coding.

Actually, please have a look at the example sketches that accompany the libraries that support the motor, the sensors, and whatever else. Usually those sketches include one that needs only a line or two changed, if at all, to work as an effective test sketch. Try that, and report the results.

We are left guessing what you don't understand, in order to do it yourself. So you have to ask specific questions about, for example, the sketch. Like, why does this do, or what does this line mean, etc.

You have described performance issues. You started to explain that, but some additional details would really help. You mentioned battery but we have no idea what kind it is, or how it is connected to the circuit. The code is, I assume, from a kit? Did the kit offer specifications or video demos that contradict the limited performance in your view?

The code references certain ports, to troubleshoot the code you really need the diagram to understand what ports in code correspond to what hardware functions. You also need that information to test the example sketches.

Thoroughly double check all your wiring. Triple check, if you already did it twice!

Some of the behaviours that you describe do actually match closely what you would expect from the code. Have a look, understanding that a delay(1000) is a one second wait.

  if (8 >= centerSensor) {
    Stop();
    Serial.println("Stop");
    delay(1000);
    if (leftSensor > rightSensor) {
      left();
      Serial.println("Left");
      delay(500);
    } else {
      right();
      Serial.println("Right");
      delay(500);

It does appear to have very primitive directives. On the other hand, the failure to stop at an obstacle is more of a mystery. I think you're right to suspect the code, on the other hand even badly written code can work, and ship out the door and few people can tell. I would be suspicious that it is on the level of "demo code" and not expect too much from it. I see what looks like a Mega and a Motor Shield, that's not a bad starting point and it's likely you could learn from the working example code, and using the demo code more as a "hint", and produce something much better.

All the more importance of trying the example sketches. Since those are much more easily understood and known to be reliable. If there is hardware trouble, they will help show them up.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

thanks for the advises and for your kindness you helped me a lot Thank you again.

hello everyone, I'm having an issue related to my object avoidance car project when I plug the Arduino board it works fast and.
stable but when I plug it on the pottery which is 18650, 3.7 V. There is a delay and also the board led(L) becomes blink slowly, and there is a motor voice which is very low and the micro servo motor moves very very slow
Is this issue related to the batteries or the board itself or the programming? Because I'm lost.
What alternative batteries can I use so it becomes moving faster just like the USB plugged in
I am very beginner in Arduino projects
I will be very grateful if someone can guide me or help me and I will appreciate it attached you the card photo
Thanks very much

@thulfiqarr,

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

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