Strange Bluetooth Problem

Hi guys, thank you for your time.
I’m building up a toy boat that works with 3 motors, 2 of them are connected to a L298N bridge, and the other one works thanks to a transistor (it’s located on the back of the boat). However, the problem is that when I press any direction on my phone (it works by Bluetooth), the motors start going backwards and after a while they get the right direction. Here is the full code (skip the first bit due it’s just the music). I’d appreciate any kind of suggestion. I know is something with the ultrasonic sensor (HC-SR04), but I can’t figure exactly what. In the code, the “Atras” function is what makes it go backwards. Thank you!

// Altavoz
int speakerPin = 9;

//Sensor ultrasonidos
const int trigPin = A2;
const int echoPin = A4;

//Iluminación (no tira LED)

int LDR = A0;
int luz;
int ledLDR = A5;
int ledDER = 7;
int ledIZQ = 8;
SoftwareSerial BT(2, 3);

// Motor A
int ENA = 6;
int IN1 = 13;
int IN2 = 12;

// Motor B
int ENB = 5;
int IN3 = 11;
int IN4 = 10;

// MOTOR C

int btransistor = 4;
int vel = 190;

void GameOfThrones() {
 for(int i=0; i<4; i++) {
    tone(speakerPin, NOTE_G4);

    delay(500);
    noTone(speakerPin);
 
    tone(speakerPin, NOTE_C4);
  
    delay(500);
    noTone(speakerPin);
 

    tone(speakerPin, NOTE_DS4);
   
    delay(250);
    noTone(speakerPin);


    tone(speakerPin, NOTE_F4);

    delay(250);
    noTone(speakerPin);
 
  }

 /* for(int i=0; i<4; i++){
    tone(speakerPin, NOTE_G4);
   
    delay(500);
    noTone(speakerPin);
   

    tone(speakerPin, NOTE_C4);
  
    delay(500);
    noTone(speakerPin);
  

    tone(speakerPin, NOTE_E4);

    delay(250);
    noTone(speakerPin);
   

    tone(speakerPin, NOTE_F4);
    
    delay(250);
    noTone(speakerPin);
  
  } */

   

}

void setup()
{
  BT.begin(9600) ;
  // Declaramos todos los pines como salidas
  pinMode (ENA, OUTPUT);
  pinMode (ENB, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (IN3, OUTPUT);
  pinMode (IN4, OUTPUT);
  pinMode (ledLDR, OUTPUT);
  pinMode (LDR, INPUT);
  pinMode (ledDER, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(ledIZQ, OUTPUT);
  pinMode(btransistor, OUTPUT);
  GameOfThrones();
  Serial.begin(9600);
}

void loop()
{
  luz = analogRead(LDR);
 
  if(luz<600){
    digitalWrite(ledLDR, HIGH);
  }
  if(luz>600){
    digitalWrite(ledLDR, LOW);
    
  }
  
  long duration, dcm;

 
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

 
  dcm = microsecondsToCentimeters(duration);
Serial.print(dcm);
 if(dcm<2){
  Atras();
 }


  if (BT.available()){
    switch (BT.read())
    {
      case 'F':
        Adelante();
       break;
      case 'B':
        Atras();
        break;
      case 'R':
        Derecha();
       break;
      case 'L':
        Izquierda();
       break;
      case 'S':
        Parar();
        break;
    
}
}
delay(1);
 
}

void Adelante ()
{
  //Direccion motor A
  digitalWrite (IN1, HIGH);
  digitalWrite (IN2, LOW);
analogWrite (ENA, vel); //Velocidad motor A
  digitalWrite(btransistor, HIGH);
  digitalWrite(ledDER, LOW);
  digitalWrite(ledIZQ, LOW);
  
  //Direccion motor B
  digitalWrite (IN3, HIGH);
  digitalWrite (IN4, LOW);
  analogWrite (ENB, vel); //Velocidad motor B

  //Direccion motor C
  
 
}

void Atras ()
{
  //Direccion motor A
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
 analogWrite (ENA, vel);  //Velocidad motor A
  digitalWrite(btransistor, LOW);
  digitalWrite(ledDER, LOW);
  digitalWrite(ledIZQ, LOW);
  //Direccion motor B
  digitalWrite (IN3, LOW);
  digitalWrite (IN4, HIGH);
  analogWrite (ENB, vel); //Velocidad motor B
   //Direccion motor C
  
}

void Derecha ()
{
  //Direccion motor A
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
  analogWrite (ENA, vel); //Velocidad motor A
   digitalWrite(btransistor, LOW);
    digitalWrite(ledDER, HIGH);
    digitalWrite(ledIZQ, LOW);
  //Direccion motor B
  digitalWrite (IN3, HIGH);
  digitalWrite (IN4, LOW);
  analogWrite (ENB, vel); //Velocidad motor A
}

void Izquierda ()
{
  //Direccion motor A
  digitalWrite (IN1, HIGH);
  digitalWrite (IN2, LOW);
  analogWrite (ENA, vel); //Velocidad motor A
   digitalWrite(btransistor, LOW);
   digitalWrite(ledDER, LOW);
    digitalWrite(ledIZQ, HIGH);
  //Direccion motor B
  digitalWrite (IN3, LOW);
  digitalWrite (IN4, HIGH);
  analogWrite (ENB, vel); //Velocidad motor A
}

void Parar ()
{
  //Direccion motor A
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, LOW);
  analogWrite (ENA, 0); //Velocidad motor A
   digitalWrite(btransistor, LOW);
  digitalWrite(ledIZQ, LOW);
   digitalWrite(ledDER, LOW);
  //Direccion motor B
  digitalWrite (IN3, LOW);
  digitalWrite (IN4, LOW);
  analogWrite (ENB, 0); //Velocidad motor A
  

}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

I suspect your code to call the Atras() function at line 137 is the problem.

I reckon you should get the serial data as the first thing in loop() and then make a decision based on the serial data and the variable dcm as to what should happen.

As a separate issue I do not like the style switch (BT.read()) because it does not allow you to view the received value for debugging. I would always make that 2 (or 4) separate lines as in

dataReceived = BT,read()
// Serial,print("BT value ");
// Serial.println(dataReceived);
switch(dataReceived)

You may also like to look at Serial Input Basics.

...R

I’ve made some changes to the code but it still doesn’t work as expected. When it’s close to the object it DOES goes backwards, that’s what I wanted, but then when I press on the phone a direction it goes backwards for a while and then gets the direction. I post the new code.

void setup()
{
  BT.begin(9600) ;
  // Declaramos todos los pines como salidas
  pinMode (ENA, OUTPUT);
  pinMode (ENB, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (IN3, OUTPUT);
  pinMode (IN4, OUTPUT);
  pinMode (ledLDR, OUTPUT);
  pinMode (LDR, INPUT);
  pinMode (ledDER, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(ledIZQ, OUTPUT);
  pinMode(btransistor, OUTPUT);
  pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
  GameOfThrones();
  Serial.begin(9600);
}

void loop()
{
  Luz();
  Distancia();


  if (BT.available()){
    switch (BT.read())
    {
      case 'F':
        Adelante();
       break;
      case 'B':
        Atras();
        break;
      case 'R':
        Derecha();
       break;
      case 'L':
        Izquierda();
       break;
      case 'S':
        Parar();
        break;
    
}
}

}

void Adelante ()
{
  //Direccion motor A
  digitalWrite (IN1, HIGH);
  digitalWrite (IN2, LOW);
analogWrite (ENA, vel); //Velocidad motor A
  digitalWrite(btransistor, HIGH);
  digitalWrite(ledDER, LOW);
  digitalWrite(ledIZQ, LOW);
  
  //Direccion motor B
  digitalWrite (IN3, HIGH);
  digitalWrite (IN4, LOW);
  analogWrite (ENB, vel); //Velocidad motor B

  //Direccion motor C
  
 
}

void Atras ()
{
  //Direccion motor A
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
 analogWrite (ENA, vel);  //Velocidad motor A
  digitalWrite(btransistor, LOW);
  digitalWrite(ledDER, LOW);
  digitalWrite(ledIZQ, LOW);
  //Direccion motor B
  digitalWrite (IN3, LOW);
  digitalWrite (IN4, HIGH);
  analogWrite (ENB, vel); //Velocidad motor B
   //Direccion motor C
  
}

void Derecha ()
{
  //Direccion motor A
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
  analogWrite (ENA, vel); //Velocidad motor A
   digitalWrite(btransistor, LOW);
    digitalWrite(ledDER, HIGH);
    digitalWrite(ledIZQ, LOW);
  //Direccion motor B
  digitalWrite (IN3, HIGH);
  digitalWrite (IN4, LOW);
  analogWrite (ENB, vel); //Velocidad motor A
}

void Izquierda ()
{
  //Direccion motor A
  digitalWrite (IN1, HIGH);
  digitalWrite (IN2, LOW);
  analogWrite (ENA, vel); //Velocidad motor A
   digitalWrite(btransistor, LOW);
   digitalWrite(ledDER, LOW);
    digitalWrite(ledIZQ, HIGH);
  //Direccion motor B
  digitalWrite (IN3, LOW);
  digitalWrite (IN4, HIGH);
  analogWrite (ENB, vel); //Velocidad motor A
}

void Parar ()
{
  //Direccion motor A
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, LOW);
  analogWrite (ENA, 0); //Velocidad motor A
   digitalWrite(btransistor, LOW);
  digitalWrite(ledIZQ, LOW);
   digitalWrite(ledDER, LOW);
  //Direccion motor B
  digitalWrite (IN3, LOW);
  digitalWrite (IN4, LOW);
  analogWrite (ENB, 0); //Velocidad motor A
  

}

void Luz(){

    luz = analogRead(LDR);
 
  if(luz<600){
    digitalWrite(ledLDR, HIGH);
  }
  if(luz>600){
    digitalWrite(ledLDR, LOW);
    
  }
}

void Distancia(){
    long duration, dcm;

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  
  duration = pulseIn(echoPin, HIGH);

  dcm = microsecondsToCentimeters(duration);
   if(dcm<2){
  Atras();
 } 
}
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

You did not do what I suggested so you can see what is actually being received. I suspect you will be surprised. Study the link I gave you

...R