VKmaker New Avoidance tracking Motor Smart Robot Car

Hi folks
I have a car that uses 2 DC motors, an Arduino Uno with a shield and a L298H motor controller. In addition I am using a 9V DC power supply and a toggle switch to turn car off and on manually. I also have an ultrasonic sensor attached so it moves forward Using an sg90 servo motor to move sensor attached to arm of servo) until the sensor sense something is too close, backs up and then continues to move forward and repeats again.

My issue is that the car will work sometimes and not other times when I power it on. My servo and ultrasonic sensor work perfectly because my ultrasonic sensor continues to rotate but my two DC motors stop for some reason so the car stops. Sometimes it will start working again, sometimes one DC motor will work so the car spins in circles and the other doesn't work it's just not consistent performance from the DC motors and I am wondering if anyone has any ideas...

here is the amazon link this car so if you want a closer look at the actual components of the car:

here is the PDF instructions link that come with the car:

Here is my code that I am using for this car:

#include <Servo.h>
Servo servo;
// Ultrasonic Module pins
const int trigPin = 13; // 10 microsecond high pulse causes chirp , wait 50 us const int echoPin = 12; // Width of high pulse indicates distance
// Servo motor that aims ultrasonic sensor .
const int echoPin=12; //Width of high pulse indicates distance
const int servoPin = 11; // PWM output for hobby servo

// Motor control pins : L298N H bridge
const int enAPin = 6; // Left motor PWM speed control
const int in1Pin = 7; // Left motor Direction 1
const int in2Pin = 5; // Left motor Direction 2
const int in3Pin = 4; // Right motor Direction 1
const int in4Pin = 2; // Right motor Direction 2
const int enBPin = 3; // Right motor PWM speed control
enum Motor { LEFT, RIGHT };

// Set motor speed: 255 full ahead, −255 full reverse , 0 stop
void go( enum Motor m, int speed)
{
digitalWrite (m == LEFT ? in1Pin : in3Pin , speed > 0 ? HIGH : LOW );
digitalWrite (m == LEFT ? in2Pin : in4Pin , speed <= 0 ? HIGH : LOW );
analogWrite(m==LEFT ? enAPin:enBPin, speed <0? -speed : speed);
}
// Read distance from the ultrasonic sensor , return distance in mm //
// Speed of sound in dry air , 20C is 343 m/s
// pulseIn returns time in microseconds (10ˆ−6)
// 2d=p10ˆ−6s343m/s=p0.00343m=p0.343mm/us
unsigned int readDistance() {
digitalWrite ( trigPin , HIGH );
delayMicroseconds (10);
digitalWrite ( trigPin , LOW );
unsigned long period = pulseIn ( echoPin, HIGH ); return period * 343 / 2000;
}

#define NUM_ANGLES 7
unsigned char sensorAngle[NUM_ANGLES] = { 60, 70, 80, 90, 100, 110, 120 };
unsigned int distance [NUM_ANGLES];
// Scan the area ahead by sweeping the ultrasonic sensor left and right
// and recording the distance observed . This takes a reading , then
// sends the servo to the next angle . Call repeatedly once every 50 ms or so .
void readNextDistance() {
static unsigned char angleIndex = 0;
static signed char step = 1;
distance [angleIndex] = readDistance ();
angleIndex += step ;
if (angleIndex==NUM_ANGLES-1) step=-1;
else if (angleIndex==0) step=1;
servo.write( sensorAngle[angleIndex] );
}

// Initial configuration
//
// Configure the input and output pins
// Center the servo
// Turn off the motors
// Test the motors
// Scan the surroundings once //
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite ( trigPin , LOW);
pinMode(enAPin, OUTPUT);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);
pinMode(enBPin, OUTPUT);
servo . attach ( servoPin );
servo . write (90);
go(LEFT, 0);
go(RIGHT, 0);
}

// Mainloop
void loop () {

readNextDistance ();
// See if something is too close at any angle
unsigned char tooClose = 0;
for (unsigned char i = 0 ; i < NUM_ANGLES ; i++)
if ( distance [i] < 300)
tooClose = 1;
if ( tooClose ) {
// Something's nearby: back up left
go(LEFT, -50);
go(RIGHT,-170);
} else {
// Nothing in our way: go forward
go(LEFT, 255);
go(RIGHT, 255);
}
// Check the next direction in 50 ms
delay (50);
}

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