Arduino RC car project having issues

Thread Starter

Musty95

Joined Aug 23, 2022
1
Hello, so as the title says I am currently making a RC car as a little project while I try to learn Arduino more. The car is a Bluetooth controlled car controlled using a HC-06 Bluetooth Module. My main issue currently is that I have made the design minus a schematic, made the code for the basics of the car and how it will run, having lights, working with the Bluetooth application via an android phone etc, but I am wanting to now add a ultrasonic sensor, HC-Sr04 to be exact, along with a buzzer to make a parking sensor for the car. But here's the issue, the prior code worked perfectly for the simple version, but now that I am adding the code that I created for the parking sensor, nothing works anymore. As you can assume, the power and components are fine and everything still powers up, but when trying to actually make the car move with the application nothing happens.

I am kind of stumped on this one currently, as I don't know if its something obvious that I am missing or if I have messed up parts of the code or if a part of the old code is clashing with the new code maybe, which is why I could use some help if anyone spots anything that sticks out that could be causing this.

But just a rundown on the car itself as I don't have a schematic, I have a 12v supply which connects to two switches, 1 switch powers the Arduino board plugging directly into the socket, while the other powers the 12v of 1 of 2 of the L298N drivers, which then just feeds into the other driver. I have the 5v of the driver connecting to the Vin of the Arduino board, followed by the 5v from the Arduino board connecting to a breadboard which supplies power to the Bluetooth module, Ultrasonic sensor and the LED's just so they aren't connected directly to the supply. The input and enable pins from both L298N drivers connect up to the digital pins from 2-13, followed by the last 2 digital pins 0 and 1 housing the Bluetooth modules TXD and RXD. Now with all of the digital pins being used, I am having to use the Analog in on the Arduino for the LED's which is fine, along with the Buzzer and the ultrasonic sensors echo and trig pins. I am not 100% sure if I have my code correct for Analog which could be causing the issue? Although I am still getting used to Arduino so I'm kind of learning as I go.

I will leave my code below in parts just so it is easy to read. Id appreciate anyone's help with this as not being able to figure out the issue is driving me crazy haha

Fully working code prior to adding the new code:
#define ENA_m1 5        // Enable A/speed of the Front Right motor
#define ENB_m1 6        // Enable B/speed of the Back Right motor
#define ENA_m2 10       // Enable A/speed of the Front Left motor
#define ENB_m2 11       // Enable B/speed of the Back Left motor

#define IN_11  2        // L298N #1 input 1 for the Front Right motor
#define IN_12  3        // L298N #1 input 2 for the Front Right motor
#define IN_13  4        // L298N #1 input 3 for the Back Right motor
#define IN_14  7        // L298N #1 input 4 for the Back Right motor

#define IN_21  8        // L298N #2 input 1 for the Front Left motor
#define IN_22  9        // L298N #2 input 2 for the Front Left motor
#define IN_23  12       // L298N #2 input 3 for the Back Left motor
#define IN_24  13       // L298N #2 input 4 for the Back Left motor

int command;            //Int to store app command state
int speedCar = 100;     // Speed ranges from 50 - 255
int speed_Coeff = 4;
boolean lightFront = false;
boolean lightBack = false;

void setup() {
 
    pinMode(light_FRFL, OUTPUT);
    pinMode(light_BRBL, OUTPUT);
   
    pinMode(ENA_m1, OUTPUT);
    pinMode(ENB_m1, OUTPUT);
    pinMode(ENA_m2, OUTPUT);
    pinMode(ENB_m2, OUTPUT);

    pinMode(IN_11, OUTPUT);
    pinMode(IN_12, OUTPUT);
    pinMode(IN_13, OUTPUT);
    pinMode(IN_14, OUTPUT);
   
    pinMode(IN_21, OUTPUT);
    pinMode(IN_22, OUTPUT);
    pinMode(IN_23, OUTPUT);
    pinMode(IN_24, OUTPUT);
   
    Serial.begin(9600);

  }

void goAhead(){

      digitalWrite(IN_11, HIGH);
      digitalWrite(IN_12, LOW);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, LOW);
      digitalWrite(IN_14, HIGH);
      analogWrite(ENB_m1, speedCar);

      digitalWrite(IN_21, LOW);
      digitalWrite(IN_22, HIGH);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, HIGH);
      digitalWrite(IN_24, LOW);
      analogWrite(ENB_m2, speedCar);

  }

void goBack(){

      digitalWrite(IN_11, LOW);
      digitalWrite(IN_12, HIGH);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, HIGH);
      digitalWrite(IN_14, LOW);
      analogWrite(ENB_m1, speedCar);

      digitalWrite(IN_21, HIGH);
      digitalWrite(IN_22, LOW);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, LOW);
      digitalWrite(IN_24, HIGH);
      analogWrite(ENB_m2, speedCar);

  }

void goRight(){

      digitalWrite(IN_11, LOW);
      digitalWrite(IN_12, HIGH);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, HIGH);
      digitalWrite(IN_14, LOW);
      analogWrite(ENB_m1, speedCar);

      digitalWrite(IN_21, LOW);
      digitalWrite(IN_22, HIGH);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, HIGH);
      digitalWrite(IN_24, LOW);
      analogWrite(ENB_m2, speedCar);

  }

void goLeft(){

      digitalWrite(IN_11, HIGH);
      digitalWrite(IN_12, LOW);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, LOW);
      digitalWrite(IN_14, HIGH);
      analogWrite(ENB_m1, speedCar);

       
      digitalWrite(IN_21, HIGH);
      digitalWrite(IN_22, LOW);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, LOW);
      digitalWrite(IN_24, HIGH);
      analogWrite(ENB_m2, speedCar);

       
  }

void goAheadRight(){
     
      digitalWrite(IN_11, HIGH);
      digitalWrite(IN_12, LOW);
      analogWrite(ENA_m1, speedCar/speed_Coeff);

      digitalWrite(IN_13, LOW);
      digitalWrite(IN_14, HIGH);
      analogWrite(ENB_m1, speedCar/speed_Coeff);

      digitalWrite(IN_21, LOW);
      digitalWrite(IN_22, HIGH);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, HIGH);
      digitalWrite(IN_24, LOW);
      analogWrite(ENB_m2, speedCar);

  }

void goAheadLeft(){
     
      digitalWrite(IN_11, HIGH);
      digitalWrite(IN_12, LOW);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, LOW);
      digitalWrite(IN_14, HIGH);
      analogWrite(ENB_m1, speedCar);

      digitalWrite(IN_21, LOW);
      digitalWrite(IN_22, HIGH);
      analogWrite(ENA_m2, speedCar/speed_Coeff);

      digitalWrite(IN_23, HIGH);
      digitalWrite(IN_24, LOW);
      analogWrite(ENB_m2, speedCar/speed_Coeff);

  }

void goBackRight(){

      digitalWrite(IN_11, LOW);
      digitalWrite(IN_12, HIGH);
      analogWrite(ENA_m1, speedCar/speed_Coeff);

      digitalWrite(IN_13, HIGH);
      digitalWrite(IN_14, LOW);
      analogWrite(ENB_m1, speedCar/speed_Coeff);

      digitalWrite(IN_21, HIGH);
      digitalWrite(IN_22, LOW);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, LOW);
      digitalWrite(IN_24, HIGH);
      analogWrite(ENB_m2, speedCar);

  }

void goBackLeft(){

      digitalWrite(IN_11, LOW);
      digitalWrite(IN_12, HIGH);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, HIGH);
      digitalWrite(IN_14, LOW);
      analogWrite(ENB_m1, speedCar);

      digitalWrite(IN_21, HIGH);
      digitalWrite(IN_22, LOW);
      analogWrite(ENA_m2, speedCar/speed_Coeff);

      digitalWrite(IN_23, LOW);
      digitalWrite(IN_24, HIGH);
      analogWrite(ENB_m2, speedCar/speed_Coeff);

  }

void stopRobot(){

      digitalWrite(IN_11, LOW);
      digitalWrite(IN_12, LOW);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, LOW);
      digitalWrite(IN_14, LOW);
      analogWrite(ENB_m1, speedCar);


      digitalWrite(IN_21, LOW);
      digitalWrite(IN_22, LOW);
      analogWrite(ENA_m2, speedCar);

     
      digitalWrite(IN_23, LOW);
      digitalWrite(IN_24, LOW);
      analogWrite(ENB_m2, speedCar);

  }

void loop(){

if (Serial.available() > 0) {
  command = Serial.read();
  stopRobot();      //Initialize with motors stopped

if (lightFront) {digitalWrite(light_FRFL, HIGH);}
if (!lightFront) {digitalWrite(light_FRFL, LOW);}
if (lightBack) {digitalWrite(light_BRBL, HIGH);}
if (!lightBack) {digitalWrite(light_BRBL, LOW);}


switch (command) {
case 'F':goAhead();break;
case 'B':goBack();break;
case 'L':goLeft();break;
case 'R':goRight();break;
case 'I':goAheadRight();break;
case 'G':goAheadLeft();break;
case 'J':goBackRight();break;
case 'H':goBackLeft();break;
case '0':speedCar = 100;break;
case '1':speedCar = 115;break;
case '2':speedCar = 130;break;
case '3':speedCar = 145;break;
case '4':speedCar = 160;break;
case '5':speedCar = 175;break;
case '6':speedCar = 190;break;
case '7':speedCar = 205;break;
case '8':speedCar = 220;break;
case '9':speedCar = 235;break;
case 'q':speedCar = 255;break;
case 'W':lightFront = true;break;
case 'w':lightFront = false;break;
case 'U':lightBack = true;break;
case 'u':lightBack = false;break;

}
}
}
Just to mention that the below code is just the new code that will be added to the above code, I will show this added to the old code in another section.
New code which is to be implemented for the Ultrasonic Sensor:
#define trigPin A2       //Ultrasound pin A2 for Arduino Uno;
#define echoPin A3       //Ultrasound pin A3 for Arduino Uno;
#define buzPin A4


float speed = 0.0347;   //declare speed of sound in air @ room temp;
float dist;             //declare variable for containing distance sensed;
float pingTime;         //declare variable for containing echo time;
int buzNear = 20;       //declare buzzing time for very close proximity;
int buzHigh = 50;       //declare buzzing time for close proximity;
int buzMid  =130;       //declare buzzing time for mid proximity;
int buzFar = 500;       //declare buzzing time for far off object;
int delayFar = 260;

void setup()  {
pinMode(buzPin,OUTPUT);     //set buzzer pin as output;
    pinMode(trigPin, OUTPUT);   //Set trigger pin as output;    
    pinMode(echoPin, OUTPUT);    //set echo pin as input;
    }
   
    void loop(){

  digitalWrite(trigPin,LOW);
  delayMicroseconds(20);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);          //creating a pulse for sensing distance;
  pingTime = pulseIn(echoPin,HIGH);   //read the echoTime, &hence the distance;
  dist = (speed*pingTime*0.5);

  if(dist<=10.0){
    digitalWrite(buzPin,HIGH);         //conditional statements changing frequency based upon the distance sensed;
    delay(20);
    digitalWrite(buzPin,LOW);
    delay(20);
  }
  else if(dist<=20.0 && dist>10.0)
  {
    digitalWrite(buzPin,HIGH);
    delay(buzHigh);
    digitalWrite(buzPin,LOW);
    delay(buzHigh);
  }
  else if((dist>20.0) && (dist<40.0))
  {
    digitalWrite(buzPin,HIGH);
    delay(buzMid);
    digitalWrite(buzPin,LOW);
    delay(buzMid);
  }
  else if(dist>=40.0 && dist<80.0)
  {
    digitalWrite(buzPin,HIGH);
    delay(buzFar);
    digitalWrite(buzPin,LOW);
    delay(delayFar);
  }
Final set of code is both the old and new code together:
#define light_FRFL  A0    //For the Front Right LED  pin A0 for Arduino Uno
#define light_BRBL  A1    //For the Back Right LED   pin A1 for Arduino Uno
#define trigPin A2       //Ultrasound pin A2 for Arduino Uno;
#define echoPin A3       //Ultrasound pin A3 for Arduino Uno;
#define buzPin A4

#define ENA_m1 5        // Enable A/speed of the Front Right motor
#define ENB_m1 6        // Enable B/speed of the Back Right motor
#define ENA_m2 10       // Enable A/speed of the Front Left motor
#define ENB_m2 11       // Enable B/speed of the Back Left motor

#define IN_11  2        // L298N #1 input 1 for the Front Right motor
#define IN_12  3        // L298N #1 input 2 for the Front Right motor
#define IN_13  4        // L298N #1 input 3 for the Back Right motor
#define IN_14  7        // L298N #1 input 4 for the Back Right motor

#define IN_21  8        // L298N #2 input 1 for the Front Left motor
#define IN_22  9        // L298N #2 input 2 for the Front Left motor
#define IN_23  12       // L298N #2 input 3 for the Back Left motor
#define IN_24  13       // L298N #2 input 4 for the Back Left motor

int command;            //Int to store app command state
int speedCar = 100;     // Speed ranges from 50 - 255
int speed_Coeff = 4;
boolean lightFront = false;
boolean lightBack = false;
float speed = 0.0347;   //declare speed of sound in air @ room temp;
float dist;             //declare variable for containing distance sensed;
float pingTime;         //declare variable for containing echo time;
int buzNear = 20;       //declare buzzing time for very close proximity;
int buzHigh = 50;       //declare buzzing time for close proximity;
int buzMid  =130;       //declare buzzing time for mid proximity;
int buzFar = 500;       //declare buzzing time for far off object;
int delayFar = 260;

void setup() {
 
    pinMode(light_FRFL, OUTPUT);
    pinMode(light_BRBL, OUTPUT);
   
    pinMode(ENA_m1, OUTPUT);
    pinMode(ENB_m1, OUTPUT);
    pinMode(ENA_m2, OUTPUT);
    pinMode(ENB_m2, OUTPUT);

    pinMode(IN_11, OUTPUT);
    pinMode(IN_12, OUTPUT);
    pinMode(IN_13, OUTPUT);
    pinMode(IN_14, OUTPUT);
   
    pinMode(IN_21, OUTPUT);
    pinMode(IN_22, OUTPUT);
    pinMode(IN_23, OUTPUT);
    pinMode(IN_24, OUTPUT);
   
    pinMode(buzPin,OUTPUT);     //set buzzer pin as output;
    pinMode(trigPin, OUTPUT);   //Set trigger pin as output;    
    pinMode(echoPin, OUTPUT);    //set echo pin as input;
 

  Serial.begin(9600);

  }

void goAhead(){

      digitalWrite(IN_11, HIGH);
      digitalWrite(IN_12, LOW);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, LOW);
      digitalWrite(IN_14, HIGH);
      analogWrite(ENB_m1, speedCar);

      digitalWrite(IN_21, LOW);
      digitalWrite(IN_22, HIGH);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, HIGH);
      digitalWrite(IN_24, LOW);
      analogWrite(ENB_m2, speedCar);

  }

void goBack(){

      digitalWrite(IN_11, LOW);
      digitalWrite(IN_12, HIGH);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, HIGH);
      digitalWrite(IN_14, LOW);
      analogWrite(ENB_m1, speedCar);

      digitalWrite(IN_21, HIGH);
      digitalWrite(IN_22, LOW);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, LOW);
      digitalWrite(IN_24, HIGH);
      analogWrite(ENB_m2, speedCar);

  }

void goRight(){

      digitalWrite(IN_11, LOW);
      digitalWrite(IN_12, HIGH);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, HIGH);
      digitalWrite(IN_14, LOW);
      analogWrite(ENB_m1, speedCar);

      digitalWrite(IN_21, LOW);
      digitalWrite(IN_22, HIGH);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, HIGH);
      digitalWrite(IN_24, LOW);
      analogWrite(ENB_m2, speedCar);

  }

void goLeft(){

      digitalWrite(IN_11, HIGH);
      digitalWrite(IN_12, LOW);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, LOW);
      digitalWrite(IN_14, HIGH);
      analogWrite(ENB_m1, speedCar);

       
      digitalWrite(IN_21, HIGH);
      digitalWrite(IN_22, LOW);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, LOW);
      digitalWrite(IN_24, HIGH);
      analogWrite(ENB_m2, speedCar);

       
  }

void goAheadRight(){
     
      digitalWrite(IN_11, HIGH);
      digitalWrite(IN_12, LOW);
      analogWrite(ENA_m1, speedCar/speed_Coeff);

      digitalWrite(IN_13, LOW);
      digitalWrite(IN_14, HIGH);
      analogWrite(ENB_m1, speedCar/speed_Coeff);

      digitalWrite(IN_21, LOW);
      digitalWrite(IN_22, HIGH);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, HIGH);
      digitalWrite(IN_24, LOW);
      analogWrite(ENB_m2, speedCar);

  }

void goAheadLeft(){
     
      digitalWrite(IN_11, HIGH);
      digitalWrite(IN_12, LOW);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, LOW);
      digitalWrite(IN_14, HIGH);
      analogWrite(ENB_m1, speedCar);

      digitalWrite(IN_21, LOW);
      digitalWrite(IN_22, HIGH);
      analogWrite(ENA_m2, speedCar/speed_Coeff);

      digitalWrite(IN_23, HIGH);
      digitalWrite(IN_24, LOW);
      analogWrite(ENB_m2, speedCar/speed_Coeff);

  }

void goBackRight(){

      digitalWrite(IN_11, LOW);
      digitalWrite(IN_12, HIGH);
      analogWrite(ENA_m1, speedCar/speed_Coeff);

      digitalWrite(IN_13, HIGH);
      digitalWrite(IN_14, LOW);
      analogWrite(ENB_m1, speedCar/speed_Coeff);

      digitalWrite(IN_21, HIGH);
      digitalWrite(IN_22, LOW);
      analogWrite(ENA_m2, speedCar);

      digitalWrite(IN_23, LOW);
      digitalWrite(IN_24, HIGH);
      analogWrite(ENB_m2, speedCar);

  }

void goBackLeft(){

      digitalWrite(IN_11, LOW);
      digitalWrite(IN_12, HIGH);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, HIGH);
      digitalWrite(IN_14, LOW);
      analogWrite(ENB_m1, speedCar);

      digitalWrite(IN_21, HIGH);
      digitalWrite(IN_22, LOW);
      analogWrite(ENA_m2, speedCar/speed_Coeff);

      digitalWrite(IN_23, LOW);
      digitalWrite(IN_24, HIGH);
      analogWrite(ENB_m2, speedCar/speed_Coeff);

  }

void stopRobot(){

      digitalWrite(IN_11, LOW);
      digitalWrite(IN_12, LOW);
      analogWrite(ENA_m1, speedCar);

      digitalWrite(IN_13, LOW);
      digitalWrite(IN_14, LOW);
      analogWrite(ENB_m1, speedCar);


      digitalWrite(IN_21, LOW);
      digitalWrite(IN_22, LOW);
      analogWrite(ENA_m2, speedCar);

     
      digitalWrite(IN_23, LOW);
      digitalWrite(IN_24, LOW);
      analogWrite(ENB_m2, speedCar);

  }

void loop(){

  digitalWrite(trigPin,LOW);
  delayMicroseconds(20);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);          //creating a pulse for sensing distance;
  pingTime = pulseIn(echoPin,HIGH);   //read the echoTime, &hence the distance;
  dist = (speed*pingTime*0.5);

  if(dist<=10.0){
    digitalWrite(buzPin,HIGH);         //conditional statements changing frequency based upon the distance sensed;
    delay(20);
    digitalWrite(buzPin,LOW);
    delay(20);
  }
  else if(dist<=20.0 && dist>10.0)
  {
    digitalWrite(buzPin,HIGH);
    delay(buzHigh);
    digitalWrite(buzPin,LOW);
    delay(buzHigh);
  }
  else if((dist>20.0) && (dist<40.0))
  {
    digitalWrite(buzPin,HIGH);
    delay(buzMid);
    digitalWrite(buzPin,LOW);
    delay(buzMid);
  }
  else if(dist>=40.0 && dist<80.0)
  {
    digitalWrite(buzPin,HIGH);
    delay(buzFar);
    digitalWrite(buzPin,LOW);
    delay(delayFar);
  }

  if (Serial.available() > 0) {
  command = Serial.read();
  stopRobot();      //Initialize with motors stopped

if (lightFront) {digitalWrite(light_FRFL, HIGH);}
if (!lightFront) {digitalWrite(light_FRFL, LOW);}
if (lightBack) {digitalWrite(light_BRBL, HIGH);}
if (!lightBack) {digitalWrite(light_BRBL, LOW);}


switch (command) {
case 'F':goAhead();break;
case 'B':goBack();break;
case 'L':goLeft();break;
case 'R':goRight();break;
case 'I':goAheadRight();break;
case 'G':goAheadLeft();break;
case 'J':goBackRight();break;
case 'H':goBackLeft();break;
case '0':speedCar = 100;break;
case '1':speedCar = 115;break;
case '2':speedCar = 130;break;
case '3':speedCar = 145;break;
case '4':speedCar = 160;break;
case '5':speedCar = 175;break;
case '6':speedCar = 190;break;
case '7':speedCar = 205;break;
case '8':speedCar = 220;break;
case '9':speedCar = 235;break;
case 'q':speedCar = 255;break;
case 'W':lightFront = true;break;
case 'w':lightFront = false;break;
case 'U':lightBack = true;break;
case 'u':lightBack = false;break;

}
}
}
 

Ya’akov

Joined Jan 27, 2019
9,152
Welcome to AAC.

With the proviso that I only had a couple of minutes to review your code, here are sone observations.

First, your various move commands are coded with a lot of redundancy. You might want ot consider making just one function that accepts parameters for all of the motors, then pass those parameters to the function from your switch statement. If Arduino’s C++ version implemets std::map you can make it even nicer by using a named or associative array so the call can just be to the named direction and the values can be taken from the array.

Second, you are using delay() in a loop that also has to do other things. Blocking operations like that are are sure way to have unexpected problems. Instead, you need to use a state machine strategy, that uses the millis() function to continuously loop checking the clock to see if the delay time has exported. In this way, you are constantly dealing with the other parts of the loop that need attention. (There are innumerable tutorials on this approach, a search for “Arduino state machine” will let you choose one that appeals).

On troubleshooting your problem, you need to instrument your code. That is to say you need to be able to see what your code thinks it’s being told to do. This is usually and most easily done with print statement to the serial monitor. You can also use spare GPIO pins to light LEDs corresponding to different code bucks being executed to see where it might be going off the rails. The prints statements, though, are probably the most common method of tacking variable values.

Are the commands from the app making it into the command variable?
Is the BT actually being received? (just after the serial ready conditional you can print that it was true).
Is the command variable ever actually set?
Is the command variable still set when it enters the switch statement?

I think you get the idea. You need more information. Working blind is a frustrating guessing game, collect some data.
 
Top