I am trying to make a line follower robot that has obstacle avoidance and cannot figure out why the code will not work

The parts for this robot are an Arduino uno, a breadboard, IR Sensor (OPB706A), motor driver, switch, 2 gear motors, and distance sensor. (*Everything is from a SparkFun kit expect the IR sensor)

 #include <Wire.h>
#include <SparkFun_MMA8452Q.h>

const int leftIR_PIN = A0;   // Sensor1 output voltage
const int rightIR_PIN = A1;  // Sensor2 output voltage
const int AIN1 = 13;         // Control pin 1 on the motor driver for the right motor
const int AIN2 = 12;         // Control pin 2 on the motor driver for the right motor
const int PWMA = 11;         // Speed control pin on the motor driver for the right motor
const int PWMB = 10;         // Speed control pin on the motor driver for the left motor
const int BIN2 = 9;          // Control pin 2 on the motor driver for the left motor
const int BIN1 = 8;          // Control pin 1 on the motor driver for the left motor
const int trigPin = 6;
const int echoPin = 5;
const int switchPin = 7;     // Switch to turn the robot on and off

const int backupTime = 300;  // Amount of time that the robot will back up when it senses an object
const int turnTime = 200;    // Amount that the robot will turn once it has backed up

const int baseSpeed = 150;   // Base speed for both motors
const float kp = 50;         // Proportional gain for line-following
const int minSpeed = 100;    // Minimum motor speed
const int maxSpeed = 255;    // Maximum motor speed

const float leftThreshold = 4;   // Adjust these values according to your setup
const float rightThreshold = 4;  // Adjust these values according to your setup

void setup() {
    pinMode(leftIR_PIN, INPUT);
    pinMode(rightIR_PIN, INPUT);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(switchPin, INPUT_PULLUP);

    pinMode(AIN1, OUTPUT);
    pinMode(AIN2, OUTPUT);
    pinMode(PWMA, OUTPUT);
    pinMode(BIN1, OUTPUT);
    pinMode(BIN2, OUTPUT);
    pinMode(PWMB, OUTPUT);

    Serial.begin(9600);
    Serial.println("Hello World!");
}

void loop() {
    int proximityADC_left = analogRead(leftIR_PIN);
    int proximityADC_right = analogRead(rightIR_PIN);
    float proximityV_left = (float)proximityADC_left * 5.0 / 1023.0;
    float proximityV_right = (float)proximityADC_right * 5.0 / 1023.0;

    float distance = getDistance();
    float error = proximityV_left - proximityV_right;
    int leftSpeed = baseSpeed - kp * error;
    int rightSpeed = baseSpeed + kp * error;
    leftSpeed = constrain(leftSpeed, minSpeed, maxSpeed);
    rightSpeed = constrain(rightSpeed, minSpeed, maxSpeed);

    if (digitalRead(switchPin) == LOW) {
        if (distance < 3) {  // If an object is detected
            rightMotor(-255);  // Back up
            leftMotor(-255);
            delay(backupTime);

            rightMotor(255);   // Turn away from obstacle
            leftMotor(-255);
            delay(turnTime);
        } else {              // If no obstacle is detected, line-follow
            if (proximityV_left < leftThreshold && proximityV_right < rightThreshold ) {  // Both sensors detect the line
                // Code to follow the line
            } else if (proximityV_left < leftThreshold && proximityV_right > rightThreshold) {  // Left sensor detects the line
                // Code to adjust robot's direction towards the right
            } else if (proximityV_left > leftThreshold && proximityV_right < rightThreshold) {  // Right sensor detects the line
                // Code to adjust robot's direction towards the left
            } else {  // No sensor detects the line
                // Code to handle this situation
            }
        }
    } else {
        leftMotor(0);
        rightMotor(0);
    }

    delay(10);
}

void rightMotor(int motorSpeed) {
    if (motorSpeed > 0) {
        digitalWrite(AIN1, HIGH);
        digitalWrite(AIN2, LOW);
    } else if (motorSpeed < 0) {
        digitalWrite(AIN1, LOW);
        digitalWrite(AIN2, HIGH);
    } else {
        digitalWrite(AIN1, LOW);
        digitalWrite(AIN2, LOW);
    }
    analogWrite(PWMA, abs(motorSpeed));
}

void leftMotor(int motorSpeed) {
    if (motorSpeed > 0) {
        digitalWrite(BIN1, HIGH);
        digitalWrite(BIN2, LOW);
    } else if (motorSpeed < 0) {
        digitalWrite(BIN1, LOW);
        digitalWrite(BIN2, HIGH);
    } else {
        digitalWrite(BIN1, LOW);
        digitalWrite(BIN2, LOW);
    }
    analogWrite(PWMB, abs(motorSpeed));
}

float getDistance() {
    float echoTime;
    float calculatedDistance;
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    echoTime = pulseIn(echoPin, HIGH);
    calculatedDistance = echoTime / 148.0;
    return calculatedDistance;
}

Well for starters, no action is programmed in the else/if clause.

EDIT: just looked again and the switch made sense, kind of. More in my next question/post

Then you have else if (a bunch of conditions, where no obstacle detected)
and comments for where some action should go. If nothing should go there (seems unlikely), then your last clause

else { leftMotor(0); rightMotor(0);

turn the motors off will be the only thing that occurs if you don't detect an obstacle

Hi, the problem I am having is that it doesn't seem to follow the line. The motors spin up to speed and it just goes straight.

I'm sorry I completely overlooked that as I am new to coding. I made the changes to this. I chose to leave the "//No sensor detects the line" empty because should that happen, the bot will have failed already. After running this change the bot went straight and randomly turned seeming to not stick to the line. I am sure there is an error in my code, however I cannot find it.

     #include <Wire.h>
#include <SparkFun_MMA8452Q.h>

const int leftIR_PIN = A0;   // Sensor1 output voltage
const int rightIR_PIN = A1;  // Sensor2 output voltage
const int AIN1 = 13;         // Control pin 1 on the motor driver for the right motor
const int AIN2 = 12;         // Control pin 2 on the motor driver for the right motor
const int PWMA = 11;         // Speed control pin on the motor driver for the right motor
const int PWMB = 10;         // Speed control pin on the motor driver for the left motor
const int BIN2 = 9;          // Control pin 2 on the motor driver for the left motor
const int BIN1 = 8;          // Control pin 1 on the motor driver for the left motor
const int trigPin = 6;
const int echoPin = 5;
const int switchPin = 7;     // Switch to turn the robot on and off

const int backupTime = 300;  // Amount of time that the robot will back up when it senses an object
const int turnTime = 200;    // Amount that the robot will turn once it has backed up

const int baseSpeed = 150;   // Base speed for both motors
const float kp = 50;         // Proportional gain for line-following
const int minSpeed = 100;    // Minimum motor speed
const int maxSpeed = 255;    // Maximum motor speed

const float leftThreshold = 3.80;   // Adjust these values according to your setup
const float rightThreshold = 3.80;  // Adjust these values according to your setup

void setup() {
    pinMode(leftIR_PIN, INPUT);
    pinMode(rightIR_PIN, INPUT);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(switchPin, INPUT_PULLUP);

    pinMode(AIN1, OUTPUT);
    pinMode(AIN2, OUTPUT);
    pinMode(PWMA, OUTPUT);
    pinMode(BIN1, OUTPUT);
    pinMode(BIN2, OUTPUT);
    pinMode(PWMB, OUTPUT);

    Serial.begin(9600);
    Serial.println("Hello World!");
}

void loop() {
    int proximityADC_left = analogRead(leftIR_PIN);
    int proximityADC_right = analogRead(rightIR_PIN);
    float proximityV_left = (float)proximityADC_left * 5.0 / 1023.0;
    float proximityV_right = (float)proximityADC_right * 5.0 / 1023.0;

    float distance = getDistance();
    float error = proximityV_left - proximityV_right;
    int leftSpeed = baseSpeed - kp * error;
    int rightSpeed = baseSpeed + kp * error;
    leftSpeed = constrain(leftSpeed, minSpeed, maxSpeed);
    rightSpeed = constrain(rightSpeed, minSpeed, maxSpeed);

    if (digitalRead(switchPin) == LOW) {
        if (distance < 3) {  // If an object is detected
            rightMotor(-255);  // Back up
            leftMotor(-255);
            delay(backupTime);

            rightMotor(255);   // Turn away from obstacle
            leftMotor(-255);
            delay(turnTime);
        } else {              // If no obstacle is detected, line-follow
            if (proximityV_left < leftThreshold && proximityV_right < rightThreshold ) {  // Both sensors detect the line
                rightMotor(150);
                leftMotor(150);
            } else if (proximityV_left < leftThreshold && proximityV_right > rightThreshold) {  // Left sensor detects the line
                rightMotor(255);
                leftMotor(150);
            } else if (proximityV_left > leftThreshold && proximityV_right < rightThreshold) {  // Right sensor detects the line
                rightMotor(150);
                leftMotor(255);
            } else {  // No sensor detects the line
                // Code to handle this situation
            }
        }
    } else {
        leftMotor(0);
        rightMotor(0);
    }
    delay(10);
}

void rightMotor(int motorSpeed) {
    if (motorSpeed > 0) {
        digitalWrite(AIN1, HIGH);
        digitalWrite(AIN2, LOW);
    } else if (motorSpeed < 0) {
        digitalWrite(AIN1, LOW);
        digitalWrite(AIN2, HIGH);
    } else {
        digitalWrite(AIN1, LOW);
        digitalWrite(AIN2, LOW);
    }
    analogWrite(PWMA, abs(motorSpeed));
}

void leftMotor(int motorSpeed) {
    if (motorSpeed > 0) {
        digitalWrite(BIN1, HIGH);
        digitalWrite(BIN2, LOW);
    } else if (motorSpeed < 0) {
        digitalWrite(BIN1, LOW);
        digitalWrite(BIN2, HIGH);
    } else {
        digitalWrite(BIN1, LOW);
        digitalWrite(BIN2, LOW);
    }
    analogWrite(PWMB, abs(motorSpeed));
}

float getDistance() {
    float echoTime;
    float calculatedDistance;
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    echoTime = pulseIn(echoPin, HIGH);
    calculatedDistance = echoTime / 148.0;
    return calculatedDistance;
}

This is a library for an accelerometer, but I don't see any such device in your description or code.

The code that I based mine off of comes from this kit. I tried to modify it to my use case as that is what we were instructed to do, however I keep running into issues as you can see. Is there anything you recommend I do or a different approach to take? (*This is the base code provided in the kit for a " Circuit 5C: Autonomous Robot. It has all the same parts expect the 2 IR Sensors)

/*
  SparkFun Inventor’s Kit
  Circuit 5C - Autonomous Robot

  This robot will drive around on its own and react to obstacles by backing up and turning to a new direction.
  This sketch was adapted from one of the activities in the SparkFun Guide to Arduino.
  Check out the rest of the book at
  https://www.sparkfun.com/products/14326

  This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
  This code is completely free for any use.

  View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40
  Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
*/



//the right motor will be controlled by the motor A pins on the motor driver
const int AIN1 = 13;           //control pin 1 on the motor driver for the right motor
const int AIN2 = 12;            //control pin 2 on the motor driver for the right motor
const int PWMA = 11;            //speed control pin on the motor driver for the right motor

//the left motor will be controlled by the motor B pins on the motor driver
const int PWMB = 10;           //speed control pin on the motor driver for the left motor
const int BIN2 = 9;           //control pin 2 on the motor driver for the left motor
const int BIN1 = 8;           //control pin 1 on the motor driver for the left motor


//distance variables
const int trigPin = 6;
const int echoPin = 5;

int switchPin = 7;             //switch to turn the robot on and off

float distance = 0;            //variable to store the distance measured by the distance sensor

//robot behaviour variables
int backupTime = 300;           //amount of time that the robot will back up when it senses an object
int turnTime = 200;             //amount that the robot will turn once it has backed up

/********************************************************************************/
void setup()
{
  pinMode(trigPin, OUTPUT);       //this pin will send ultrasonic pulses out from the distance sensor
  pinMode(echoPin, INPUT);        //this pin will sense when the pulses reflect back to the distance sensor

  pinMode(switchPin, INPUT_PULLUP);   //set this as a pullup to sense whether the switch is flipped


  //set the motor control pins as outputs
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMA, OUTPUT);

  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);

  Serial.begin(9600);                       //begin serial communication with the computer
  Serial.print("To infinity and beyond!");  //test the serial connection
}

/********************************************************************************/
void loop()
{
  //DETECT THE DISTANCE READ BY THE DISTANCE SENSOR
  distance = getDistance();

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" in");              // print the units

  if (digitalRead(switchPin) == LOW) { //if the on switch is flipped

    if (distance < 10) {              //if an object is detected
      //back up and turn
      Serial.print(" ");
      Serial.print("BACK!");

      //stop for a moment
      rightMotor(0);
      leftMotor(0);
      delay(200);

      //back up
      rightMotor(-255);
      leftMotor(-255);
      delay(backupTime);

      //turn away from obstacle
      rightMotor(255);
      leftMotor(-255);
      delay(turnTime);

    } else {                        //if no obstacle is detected drive forward
      Serial.print(" ");
      Serial.print("Moving...");


      rightMotor(255);
      leftMotor(255);
    }
  } else {                        //if the switch is off then stop

    //stop the motors
    rightMotor(0);
    leftMotor(0);
  }

  delay(50);                      //wait 50 milliseconds between readings
}

/********************************************************************************/
void rightMotor(int motorSpeed)                       //function for driving the right motor
{
  if (motorSpeed > 0)                                 //if the motor should drive forward (positive speed)
  {
    digitalWrite(AIN1, HIGH);                         //set pin 1 to high
    digitalWrite(AIN2, LOW);                          //set pin 2 to low
  }
  else if (motorSpeed < 0)                            //if the motor should drive backward (negative speed)
  {
    digitalWrite(AIN1, LOW);                          //set pin 1 to low
    digitalWrite(AIN2, HIGH);                         //set pin 2 to high
  }
  else                                                //if the motor should stop
  {
    digitalWrite(AIN1, LOW);                          //set pin 1 to low
    digitalWrite(AIN2, LOW);                          //set pin 2 to low
  }
  analogWrite(PWMA, abs(motorSpeed));                 //now that the motor direction is set, drive it at the entered speed
}

/********************************************************************************/
void leftMotor(int motorSpeed)                        //function for driving the left motor
{
  if (motorSpeed > 0)                                 //if the motor should drive forward (positive speed)
  {
    digitalWrite(BIN1, HIGH);                         //set pin 1 to high
    digitalWrite(BIN2, LOW);                          //set pin 2 to low
  }
  else if (motorSpeed < 0)                            //if the motor should drive backward (negative speed)
  {
    digitalWrite(BIN1, LOW);                          //set pin 1 to low
    digitalWrite(BIN2, HIGH);                         //set pin 2 to high
  }
  else                                                //if the motor should stop
  {
    digitalWrite(BIN1, LOW);                          //set pin 1 to low
    digitalWrite(BIN2, LOW);                          //set pin 2 to low
  }
  analogWrite(PWMB, abs(motorSpeed));                 //now that the motor direction is set, drive it at the entered speed
}

/********************************************************************************/
//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
float getDistance()
{
  float echoTime;                   //variable to store the time it takes for a ping to bounce off an object
  float calculatedDistance;         //variable to store the distance calculated from the echo time

  //send out an ultrasonic pulse that's 10ms long
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  echoTime = pulseIn(echoPin, HIGH);      //use the pulsein command to see how long it takes for the
                                          //pulse to bounce back to the sensor

  calculatedDistance = echoTime / 148.0;  //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)

  return calculatedDistance;              //send back the distance that was calculated
}

This is some code I originally had based of this (I converted the volt readings of the sensor so that is why they are in the 100s):

/*
  SparkFun Inventor’s Kit
  Circuit 5C - Autonomous Robot


  This robot will drive around on its own and react to obstacles by backing up and turning to a new direction.
  This sketch was adapted from one of the activities in the SparkFun Guide to Arduino.
  Check out the rest of the book at
  https://www.sparkfun.com/products/14326

  This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
  This code is completely free for any use.

  View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40
  Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
*/
#include <Wire.h>
#include <SparkFun_MMA8452Q.h>


const int leftIR_PIN = A0; // Sensor1 output voltage
const int rightIR_PIN = A1; // Sensor output voltage

//the right motor will be controlled by the motor A pins on the motor driver
const int AIN1 = 13;           //control pin 1 on the motor driver for the right motor
const int AIN2 = 12;            //control pin 2 on the motor driver for the right motor
const int PWMA = 11;            //speed control pin on the motor driver for the right motor

//the left motor will be controlled by the motor B pins on the motor driver
const int PWMB = 10;           //speed control pin on the motor driver for the left motor
const int BIN2 = 9;           //control pin 2 on the motor driver for the left motor
const int BIN1 = 8;           //control pin 1 on the motor driver for the left motor


//distance variables
const int trigPin = 6;
const int echoPin = 5;

int switchPin = 7;             //switch to turn the robot on and off

float distance = 0;            //variable to store the distance measured by the distance sensor

//robot behaviour variables
int backupTime = 300;           //amount of time that the robot will back up when it senses an object
int turnTime = 200;             //amount that the robot will turn once it has backed up

/********************************************************************************/
void setup()
{ 
   Serial.begin(9600);
  
  pinMode(leftIR_PIN, INPUT);
  pinMode(rightIR_PIN, INPUT);

  pinMode(trigPin, OUTPUT);       //this pin will send ultrasonic pulses out from the distance sensor
  pinMode(echoPin, INPUT);        //this pin will sense when the pulses reflect back to the distance sensor

  pinMode(switchPin, INPUT_PULLUP);   //set this as a pullup to sense whether the switch is flipped


  //set the motor control pins as outputs
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMA, OUTPUT);

  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);

  Serial.begin(9600);                       //begin serial communication with the computer
  Serial.print("To infinity and beyond!");  //test the serial connection
}

/********************************************************************************/
void loop()
{
    // Read in the ADC and convert it to a voltage:
  int leftSensor = analogRead(leftIR_PIN);
  int rightSensor = analogRead(rightIR_PIN);
  Serial.println(rightSensor);
  
  //DETECT THE DISTANCE READ BY THE DISTANCE SENSOR
  distance = getDistance();

  /*Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" in");              // print the units
  */

  if (digitalRead(switchPin) == LOW) { //if the on switch is flipped
    

    if (distance < 10) {              //if an object is detected
      /*back up and turn
      Serial.print(" ");
      Serial.print("BACK!");
      */
      //stop for a moment
      rightMotor(0);
      leftMotor(0);
      delay(200);

      //back up
      rightMotor(-255);
      leftMotor(-255);
      delay(backupTime);

      //turn away from obstacle
      rightMotor(255);
      leftMotor(-255);
      delay(turnTime);
      
    } else {                        //if no obstacle is detected drive forward
      /*
      Serial.print(" ");
      Serial.print("Moving...");
      */
     /*f(leftSensor < 850 && rightSensor < 750){
          rightMotor(-225);
          leftMotor(-225);
      }*/
         if(leftSensor < 850 && rightSensor > 750){
          rightMotor(-175);
          leftMotor(-175);
        }
        else if(leftSensor > 850 && rightSensor < 750 ){
          rightMotor(-175);
          leftMotor(-175);
        }
       /* else if( proximityV_left > 3.7 && proximityV_right > 3.7){
          rightMotor(50);
          leftMotor(255);
        }
*/    }
  
  } else {                        //if the switch is off then stop

    //stop the motors
    rightMotor(0);
    leftMotor(0);
  }

}

/********************************************************************************/
void rightMotor(int motorSpeed)                       //function for driving the right motor
{
  if (motorSpeed > 0)                                 //if the motor should drive forward (positive speed)
  {
    digitalWrite(AIN1, HIGH);                         //set pin 1 to high
    digitalWrite(AIN2, LOW);                          //set pin 2 to low
  }
  else if (motorSpeed < 0)                            //if the motor should drive backward (negative speed)
  {
    digitalWrite(AIN1, LOW);                          //set pin 1 to low
    digitalWrite(AIN2, HIGH);                         //set pin 2 to high
  }
  else                                                //if the motor should stop
  {
    digitalWrite(AIN1, LOW);                          //set pin 1 to low
    digitalWrite(AIN2, LOW);                          //set pin 2 to low
  }
  analogWrite(PWMA, abs(motorSpeed));                 //now that the motor direction is set, drive it at the entered speed
}

/********************************************************************************/
void leftMotor(int motorSpeed)                        //function for driving the left motor
{
  if (motorSpeed > 0)                                 //if the motor should drive forward (positive speed)
  {
    digitalWrite(BIN1, HIGH);                         //set pin 1 to high
    digitalWrite(BIN2, LOW);                          //set pin 2 to low
  }
  else if (motorSpeed < 0)                            //if the motor should drive backward (negative speed)
  {
    digitalWrite(BIN1, LOW);                          //set pin 1 to low
    digitalWrite(BIN2, HIGH);                         //set pin 2 to high
  }
  else                                                //if the motor should stop
  {
    digitalWrite(BIN1, LOW);                          //set pin 1 to low
    digitalWrite(BIN2, LOW);                          //set pin 2 to low
  }
  analogWrite(PWMB, abs(motorSpeed));                 //now that the motor direction is set, drive it at the entered speed
}

/********************************************************************************/
//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
float getDistance()
{
  float echoTime;                   //variable to store the time it takes for a ping to bounce off an object
  float calculatedDistance;         //variable to store the distance calculated from the echo time

  //send out an ultrasonic pulse that's 10ms long
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  echoTime = pulseIn(echoPin, HIGH);      //use the pulsein command to see how long it takes for the
                                          //pulse to bounce back to the sensor

  calculatedDistance = echoTime / 148.0;  //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)

  return calculatedDistance;              //send back the distance that was calculated
}

Well the good news is, it's totally not your fault. Silly Sparkfun example sketch led you down the wrong path.
Gimme a sec, working on it.

So good effort, don't give up. You've got this and be sure to send Sparkfun a sternly worded email to the effect of "Gee, thanks for the example using the wrong library".

Here's the basic motor driver sketch from their correct library for the motor driver, the TB6612 H-Bridge, and a link to the Github for same. If you would like proof, comment out the

#include <SparkFun_MMA8452Q.h>

from your first sketch and verify, it will compile. That means nothing in it was used.
Now after installing the correct library and swapping out the wrong one and putting this in instead

#include <SparkFun_TB6612.h>

and adding a line from the that library's example sketch, say after your else if condition where you only had comments before

motor1.drive(255,1000);

Then add these:

const int STBY = 9;
// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;

Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);

cribbed right from the motor example sketch,click verify and you'll see it compile. Obviously this is just a proof, you will have to tailor the actions under else if to your needs.

Now, comment out the new motor driver library (and delete the MMA8452Q line from your code altogether) and click verify. It won't compile because that motor1.drive line needs the library. So uncomment the library again and get back to the fun!

https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library

/******************************************************************************
TestRun.ino
TB6612FNG H-Bridge Motor Driver Example code
Michelle @ SparkFun Electronics
8/20/16
https://github.com/sparkfun/SparkFun_TB6612FNG_Arduino_Library

Uses 2 motors to show examples of the functions in the library.  This causes
a robot to do a little 'jig'.  Each movement has an equal and opposite movement
so assuming your motors are balanced the bot should end up at the same place it
started.

Resources:
TB6612 SparkFun Library

Development environment specifics:
Developed on Arduino 1.6.4
Developed with ROB-9457
******************************************************************************/

// This is the library for the TB6612 that contains the class Motor and all the
// functions
#include <SparkFun_TB6612.h>

// Pins for all inputs, keep in mind the PWM defines must be on PWM pins
// the default pins listed are the ones used on the Redbot (ROB-12097) with
// the exception of STBY which the Redbot controls with a physical switch
#define AIN1 2
#define BIN1 7
#define AIN2 4
#define BIN2 8
#define PWMA 5
#define PWMB 6
#define STBY 9

// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;

// Initializing motors.  The library will allow you to initialize as many
// motors as you have memory for.  If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
// call the function more than once.
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);

void setup()
{
 //Nothing here
}


void loop()
{
   //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255.  Also use of the 
   //brake function which takes no arguements.
   motor1.drive(255,1000);
   motor1.drive(-255,1000);
   motor1.brake();
   delay(1000);
   
   //Use of the drive function which takes as arguements the speed
   //and optional duration.  A negative speed will cause it to go
   //backwards.  Speed can be from -255 to 255.  Also use of the 
   //brake function which takes no arguements.
   motor2.drive(255,1000);
   motor2.drive(-255,1000);
   motor2.brake();
   delay(1000);
   
   //Use of the forward function, which takes as arguements two motors
   //and optionally a speed.  If a negative number is used for speed
   //it will go backwards
   forward(motor1, motor2, 150);
   delay(1000);
   
   //Use of the back function, which takes as arguments two motors 
   //and optionally a speed.  Either a positive number or a negative
   //number for speed will cause it to go backwards
   back(motor1, motor2, -150);
   delay(1000);
   
   //Use of the brake function which takes as arguments two motors.
   //Note that functions do not stop motors on their own.
   brake(motor1, motor2);
   delay(1000);
   
   //Use of the left and right functions which take as arguements two
   //motors and a speed.  This function turns both motors to move in 
   //the appropriate direction.  For turning a single motor use drive.
   left(motor1, motor2, 100);
   delay(1000);
   right(motor1, motor2, 100);
   delay(1000);
   
   //Use of brake again.
   brake(motor1, motor2);
   delay(1000);
   
}

Hello @jacksonbanko - Your kit car with the TB6612FNG motor driver sounds like it has the same parts as another kit car where I scaled-down the all-in-one (and poorly written) sketches into individual (working) device sketches. I did not include BlueTooth.

Ok, I would keep the IR sensor out of it for now, stick to the motors and ultrasonic and switch until you get that working, then add in more. I don't have the kit to test this, but try this on for size.
You'll have to add in your own "go" commands in after if/else if conditionals

#include <Wire.h>
#include <SparkFun_TB6612.h>

const int leftIR_PIN = A0;   // Sensor1 output voltage
const int rightIR_PIN = A1;  // Sensor2 output voltage
const int AIN1 = 13;         // Control pin 1 on the motor driver for the right motor
const int AIN2 = 12;         // Control pin 2 on the motor driver for the right motor
const int PWMA = 11;         // Speed control pin on the motor driver for the right motor
const int PWMB = 10;         // Speed control pin on the motor driver for the left motor
const int BIN2 = 9;          // Control pin 2 on the motor driver for the left motor
const int BIN1 = 8;          // Control pin 1 on the motor driver for the left motor
// const int STBY = 9; does yours have this? you already used D9 so find a new one if needed?
const int trigPin = 6;
const int echoPin = 5;
const int switchPin = 7;     // Switch to turn the robot on and off

const int backupTime = 300;  // Amount of time that the robot will back up when it senses an object
const int turnTime = 200;    // Amount that the robot will turn once it has backed up

const int baseSpeed = 150;   // Base speed for both motors
const float kp = 50;         // Proportional gain for line-following
const int minSpeed = 100;    // Minimum motor speed
const int maxSpeed = 255;    // Maximum motor speed

const float leftThreshold = 4;   // Adjust these values according to your setup
const float rightThreshold = 4;  // Adjust these values according to your setup

// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;

Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);

void setup() {
    pinMode(leftIR_PIN, INPUT);
    pinMode(rightIR_PIN, INPUT);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(switchPin, INPUT_PULLUP);

    pinMode(AIN1, OUTPUT);
    pinMode(AIN2, OUTPUT);
    pinMode(PWMA, OUTPUT);
    pinMode(BIN1, OUTPUT);
    pinMode(BIN2, OUTPUT);
    pinMode(PWMB, OUTPUT);

    Serial.begin(9600);
    Serial.println("Hello World!");
}

void loop() {
    int proximityADC_left = analogRead(leftIR_PIN);
    int proximityADC_right = analogRead(rightIR_PIN);
    float proximityV_left = (float)proximityADC_left * 5.0 / 1023.0;
    float proximityV_right = (float)proximityADC_right * 5.0 / 1023.0;

    float distance = getDistance();
    float error = proximityV_left - proximityV_right;
    int leftSpeed = baseSpeed - kp * error;
    int rightSpeed = baseSpeed + kp * error;
    leftSpeed = constrain(leftSpeed, minSpeed, maxSpeed);
    rightSpeed = constrain(rightSpeed, minSpeed, maxSpeed);

    if (digitalRead(switchPin) == LOW) {
        if (distance < 3) {  // If an object is detected
            rightMotor(-255);  // Back up
            leftMotor(-255);
            delay(backupTime);

            rightMotor(255);   // Turn away from obstacle
            leftMotor(-255);
            delay(turnTime);
        } else {              // If no obstacle is detected, line-follow
            if (proximityV_left < leftThreshold && proximityV_right < rightThreshold ) {  // Both sensors detect the line
                   motor2.drive(-255,1000);
            } else if (proximityV_left < leftThreshold && proximityV_right > rightThreshold) {  // Left sensor detects the line
                // Code to adjust robot's direction towards the right
            } else if (proximityV_left > leftThreshold && proximityV_right < rightThreshold) {  // Right sensor detects the line
                // Code to adjust robot's direction towards the left
            } else {  // No sensor detects the line
                // Code to handle this situation
            }
        }
    } else {
        leftMotor(0);
        rightMotor(0);
    }

    delay(10);
}

void rightMotor(int motorSpeed) {
    if (motorSpeed > 0) {
        digitalWrite(AIN1, HIGH);
        digitalWrite(AIN2, LOW);
    } else if (motorSpeed < 0) {
        digitalWrite(AIN1, LOW);
        digitalWrite(AIN2, HIGH);
    } else {
        digitalWrite(AIN1, LOW);
        digitalWrite(AIN2, LOW);
    }
    analogWrite(PWMA, abs(motorSpeed));
}

void leftMotor(int motorSpeed) {
    if (motorSpeed > 0) {
        digitalWrite(BIN1, HIGH);
        digitalWrite(BIN2, LOW);
    } else if (motorSpeed < 0) {
        digitalWrite(BIN1, LOW);
        digitalWrite(BIN2, HIGH);
    } else {
        digitalWrite(BIN1, LOW);
        digitalWrite(BIN2, LOW);
    }
    analogWrite(PWMB, abs(motorSpeed));
}

float getDistance() {
    float echoTime;
    float calculatedDistance;
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    echoTime = pulseIn(echoPin, HIGH);
    calculatedDistance = echoTime / 148.0;
    return calculatedDistance;
}

Ok. So, I did this and when I put my finger over the sensors it does the correct thing, ie. If my finger is over the left sensor the right motor goes faster and the inverse is true. However, when I put it on the line it doesn't replicate what my finger did.

#include <Wire.h>
#include <SparkFun_TB6612.h>

const int leftIR_PIN = A0;   // Sensor1 output voltage
const int rightIR_PIN = A1;  // Sensor2 output voltage
const int AIN1 = 13;         // Control pin 1 on the motor driver for the right motor
const int AIN2 = 12;         // Control pin 2 on the motor driver for the right motor
const int PWMA = 11;         // Speed control pin on the motor driver for the right motor
const int PWMB = 10;         // Speed control pin on the motor driver for the left motor
const int BIN2 = 9;          // Control pin 2 on the motor driver for the left motor
const int BIN1 = 8;          // Control pin 1 on the motor driver for the left motor
// const int STBY = 9; does yours have this? you already used D9 so find a new one if needed?
const int trigPin = 6;
const int echoPin = 5;
const int switchPin = 7;     // Switch to turn the robot on and off

const int backupTime = 300;  // Amount of time that the robot will back up when it senses an object
const int turnTime = 200;    // Amount that the robot will turn once it has backed up

const int baseSpeed = 150;   // Base speed for both motors
const float kp = 50;         // Proportional gain for line-following
const int minSpeed = 100;    // Minimum motor speed
const int maxSpeed = 255;    // Maximum motor speed

const float leftThreshold = 2;   // Adjust these values according to your setup
const float rightThreshold = 2;  // Adjust these values according to your setup

// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1
const int offsetA = 1;
const int offsetB = 1;

const int STBY = 9;
// these constants are used to allow you to make your motor configuration 
// line up with function names like forward.  Value can be 1 or -1

Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);

void setup() {
    pinMode(leftIR_PIN, INPUT);
    pinMode(rightIR_PIN, INPUT);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(switchPin, INPUT_PULLUP);

    pinMode(AIN1, OUTPUT);
    pinMode(AIN2, OUTPUT);
    pinMode(PWMA, OUTPUT);
    pinMode(BIN1, OUTPUT);
    pinMode(BIN2, OUTPUT);
    pinMode(PWMB, OUTPUT);

    Serial.begin(9600);
    Serial.println("Hello World!");
}

void loop() {
    int proximityADC_left = analogRead(leftIR_PIN);
    int proximityADC_right = analogRead(rightIR_PIN);
    float proximityV_left = (float)proximityADC_left * 5.0 / 1023.0;
    float proximityV_right = (float)proximityADC_right * 5.0 / 1023.0;

    float distance = getDistance();
    float error = proximityV_left - proximityV_right;
    int leftSpeed = baseSpeed - kp * error;
    int rightSpeed = baseSpeed + kp * error;
    leftSpeed = constrain(leftSpeed, minSpeed, maxSpeed);
    rightSpeed = constrain(rightSpeed, minSpeed, maxSpeed);

    if (digitalRead(switchPin) == LOW) {
        if (distance < 3) {  // If an object is detected
            rightMotor(-255);  // Back up
            leftMotor(-255);
            delay(backupTime);

            rightMotor(255);   // Turn away from obstacle
            leftMotor(-255);
            delay(turnTime);
        } else {              // If no obstacle is detected, line-follow
            if (proximityV_left < leftThreshold && proximityV_right < rightThreshold ) {  // Both sensors detect the line
                   motor2.drive(255,1000);
            } else if (proximityV_left < leftThreshold && proximityV_right > rightThreshold) {  // Left sensor detects the line
                rightMotor(255);
                leftMotor(155);
            } else if (proximityV_left > leftThreshold && proximityV_right < rightThreshold) {  // Right sensor detects the line
                rightMotor(155);
                leftMotor(255);
            } else {  // No sensor detects the line
                rightMotor(255);
                leftMotor(255);
            }
        }
    } else {
        leftMotor(0);
        rightMotor(0);
    }

    delay(10);
}

void rightMotor(int motorSpeed) {
    if (motorSpeed > 0) {
        digitalWrite(AIN1, HIGH);
        digitalWrite(AIN2, LOW);
    } else if (motorSpeed < 0) {
        digitalWrite(AIN1, LOW);
        digitalWrite(AIN2, HIGH);
    } else {
        digitalWrite(AIN1, LOW);
        digitalWrite(AIN2, LOW);
    }
    analogWrite(PWMA, abs(motorSpeed));
}

void leftMotor(int motorSpeed) {
    if (motorSpeed > 0) {
        digitalWrite(BIN1, HIGH);
        digitalWrite(BIN2, LOW);
    } else if (motorSpeed < 0) {
        digitalWrite(BIN1, LOW);
        digitalWrite(BIN2, HIGH);
    } else {
        digitalWrite(BIN1, LOW);
        digitalWrite(BIN2, LOW);
    }
    analogWrite(PWMB, abs(motorSpeed));
}

float getDistance() {
    float echoTime;
    float calculatedDistance;
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    echoTime = pulseIn(echoPin, HIGH);
    calculatedDistance = echoTime / 148.0;
    return calculatedDistance;
}

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