Problem with Arduino Sensor Car

I've recently started with building a car which I control remotely with an App I created on the MIT App Inventor website. If I were to use it just to control it remotely I wouldn't have any kind of problem, for I tried it like that and it worked perfectly.

This is how I programmed my app and how it looks on the phone:

Adelante= Forward
Atras= Backwards
Deretcha=Right
Izquierda= Left

However, I tried to make some changes on it and sort of implement "emergency measures". Basically, I added a front sensor to the robot and gave it new orders:

  1. If the Robot felt an obstacle from 40 to 20 centimeters away it just would be able to back up through the controls, that way he wouldn't be able to crash into something.

  2. If the Robot felt an obstacle less than 20 centimeters away it would start backing up automatically; which means that if something like this happened it would mean that the unknown object was advancing towards the Robot.

  3. If the Robot didn't sense anything on more than 40 cm away, then the person controlling the robot would be able to use the controls.

And this is how I did it:

int state=0;

const int trigPin1 = 10;
const int echoPin1 = 7;
// defines variables
long duration;
int distance;
int angle = 0;  

void setup()
{
  //Setup Channels
  pinMode(12, OUTPUT); //Motor Channel A
  pinMode(9, OUTPUT); //Brake Channel A 
  pinMode(13, OUTPUT); //Motor Channel B
  pinMode(8, OUTPUT); //Brake Channel B
  pinMode(trigPin1, OUTPUT); 
  pinMode(echoPin1, INPUT); 

  digitalWrite(12, HIGH);
  digitalWrite(13, LOW); // Connection is inverted.
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}


void loop() 
{
     // Clears the trigPin1
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(200);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin1, LOW);
  // Reads the echoPin1, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin1, HIGH);
  // Calculating the distance
  distance= duration*0.034/2;
  Serial.println(distance);
    //if some data is sent, reads it and saves in state
    
    if(Serial.available() > 0)
    {     
      state = Serial.read();   
      
    }   
      
          
    if (distance > 40)
  {

     switch (state) 
     {
      
      case 'F':
        delayMicroseconds(100);
        digitalWrite(12, HIGH);
        digitalWrite(9, LOW); //Brake is off
        digitalWrite(8, LOW); // Brake is off
        digitalWrite(13, LOW);
         
        analogWrite(3, 255);   //Spins the motor on Channel A at full speed
        analogWrite(11, 255);   //Spins the motor on Channel B at full speed
        break;
        
      case 'B':
        delayMicroseconds(100);
        digitalWrite(12, LOW);
        digitalWrite(9, LOW); 
        digitalWrite(8, LOW);
        digitalWrite(13, HIGH);

        analogWrite(3, 255);   //Spins the motor on Channel A at full speed
        analogWrite(11, 255);   //Spins the motor on Channel B at full speed   
        break;
        
      case 'R':
        delayMicroseconds(100);
        digitalWrite(12, HIGH);
        digitalWrite(9, LOW); //A
        digitalWrite(8, LOW);//B
        digitalWrite(13, LOW);//B
        
        analogWrite(11, 90);   //Spins the motor on Channel B at full speed  
        analogWrite(3, 255);   //Spins the motor on Channel A at low speed
        break;
        
      case 'L':
        delayMicroseconds(100);
        digitalWrite(12, HIGH); //A
        digitalWrite(9, LOW); //A
        digitalWrite(8, LOW);//B
        digitalWrite(13, LOW);//B
 
        analogWrite(3, 90);   //Spins the motor on Channel A at low speed
        analogWrite(11, 255);   //Spins the motor on Channel B at full speed  
        break;
        
      case '3':
         delayMicroseconds(100);
         digitalWrite(12, HIGH); //A
         digitalWrite(9, LOW); //A
         digitalWrite(8, LOW);//B
         digitalWrite(13, HIGH);//B
 
         analogWrite(3, 255);   //Spins the motor on Channel A at full speed
         analogWrite(11, 255);   //Spins the motor on Channel B at full speed  
        break;
        
      case '2':
        delayMicroseconds(100);
        digitalWrite(12, LOW); //A
        digitalWrite(9, LOW); //A
        digitalWrite(8, LOW);//B
        digitalWrite(13, LOW);//B
 
        analogWrite(3, 255);   //Spins the motor on Channel A at full speed
        analogWrite(11, 255);   //Spins the motor on Channel B at full speed  
        break;
        
      case 'S':
        delayMicroseconds(100);
        digitalWrite(9, HIGH); 
        digitalWrite(8, HIGH);
        break;
        
      case '0':
        delayMicroseconds(100);
        digitalWrite(9, HIGH); 
        digitalWrite(8, HIGH);
        break;
     }

    }

      
    if (distance < 40 && distance > 20) {
    
     switch (state) {

       case 'B':
         delayMicroseconds(100);
         digitalWrite(12, LOW);
         digitalWrite(9, LOW); 
         digitalWrite(8, LOW);
         digitalWrite(13, HIGH);

         analogWrite(3, 255);   //Spins the motor on Channel A at full speed
         analogWrite(11, 255);   //Spins the motor on Channel B at full speed 
        break;

        default:
         delayMicroseconds(100);
         digitalWrite(9, HIGH); 
         digitalWrite(8, HIGH);
        break;
      }
   

    }

if (distance < 20) {
   
         delayMicroseconds(100);
         digitalWrite(9, HIGH); 
         digitalWrite(8, HIGH);
  
      }
   

}

When I implement the last code I have a problem, at first if there aren't any objects nearby then the robot works.

Then, when it finds an object ranging from 40 to 20 cm it just stops, which is supposed to do, and if I try to make it back up with the phone it responds and does as I told it to. However, I can just use that action once because when I stop pressing the button everything stops working, I cannot make it back up or go forward, nor do anything else. The app also seems to stop working.

Moreover, if I decide to make another object advance towards it so it backs up automatically it seems to follow code, but then doesn't stop even when it is too further away from the object.

I tried to see if maybe it was the distance sensor was the one that wasn't working, and I plugged the robot into the computer to see if it correctly read the distance. And it did, I even decided to change it and try it with a newer one I had, just in case. I've also tried changing the code but it doesn't seem to work no matter what I do. Any ideas would be helpful.

Thanks

I am just picking this piece of code because it is simple and typical of the rest of your program and will be suitable to make some points

if (distance < 20) {
  
         delayMicroseconds(100);
         digitalWrite(9, HIGH);
         digitalWrite(8, HIGH);
  
      }

Probably the most important thing is that you have the testing of the distance and the action all rolled into one. That makes it very difficult to unravel the actions when you send another command. A much better way to handle things is to have a variable to keep track of the state of the system. Maybe two variables makes more sense - one for the state of the obstacle detection and one for the state of the command selected by the user.

Then the action can take place depending on the states of the variables. For example, all the directions might be available if there is no obstacle. But if there is a medium distance obstacle maybe only reverse is available etc.

An example of what I mean is code something like this for forward

void moveForward() {
   if (direction == 'F' and obstacle == 'N') {
      // commands to make the motor move forwards
   }
}

Doing it that way should automatically make FWD available again as soon as the car is far enough from the obstacle. I am assuming there is another function to set the value of the variable obstacle - something like

void checkObstacle() {
  if (distance > x) {
     obstacle = 'N'; // N for none
   }
   else if (distance > y) {
     obstacle = 'C';  // C for close
   }
   else {
     obstacle = 'A'; // A for approaching
   }
}

Another, separate point I would like to make is how you handle the pins in this code

digitalWrite(9, HIGH);
digitalWrite(8, HIGH);

Your code would be much easier to develop and maintain if you give names to the numbers. For example. at the top of the program

byte leftMotorPin = 9;  // if my names are wrong, use something suitable
byte rightMotorPin = 8;

and then

digitalWrite(leftMotorPin, HIGH);
digitalWrite(rightMotorPin, HIGH);

Finally, if you want a responsive program do not use delay() or delayMicroseconds(). If you really need timing intervals then use millis() ( or micros() ) to manage the timing without blocking as illustrated in Several Things at a Time

...R
Planning and Implementing a Program

Thanks! I changed my code completely following your other post and Planning and Implementing a Programm. Which I found extremely helpful.

This was the result;

int state=0;

const int trigPin1 = 10;
const int echoPin1 = 7;
// defines variables
long duration;
int distance;
int angle = 0; 
int Fobstacle;


const int LeftMotor= 12;
const int RightMotor= 13;
const int LeftBrake= 9;
const int RightBrake= 8;
const int LeftGo= 3;
const int RightGo= 11;

void setup() {
  //Setup Channels
  pinMode(LeftMotor, OUTPUT); //Motor Channel A
  pinMode(LeftBrake, OUTPUT); //Brake Channel A 
  pinMode(RightMotor, OUTPUT); //Motor Channel B
  pinMode(RightBrake, OUTPUT); //Brake Channel B
  pinMode(trigPin1, OUTPUT); 
  pinMode(echoPin1, INPUT); 

  digitalWrite(LeftMotor, HIGH);
  digitalWrite(RightMotor, LOW); // Connection is inverted.
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {


  checkFrontObstacle();
  checkDirection();
  
  moveForward();
  moveBackwards();
  moveRight();
  moveLeft();
  nowStop();
  turnRight();
  turnLeft();
  
}


void moveForward() {
  if (state == 'F' && Fobstacle == 'N') {
     delayMicroseconds(100);
     digitalWrite(LeftMotor, HIGH);
     digitalWrite(LeftBrake, LOW); //Brake is off
     digitalWrite(RightBrake, LOW); // Brake is off
     digitalWrite(RightMotor, LOW);
         
     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed
     }
  }
   
void moveBackwards() {
  if (state == 'B' && Fobstacle == 'N' ){
     delayMicroseconds(100); 
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW); 
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed 
     }
  if (state == 'B' && Fobstacle == 'C' ){
     delayMicroseconds(100); 
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW); 
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed
     }
  else if (Fobstacle == 'A'){
     delayMicroseconds(100);
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW); 
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed   
     }
  }
   
void moveRight() {
  if (state == 'R'&& Fobstacle == 'N'){
      delayMicroseconds(100);
      digitalWrite(LeftMotor, HIGH);
      digitalWrite(LeftBrake, LOW); //A
      digitalWrite(RightBrake, LOW);//B
      digitalWrite(RightMotor, LOW);//B
        
      analogWrite(RightGo, 90);   //Spins the motor on Channel B at full speed  
      analogWrite(LeftGo, 255);   //Spins the motor on Channel A at low speed
      }
  }
void moveLeft() {
  if (state == 'L' && Fobstacle == 'N'){
      delayMicroseconds(100);
      digitalWrite(LeftMotor, HIGH); //A
      digitalWrite(LeftBrake, LOW); //A
      digitalWrite(RightBrake, LOW);//B
      digitalWrite(RightMotor, LOW);//B
 
      analogWrite(LeftGo, 90);   //Spins the motor on Channel A at low speed
      analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed  
     }
  }
   
void turnRight() {
   if(state == '3' && Fobstacle == 'N'){
      delayMicroseconds(100);
      digitalWrite(LeftMotor, HIGH); //A
      digitalWrite(LeftBrake, LOW); //A
      digitalWrite(RightBrake, LOW);//B
      digitalWrite(RightMotor, HIGH);//B
 
      analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
      analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed 
     }
   }
void turnLeft() {
   if(state == '2' && Fobstacle == 'N'){
      delayMicroseconds(100);
      digitalWrite(LeftMotor, LOW); //A
      digitalWrite(LeftBrake, LOW); //A
      digitalWrite(RightBrake, LOW);//B
      digitalWrite(RightMotor, LOW);//B
 
      analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
      analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed  
     }
   }
   
void nowStop() {
   if(state == '0'){
     delayMicroseconds(100);
     digitalWrite(LeftBrake, HIGH); 
     digitalWrite(RightBrake, HIGH);
     }
   }

   
void checkFrontObstacle() {
  
     // Clears the trigPin1
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(200);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin1, LOW);
  // Reads the echoPin1, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin1, HIGH);
  // Calculating the distance
  distance= duration*0.034/2;
    //if some data is sent, reads it and saves in distance


   if (distance > 40) {
     Fobstacle = 'N'; // N for none
    }
   if (distance < 40 && distance > 20 ) {
     Fobstacle = 'C';  // C for close
    }
   if (distance < 20 ) {
     Fobstacle = 'A'; // A for approaching
    }
    
   }

   
void checkDirection(){
  if(Serial.available() >= 0)
    {     
      state = Serial.read();   
    }   
   }

Now I am able to make the car function properly, even when it comes to "If the Robot felt an obstacle from 40 to 20 centimeters away it just would be able to back up through the controls, that way it wouldn't be able to crash into something. "

However, when it comes to something "Advancing" towards the robot it goes two ways:

  1. It goes completely haywire, to the point that doesn't respond to the phone commands.

  2. Starts backing up and doesn't stop. Only if I tell it to with the phone. At that point when it does stop and I tell it to go forward, even if it had worked before, it starts going backward again, almost if this part of the code had been overwritten

void moveForward() {
  if (state == 'F' && Fobstacle == 'N') {
     delayMicroseconds(100);
     digitalWrite(LeftMotor, HIGH);
     digitalWrite(LeftBrake, LOW); //Brake is off
     digitalWrite(RightBrake, LOW); // Brake is off
     digitalWrite(RightMotor, LOW);
         
     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed
     }
  }

by this other part

void moveBackwards() {
  if (state == 'B' && Fobstacle == 'N' ){
     delayMicroseconds(100); 
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW); 
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed 
     }
  if (state == 'B' && Fobstacle == 'C' ){
     delayMicroseconds(100); 
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW); 
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed
     }
  else if (Fobstacle == 'A'){
     delayMicroseconds(100);
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW); 
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed   
     }
  }

My guess is that I'm having problems with this part of the code

  else if (Fobstacle == 'A'){
     delayMicroseconds(100);
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW); 
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed   
     }

And so I have even tried to change it with using

 else if (Fobstacle =='A' && state > 0){}

But that didn't work because I am trying to make it back up automatically until the distance is wide enough and this only makes it manual. However, it does make the robot work at all times.

I would appreciate any other kind of idea to make it work.

Thanks

Why have you the same code three times in this piece

void moveBackwards() {
  if (state == 'B' && Fobstacle == 'N' ){
     delayMicroseconds(100);
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW);
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed
     }
  if (state == 'B' && Fobstacle == 'C' ){
     delayMicroseconds(100);
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW);
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed
     }
  else if (Fobstacle == 'A'){
     delayMicroseconds(100);
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW);
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed   
     }
  }

I think this is logically identical

void moveBackwards() {
    if (Fobstacle == 'A' or state == 'B') {
         delayMicroseconds(100);
         digitalWrite(LeftMotor, LOW);
         digitalWrite(LeftBrake, LOW);
         digitalWrite(RightBrake, LOW);
         digitalWrite(RightMotor, HIGH);

         analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
         analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed
    }
}

I don't know if that will solve the problem - but it should simplify the search for it :slight_smile:

Why is the RightMotor being treated differently from the LeftMotor?

You don't have any Serial.print() statements in the code so you can monitor how any of the variables are changing.

...R

Well, I tried making said changes that you told me but the robot just went entirely mad, it would have similar problems to the ones I had at the start so I tried to remake the whole code entirely, and tested it out one step at a time to see if I could find the source of my problems.

I just ended up changing a bit the app by replacing the letters it was supposed to send from the app (F, R, L, B, S....) to numbers. The result? The app wouldn't stop malfunctioning and freezing on its own. I went back to the initial app and the initial code to undo everything but now the robot doesn't even respond correctly, it does just the same as it did with the newer app.

This is the code I'm actually working on, I don't understand why this is happening, maybe I've messed up big time somewhere in there but I cannot see it.

int state=0;
long duration;
int distance;
int Fobstacle;

const int trigPin1 = 10;
const int echoPin1 = 7;
const int LeftMotor= 12;
const int RightMotor= 13;
const int LeftBrake= 9;
const int RightBrake= 8;
const int LeftGo= 3;
const int RightGo= 11;

void setup() {
  //Setup Channels
  pinMode(LeftMotor, OUTPUT); //Motor Channel A
  pinMode(LeftBrake, OUTPUT); //Brake Channel A 
  pinMode(RightMotor, OUTPUT); //Motor Channel B
  pinMode(RightBrake, OUTPUT); //Brake Channel B
  pinMode(trigPin1, OUTPUT); 
  pinMode(echoPin1, INPUT); 

  digitalWrite(LeftMotor, HIGH);
  digitalWrite(RightMotor, LOW); // Connection is inverted.
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {




  checkFrontObstacle();
  checkDirection();
  
  moveForward();
  moveBackwards();
  moveRight();
  moveLeft();
  nowStop();
  turnRight();
  turnLeft();


  
}


void moveForward() {
  if (state == '1' && Fobstacle == 'N') {
     delayMicroseconds(100);
     digitalWrite(LeftMotor, HIGH);
     digitalWrite(LeftBrake, LOW); //Brake is off
     digitalWrite(RightBrake, LOW); // Brake is off
     digitalWrite(RightMotor, LOW);
         
     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed
     }
  }
   
void moveBackwards() {
  if (state == '2' && Fobstacle == 'N'){
     delayMicroseconds(100); 
     digitalWrite(LeftMotor, LOW);
     digitalWrite(LeftBrake, LOW); 
     digitalWrite(RightBrake, LOW);
     digitalWrite(RightMotor, HIGH);

     analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
     analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed 
     }

  }

void moveLeft() {
  if (state == '3' && Fobstacle == 'N'){
      delayMicroseconds(100);
      digitalWrite(LeftMotor, HIGH); //A
      digitalWrite(LeftBrake, LOW); //A
      digitalWrite(RightBrake, LOW);//B
      digitalWrite(RightMotor, LOW);//B
 
      analogWrite(LeftGo, 90);   //Spins the motor on Channel A at low speed
      analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed  
     }
  }
   
void moveRight() {
  if (state == '4' && Fobstacle == 'N'){
      delayMicroseconds(100);
      digitalWrite(LeftMotor, HIGH);
      digitalWrite(LeftBrake, LOW); //A
      digitalWrite(RightBrake, LOW);//B
      digitalWrite(RightMotor, LOW);//B
        
      analogWrite(RightGo, 90);   //Spins the motor on Channel B at full speed  
      analogWrite(LeftGo, 255);   //Spins the motor on Channel A at low speed
      }
   }
   
void turnRight() {
   if(state == '5' && Fobstacle == 'N'){
      delayMicroseconds(100);
      digitalWrite(LeftMotor, HIGH); //A
      digitalWrite(LeftBrake, LOW); //A
      digitalWrite(RightBrake, LOW);//B
      digitalWrite(RightMotor, HIGH);//B
 
      analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
      analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed 
     }
   }

   void turnLeft() {
   if(state == '6' && Fobstacle == 'N'){
      delayMicroseconds(100);
      digitalWrite(LeftMotor, LOW); //A
      digitalWrite(LeftBrake, LOW); //A
      digitalWrite(RightBrake, LOW);//B
      digitalWrite(RightMotor, LOW);//B
 
      analogWrite(LeftGo, 255);   //Spins the motor on Channel A at full speed
      analogWrite(RightGo, 255);   //Spins the motor on Channel B at full speed  
     }
   }



   
void nowStop() {
   if(state == '9'){
     delayMicroseconds(100);
     digitalWrite(LeftBrake, HIGH); 
     digitalWrite(RightBrake, HIGH);
     }

   }

   

   
void checkDirection(){
  if(Serial.available() >= 0)
    {     
      state = Serial.read();   
    }   
   }
   



void checkFrontObstacle() {
  
     // Clears the trigPin1
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(200);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin1, LOW);
  // Reads the echoPin1, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin1, HIGH);
  // Calculating the distance
  distance= duration*0.034/2;
    //if some data is sent, reads it and saves in distance

Serial.println(distance);

   if (distance >= 40) {
     Fobstacle = 'N'; // N for none
    }
   if (distance < 40 && distance >= 20 ) {
     Fobstacle = 'C';  // C for close
    }
   if (distance < 20 ) {
     Fobstacle = 'A'; // A for approaching
    }

   }

Also, I've considered the possibility that the problem comes from the app, which would make a lot of sense. If so, does anybody know anywhere where I can build an app like MIT App Inventor? If not I will have to get into code as well.

By the way, the motors are inverted because when I welded them to the cables I didn't notice my mistake until the very end.

Thanks

It won't make the slightest difference whether you use '1' '2' '3' etc or 'F' 'B' 'L' etc. Just use whichever you prefer - personally I like the letters because they are informative.

You seem to have no code in your program to tell you what characters are actually being received.

Make a backup copy of your program and then delete all the directions except FWD and STOP and concentrate on getting those working reliably.

...R