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);
}