2wd robot car help

Hello i am new to arduino and having some problems with a car kit i bought. everything seems to work except for the fact it just wants to go in circles when powered on. i played with the values a little bit and got one motor to engage for a second at a time and still go in circles.

#include <Servo.h> 
int pinLB=14;     // Define a 14 Pin
int pinLF=15;     // Define a 15 Pin

int pinRB=16;    // Define a 16 Pin
int pinRF=17;    // Define a 17 Pin

//int MotorLPWM=5;  //Define a 5 Pin
//int MotorRPWM=6;  //Define a 6 Pin

int inputPin = 9;  // Define the ultrasound signal receiving a Pin
int outputPin =8;  //Define the ultrasound signal emission Pin

int Fspeedd = 0;      // go
int Rspeedd = 0;      // The right to
int Lspeedd = 0;      // Turn left to
int directionn = 0;   // After the former = 8 = 2 left = 4 right = 6 
Servo myservo;        // Set up the myservo
int delay_time = 250; // After the servo motor to the stability of the time

int Fgo = 8;         // go
int Rgo = 6;         // The right to
int Lgo = 4;         // Turn left to
int Bgo = 2;         // astern

void setup()
 {
  Serial.begin(9600);     // Initialize 
  pinMode(pinLB,OUTPUT); // Define 14 pin for the output (PWM)
  pinMode(pinLF,OUTPUT); // Define 15 pin for the output (PWM)
  pinMode(pinRB,OUTPUT); // Define 16 pin for the output (PWM) 
  pinMode(pinRF,OUTPUT); // Define 17 pin for the output (PWM)
  
  //pinMode(MotorLPWM,  OUTPUT);  // Define 5 pin for PWM output 
 // pinMode(MotorRPWM,  OUTPUT);  // Define 6 pin for PWM output
  
  pinMode(inputPin, INPUT);    // Define the ultrasound enter pin
  pinMode(outputPin, OUTPUT);  // Define the ultrasound output pin   

  myservo.attach(11);    // Define the servo motor output 10 pin(PWM)
 }
void advance(int a)     // go
    {
     digitalWrite(pinRB,HIGH);  // 16 feet for high level
     digitalWrite(pinRF,LOW);   //17 feet for low level
     //analogWrite(MotorRPWM,180);//Set the output speed(PWM)
     digitalWrite(pinLB,HIGH);  // 14 feet for high level
     digitalWrite(pinLF,LOW);   //15 feet for high level
     //analogWrite(MotorLPWM,180);//Set the output speed(PWM)
     delay(a * 1);     
    }

void right(int b)        //right
    {
     digitalWrite(pinRB,LOW);   
     digitalWrite(pinRF,HIGH);
     //analogWrite(MotorRPWM,250);
     digitalWrite(pinLB,LOW);
     digitalWrite(pinLF,LOW);
     delay(b * 100);
    }
void left(int c)         //left
    {
     digitalWrite(pinRB,LOW);
     digitalWrite(pinRF,LOW);
     digitalWrite(pinLB,LOW);   
     digitalWrite(pinLF,HIGH);
     //analogWrite(MotorLPWM,250);
     delay(c * 100);
    }
void turnR(int d)        //right
    {
     digitalWrite(pinRB,HIGH);  
     digitalWrite(pinRF,LOW);
     //analogWrite(MotorRPWM,250);
     digitalWrite(pinLB,LOW);
     digitalWrite(pinLF,HIGH);  
     //analogWrite(MotorLPWM,250);
     delay(d * 50);
    }
void turnL(int e)        //left
    {
     digitalWrite(pinRB,LOW);
     digitalWrite(pinRF,HIGH);   
     //analogWrite(MotorRPWM,220);
     digitalWrite(pinLB,HIGH);   
     digitalWrite(pinLF,LOW);
     //analogWrite(MotorLPWM,220);
     delay(e * 50);
    }    
void stopp(int f)         //stop
    {
     digitalWrite(pinRB,LOW);
     digitalWrite(pinRF,LOW);
     digitalWrite(pinLB,LOW);
     digitalWrite(pinLF,LOW);
     delay(f * 100);
    }
void back(int g)          //back
    {

     digitalWrite(pinRB,LOW);  
     digitalWrite(pinRF,HIGH);
     //analogWrite(MotorRPWM,0);
     digitalWrite(pinLB,LOW);  
     digitalWrite(pinLF,HIGH);
     //analogWrite(MotorLPWM,230);
     delay(g * 500);     
    }
    
void detection()        //Measuring three angles(0.90.179)
    {      
      int delay_time = 200;   // After the servo motor to the stability of the time
      ask_pin_F();            // Read in front of the distance
      
     if(Fspeedd < 10)         // If the front distance less than 10 cm
      {
      stopp(1);               // Remove the output data 
      back(2);                // The back two milliseconds
      }
           
      if(Fspeedd < 25)         // If the front distance less than 25 cm
      {
        stopp(1);               // Remove the output data
        ask_pin_L();            // Read the left distance
        delay(delay_time);      // Waiting for the servo motor is stable
        ask_pin_R();            // Read the right distance  
        delay(delay_time);      //  Waiting for the servo motor is stable  
        
        if(Lspeedd > Rspeedd)   //If the distance is greater than the right distance on the left
        {
         directionn = Lgo;      //Left
        }
        
        if(Lspeedd <= Rspeedd)   //If the distance is less than or equal to the distance on the right
        {
         directionn = Rgo;      //right
        } 
        
        if (Lspeedd < 15 && Rspeedd < 15)   /*If the left front distance and distance and the right distance is less than 15 cm */
        {

         directionn = Bgo;      //Walk backwards        
        }          
      }
      else                      //If the front is not less than 25 cm (greater than)    
      {
        directionn = Fgo;        //Walk forward    
      }
     
    }    
void ask_pin_F()   // Measure the distance ahead
    {
      myservo.write(90);
      digitalWrite(outputPin, LOW);   // For low voltage 2 us ultrasonic launch
      delayMicroseconds(2);
      digitalWrite(outputPin, HIGH);  // Let ultrasonic launch 10 us high voltage, there is at least 10 us
      delayMicroseconds(10);
      digitalWrite(outputPin, LOW);    // Maintaining low voltage ultrasonic launch
      float Fdistance = pulseIn(inputPin, HIGH);  // Read the time difference
      Fdistance= Fdistance/5.8/10;       // A time to distance distance (unit: cm£©
      Serial.print("F distance:");      //The output distance (unit: cm)
      Serial.println(Fdistance);         //According to the distance
      Fspeedd = Fdistance;              
    }  
 void ask_pin_L()   // Measure the distance on the left 
    {
      myservo.write(5);
      delay(delay_time);
      digitalWrite(outputPin, LOW);   // For low voltage 2 us ultrasonic launch
      delayMicroseconds(2);
      digitalWrite(outputPin, HIGH);  // Let ultrasonic launch 10 us high voltage, there is at least 10 us
      delayMicroseconds(10);
      digitalWrite(outputPin, LOW);    // Maintaining low voltage ultrasonic launch
      float Ldistance = pulseIn(inputPin, HIGH);  // Read the time difference
      Ldistance= Ldistance/5.8/10;       // Will be time to distance distance (unit: cm)
      Serial.print("L distance:");       //The output distance (unit: cm)
      Serial.println(Ldistance);         //According to the distance
      Lspeedd = Ldistance;              // Will reading Lspeedd distance
    }  
void ask_pin_R()   // Measure the distance on the right 
    {
      myservo.write(177);
      delay(delay_time);
      digitalWrite(outputPin, LOW);   // For low voltage 2 us ultrasonic launch
      delayMicroseconds(2);
      digitalWrite(outputPin, HIGH);  // Let ultrasonic launch 10 us high voltage, there is at least 10 us
      delayMicroseconds(10);
      digitalWrite(outputPin, LOW);    // Maintaining low voltage ultrasonic launch
      float Rdistance = pulseIn(inputPin, HIGH);  // Read the time difference
      Rdistance= Rdistance/5.8/10;       //Will be time to distance distance (unit: cm)
      Serial.print("R distance:");       //The output distance (unit: cm)
      Serial.println(Rdistance);         //According to the distance
      Rspeedd = Rdistance;              
    }  
    
void loop()
 {
    myservo.write(90);  /*Make the servo motor ready position Prepare the next measurement */
    detection();        //Measuring Angle And determine which direction to go to
      
   if(directionn == 2)  //If directionn (direction) = 2 (back)          
   {
     back(8);                    //  back
     turnL(2);                   //Move slightly to the left (to prevent stuck in dead end lane)
     Serial.print(" Reverse ");   //According to the direction (reverse)
   }
   if(directionn == 6)           //If directionn (direction) = 6 (right)   
   {
     back(1); 
     turnR(6);                   // right
     Serial.print(" Right ");    //According to the direction (Right)
   }
   if(directionn == 4)          //If directionn (direction) = 4 (left)   
   {  
     back(1);      
     turnL(6);                  // left
     Serial.print(" Left ");     //According to the direction (Left)  
   }  
   if(directionn == 8)          //If directionn (direction) = 8 (forward)      
   { 
    advance(1);                 // go 
    Serial.print(" Advance ");   //According to the direction (Advance)
    Serial.print("   ");    
   }
 }

You have plenty of Serial.prints. I would start by checking if the detection() routine is working. When you put things in front of or to the sides of the car are the distances detected correctly ?

I assume you have tested that all the motor control functions, advance(), turnR() etc work as you expect. If not write some simple test code to just move forward for 2 seconds, back 2 seconds, tun right etc.

Just doing those organised tests may well point out the problem to you but if not come back and tell us EXACTLY what results you got, what works and what doesn't.

Steve

Thank you for your reply. I am very new to coding. The detection seems to work properly. The car will stop turn and move just once there's nothing in front it will go in circles. To add simple test code do I replace what's in the void loop? Any examples would be much appreciated. I've seen many different ways with different setups than mine and I'm stumped

The car will stop turn and move just once there’s nothing in front it will go in circles.

This collection of words seems to be a few bricks shy of a full load, and certainly could use some punctuation. As is, I have no idea what your car actually does, when there is, and is not, an obstacle in front of it.

Oops sorry. I was in a rush when replying. It is a obstacle avoiding robot car. It has a servo motor with an ultrasonic sensor attached to it. If it detects and obstacle it will look left or right and find a way around it. When nothing is in front of it, the right motor engages forward going in circles endlessly until you place an object in its path. It will move to a clear direction then continue in circles again .

So it doesn't "just want to go in circles"? In fact it avoids obstacles correctly but when there is no obstacle so you would expect it to go in a straight line, that is when it circles. Is that correct?

So do you ever get "Advance" printed in your serial monitor? And is that when it is not doing what you expect?

Why when you set "Fgo" do you only go forward for 1 millisecond then check again? That seems a ridiculously short time.

Your code is very difficult to read because the variables don't have sensible names (e.g. why calculate the distance as Fdistance and then move it into a variable called Fspeedd to use?) and most of the comments are rubbish e.g. "// The back two milliseconds" actually goes back for a full second and I have no idea what "// After the former = 8 = 2 left = 4 right = 6" means.

Steve

slipstick:
So it doesn't "just want to go in circles"? In fact it avoids obstacles correctly but when there is no obstacle so you would expect it to go in a straight line, that is when it circles. Is that correct?

So do you ever get "Advance" printed in your serial monitor? And is that when it is not doing what you expect?

Why when you set "Fgo" do you only go forward for 1 millisecond then check again? That seems a ridiculously short time.

Your code is very difficult to read because the variables don't have sensible names (e.g. why calculate the distance as Fdistance and then move it into a variable called Fspeedd to use?) and most of the comments are rubbish e.g. "// The back two milliseconds" actually goes back for a full second and I have no idea what "// After the former = 8 = 2 left = 4 right = 6" means.

Steve

Yes that is correct. It never goes just forward. Advance printed in serial monitor? Is there a way I can see what it's doing by hooking it up to computer? Like I stated before I'm a noob. The code is not mine it came with the kit. Thanks again for your reply

VKmaker New Avoidance tracking Motor Smart Robot Car Chassis Kit Speed Encoder Battery Box 2WD Ultrasonic module with tutorial CD https://www.amazon.com/dp/B01CXVA6IO/ref=cm_sw_r_fm_apa_7KOcAbVKYMWZV

Yes that is correct. It never goes just forward.

You need to ditch that sketch, then. Disconnect the servo and the ping sensor.

Write a sketch that does NOTHING BUT make the car move forward in a straight line. Post that code. Describe, like talking to a first date, in excruciating detail exactly what it does. If it looks to us like it should make the car move forward, but the car actually does the hokey pokey, then you have a hardware problem. And a drinking problem, but that's OK.

If it makes the car run in a circle, then you have a hardware problem, that might be relatively easy to correct. You can check that by picking the car up. If the left wheels turn faster than the right wheels, the car will not go in a straight line. Ditto if the right wheels turn faster.

If the left wheels go one way and the right wheels go the other, well, it will be impossible to make the car go forward.

What, exactly, is happening is still unknown, and may be a hardware problem, a software problem, or a combination of both.

      float Fdistance = pulseIn(inputPin, HIGH);  // Read the time difference

The pulseIn() function does not return a float. The time in microseconds that it does return is NOT a distance. This variable has the wrong type AND the wrong name.

  pinMode(pinLB,OUTPUT); // Define 14 pin for the output (PWM)
  pinMode(pinLF,OUTPUT); // Define 15 pin for the output (PWM)
  pinMode(pinRB,OUTPUT); // Define 16 pin for the output (PWM)
  pinMode(pinRF,OUTPUT); // Define 17 pin for the output (PWM)

Exactly which Arduino are you using that can do PWM on pins 14 through 17?

The kit, which the OP has finally mentioned, appears to come with a UNO. And fortunately the code doesn't actually try to use PWM on pins14-17, that's just yet another example of misleading comments.

Steve

Thedeer70:
Advance printed in serial monitor? Is there a way I can see what it's doing by hooking it up to computer? Like I stated before I'm a noob.

Yes but what you didn't say was that you had nothing to do with the code and no idea what it was doing. Do you have any programming experience at all?

Anyway if you had it connected to the computer to load the code in then connect it like that again. In the IDE (code loading/writing program) select Tools/Serial Monitor. If you prop the robot up so the wheels are off the ground it should now print stuff on the PC screen telling you where in the program it is and you can watch what the wheels are doing.

Steve

Thank you guys for your comments and suggestions. I have no real programming experience other than editing values. I started learning and read a book. Ditched the sketch the car came with. wrote basic code and everything worked hardware wise. The sketch i am using now works much better. I added pwm to slow it down as it was zipping across the floor and burning out on hardwood floors.

Next step is to make it avoid obstacles better ie. chair legs, dog bed and things just out of range of the ultrasonic sensor. I have been trying to code in 2 infrared obstacle avoidance sensors. having issues figuring out how to go about coding it in. mainly playing around after the if statements in the code below. I want to make it like an emergency brake, so i don’t keep running into things and watching my car decapitate itself. Then reverse ,stop and go back to servo/ ultrasonic sensor checking left and right again. How would i go about doing this ? is it even possible to run different sensors together?

#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 1;
const int LeftMotorBackward = 2;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;
const int enableA = 3;
const int enableB = 6;

//sensor pins
#define trig_pin 8 //analog input a1
#define echo_pin 9 //analog input a2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(enableA, OUTPUT);
  pinMode(enableB, OUTPUT);
  servo_motor.attach(7); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){

  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 40){ // original sketch was 20
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  analogWrite(enableA, 127);
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
    analogWrite(enableB, 127); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  analogWrite(enableA, 127);
  analogWrite(enableB, 127);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  
  
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}