Arduino based rc car with object follow

I have been making this project for college and I would love some feedback on if the code would work at the moment and what I could add? The project includes rear wheel drive with a servo to steer the front wheels, as well as one to rotate a stalker function I have named as "objectFollow" in my code.

#include <Servo.h>.                 //include the servo library
Servo steeringServo;                //label the steering servo
Servo sensorServo;                  //label the sensor config servo

const int joyDrive = A3;            //label the input pin for fwd/bckw drive
const int joySteer = A2;            //label the input pin for steering
const int joyHorn = A1;             //label the input pin for the horn
const int objFollow = 8;            //label the input pin for object following

const int buzzer = 7;               //label the output pin for the horn
int sound = 1500;                   //state the value of sound

const int objFollowLED = 6;         //label the pin for the object follow indicator LED
const int sensorEcho = 9;           //label the pin for US Sensor echo
const int sensorTrig - 10;          //label the pin the US Sensor trigger
const int motors = 5;               //label the output pin for the motors

int driveVal;                       //state variable name for fwd/ backw drive
int steerVal;                       //state variable name for the steering
int echoVal;                        //state the variable to store value of distance

void setup() {

  steeringServo.attach(12);                     //attach steering servo to pin 12
  sensorServo.attach(11);                       //attach sensor config servo to pin 11
  pinMode(joyDrive, INPUT);                     //declare the joyX as input
  pinMode(joySteer, INPUT);                     //declare the joyY as input
  pinMode(joyHorn, INPUT);                      //declare the joyButton as input
  pinMode(objFollow, INPUT_PULLUP);             //declare the object follow button as input
  pinMode(sensorEcho, INPUT);                   //declare the echo pin as input
  pinMode(buzzer, OUTPUT);                      //declare the buzzer as output
  pinMode(objFollowLED, OUTPUT);                //declare the object follow indicator as output
  pinMode(sensorTrig, OUTPUT);                  //declare the trigger pin as an output

  driveCheck();                                 //call the drive check function
}

void loop() {

  driveSeq();                                                             //call the driving sequence

  int driveVal = map(analogRead(joyDrive), 0. 1023. 0. 180);              //set and map the value of joyX
  int steerVal = map(analogRead(joySteer), 0, 1023, 0, 180);              //set and map the value of joyY

  long durationindigit, distanceincm;                                     //calculate distance for object following
  digitalWrite(trigPin, LOW);                                             //loop the sequence to measure distance
  delayMicroseconds(2);                                                   //********
  digitalWrite(trigPin, HIGH);                                            //******
  delayMicroseconds(10);                                                  //****
  digitalWrite(trigPin, LOW);                                             //**

  int durationInDigit = pulseIn(sensorEcho, HIGH);                        //state the rebound duration
  int distanceIncm = (durationInDigit * 0.034) / 2;                       //state the distance in cm from the rebound duration

  {
    if (digitalRead(objFollow == HIGH)) {                                 //if the the object follow button is pushed
      if (distanceIncm >= 30) {                                             //and the distance to the object is out of range
        digitalWrite(motors, LOW);                                          //don't drive motors
      } else if (distanceIncm < 30 && distanceIncm > 10) {                  //if the distance to the object is in range
        digitalWrite(motors, HIGH);                                         //drive motors
      } else if (distnaceIncm < 10) {                                       //if the distance is too close
        digitalWrite(motors, LOW);                                          //don'd drive the motors
      }
    } else if (digitalRead(objFollow == LOW)) {                           //if the object follow button is not pushed
      driveSeq();                                                         //carry out the drive sequence
    }
  }
}

void driveCheck() {

  digitalWrite(motors, HIGH);                 //drive the motors
  delay(1000);                                //wait 1 sec
  digitalWrite(motors, LOW);                  //***reverse the motors***
  delay(2000);                                //wait 2 secs

  steeringServo.write(90);                    //turn the steering servo left
  sensorServo.write(90)                       //turn the sensor servo left
  delay(1500);                                //wait 1.5 secs
  steeringServo.write(0);                     //turn the steering servo right
  sensorServo.write(0);                       //turn the sensor servo right
  delay(1000);                                //wait 1 sec

  tone(buzzer);                               //sound the horn
  delay(500);                                 //wait 0.5 secs
  noTone(buzzer);                             //stop sounding the horn
  delay(1000);                                //wait 1 sec

  digitalWrite(objFollowLED, HIGH);           //turn on the object follow indicator
  delay(500);                                 //wait 0.5 secs
  digitalWrite(objFollowLED, LOW);            //turn off the object follow indicator
}

void driveSeq() {

  {
    if (analogRead(joyHorn) == HIGH) {                  //if the horn button is pressed
      tone(buzzer);                                       //sound the horn
    } else {                                            //otherwise
      noTone(buzzer);                                     //dont sound the horn
    }
  }

  {
    if (driveVal > 512) {                               //if the joyX value is positive
      digitalWrite(motors, HIGH);                         //drive the motors
    } else if (driveVal < 512) {                        //if the joyX value is negative     
      digitalWrite(motors, LOW);                          //***reverse the motors***
    }
  }
  
  steeringServo.write(steerVal);                        //write the value of the joyY to the steering servo
}

Again, any and all feedback is welcome!

that won't compile with a period after the closing >

(There are syntax errors in the code like const int sensorTrig - 10; or forgetting the () in a function call, or confusing variables with the same name)

so basically your code won't even compile... so does not work...

Why would you submit this here for comments without even trying to compile first ?

i have fixed the syntax errors, and made some sub-programs. below is the new code.

#include <Servo.h>                  //include the servo library
Servo steeringServo;                //label the steering servo
Servo sensorServo;                  //label the sensor config servo

const int joyDrive = A3;            //label the input pin for fwd/bckw drive
const int joySteer = A2;            //label the input pin for steering
const int joyHorn = A1;             //label the input pin for the horn
const int objFollow = 8;            //label the input pin for object following

const int buzzer = 7;               //label the output pin for the horn
int sound = 1500;                   //state the value of sound

const int objFollowLED = 6;         //label the pin for the object follow indicator LED
const int sensorEcho = 9;           //label the pin for US Sensor echo
const int sensorTrig = 10;          //label the pin the US Sensor trigger
const int motors = 5;               //label the output pin for the motors

const int checkLED = 3;             //label the pin for the check LED

int driveVal;                       //state variable name for fwd/ backw drive
int steerVal;                       //state variable name for the steering
int echoVal;                        //state the variable to store value of distance

int middle = 90;                    //state the value of the middle state of the servos

void setup() {

  steeringServo.attach(12);                     //attach steering servo to pin 12
  sensorServo.attach(11);                       //attach sensor config servo to pin 11
  pinMode(joyDrive, INPUT);                     //declare the joyX as input
  pinMode(joySteer, INPUT);                     //declare the joyY as input
  pinMode(joyHorn, INPUT);                      //declare the joyButton as input
  pinMode(objFollow, INPUT_PULLUP);             //declare the object follow button as input
  pinMode(sensorEcho, INPUT);                   //declare the echo pin as input
  pinMode(buzzer, OUTPUT);                      //declare the buzzer as output
  pinMode(objFollowLED, OUTPUT);                //declare the object follow indicator as output
  pinMode(sensorTrig, OUTPUT);                  //declare the trigger pin as an output
  pinMode(checkLED, OUTPUT);                    //declare the check LED as output

  driveCheck();                                 //call the drive check function
  steeringServo.write(middle);                  //make the servo turn to the middle
  sensorServo.write(middle);                    //make the servo turn to the middle
}

void loop() {

  driveSeq();                       //call the driving sequence
  objFolSeq();                      //call the object follow sequence
}

void driveCheck() {

  digitalWrite(motors, HIGH);                 //drive the motors
  delay(1000);                                //wait 1 sec
  digitalWrite(motors, LOW);                  //***reverse the motors***
  delay(2000);                                //wait 2 secs

  steeringServo.write(180);                   //turn the steering servo left
  sensorServo.write(180);                     //turn the sensor servo left
  delay(1500);                                //wait 1.5 secs
  steeringServo.write(0);                     //turn the steering servo right
  sensorServo.write(0);                       //turn the sensor servo right
  delay(500);                                 //wait 0.5 secs
  steeringServo.write(middle);                //return servo to middle
  sensorServo.write(middle);                  //return servo to middle
  delay(1000);                                //wait 1 sec

  tone(buzzer, 500);                          //sound the horn
  delay(500);                                 //wait 0.5 secs
  noTone(buzzer);                             //stop sounding the horn
  delay(1000);                                //wait 1 sec

  digitalWrite(objFollowLED, HIGH);           //turn on the object follow indicator
  delay(500);                                 //wait 0.5 secs
  digitalWrite(objFollowLED, LOW);            //turn off the object follow indicator

  digitalWrite(checkLED, HIGH);               //flash the check LED to confirm end of system check
  delay(100);                                 //******************************
  digitalWrite(checkLED, LOW);                //****************************
  delay(100);                                 //**************************
  digitalWrite(checkLED, HIGH);               //************************
  delay(100);                                 //**********************
  digitalWrite(checkLED, LOW);                //********************
  delay(100);                                 //******************
  digitalWrite(checkLED, HIGH);               //****************
  delay(100);                                 //**************
  digitalWrite(checkLED, LOW);                //************
  delay(100);                                 //**********
  digitalWrite(checkLED, HIGH);               //********
  delay(100);                                 //******
  digitalWrite(checkLED, LOW);                //****
  delay(100);                                 //**
}

void driveSeq() {

  int driveVal = map(analogRead(joyDrive), 0, 1023, 0, 180);              //set and map the value of joyX
  int steerVal = map(analogRead(joySteer), 0, 1023, 0, 180);              //set and map the value of joyY

  {
    if (analogRead(joyHorn) == HIGH) {                  //if the horn button is pressed
      tone(buzzer, 500);                                       //sound the horn
    } else {                                            //otherwise
      noTone(buzzer);                                     //dont sound the horn
    }
  }

  {
    if (driveVal > 512) {                               //if the joyX value is positive
      digitalWrite(motors, HIGH);                         //drive the motors
    } else if (driveVal < 512) {                        //if the joyX value is negative     
      digitalWrite(motors, LOW);                          //***reverse the motors***
    }
  }
  
  steeringServo.write(steerVal);                        //write the value of the joyY to the steering servo
}

void objFolSeq() {

  long durationindigit, distanceincm;                                     //calculate distance for object following
  digitalWrite(sensorTrig, LOW);                                            //loop the sequence to measure distance
  delayMicroseconds(2);                                                     //********
  digitalWrite(sensorTrig, HIGH);                                           //******
  delayMicroseconds(10);                                                    //****
  digitalWrite(sensorTrig, LOW);                                            //**

  int durationInDigit = pulseIn(sensorEcho, HIGH);                        //state the rebound duration
  int distanceIncm = (durationInDigit * 0.034) / 2;                       //state the distance in cm from the rebound duration

  {
    if (digitalRead(objFollow == HIGH)) {                                 //if the the object follow button is pushed
      if (distanceIncm >= 30) {                                             //and the distance to the object is out of range
        digitalWrite(motors, LOW);                                          //don't drive motors
      } else if (distanceIncm < 30 && distanceIncm > 10) {                  //if the distance to the object is in range
        digitalWrite(motors, HIGH);                                         //drive motors
      } else if (distanceIncm < 10) {                                       //if the distance is too close
        digitalWrite(motors, LOW);                                          //don'd drive the motors
      }
    } else if (digitalRead(objFollow == LOW)) {                           //if the object follow button is not pushed
      driveSeq();                                                         //carry out the drive sequence
    }
}

some issues that i am encountering are:

  1. i cant here the buzzer sounding before my confirmation light flashes
  2. the steering servo turns another 90 degrees to the right after the confirmation light flashes

i understand that i may not be able to reverse the motors without a motor control unit -- which i don't have

here is my current circuit diagram if it will help...

well - your code is sequential and full of delays...

things happen one after the other.

You need to code that very differently

learn about how to handle code in an asynchronous way

you might benefit from studying state machines also. Here is a small introduction to the topic: Yet another Finite State Machine introduction

1 Like

1. Connect your DC Motors using L298 Motor Driver as per Fig-1.


Figure-1:

2. Use seperate 5V supply for the Servo Motors.

The way to do a big project is to break it into a bunch of small projects. Get them all working individually and then tie them together.

So the answer to your question, "would the code work?" is no. It's too complicated to get working as one big project. Object following is complicated. RC control is complicated. You need to break it into a series of smaller tasks.

2 Likes

Thank you. I’ll do that.

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