When the robot car is in Bluetooth mode, all the wheels move with every command

Hi guys, help me fix this problem pls. Thx

`#include <DHT.h>

#define DHT_PIN A0
#define DHT_TYPE DHT11
DHT dht(DHT_PIN, DHT_TYPE);

const int Trig1 = 13;
const int Echo1 = 12;
const int Trig2 = 11;
const int Echo2 = 10;
const int Trig3 = 9;
const int Echo3 = 8;
#define maximumm_distance 200

const int motorR_EN = 2;
const int motorL_EN = 3;

const int motorR_PIN1 = 4;
const int motorR_PIN2 = 5;
const int motorL_PIN1 = 6;
const int motorL_PIN2 = 7;
const int ABS = 200;
int Left_Distance = 0;
int Right_Distance = 0;
int Middle_Distance = 0;


char command;
bool autonomousModeEnabled = false; // Variable to track autonomous mode


void setup() {

  Serial.begin(9600);

  pinMode(Trig1, OUTPUT);
  pinMode(Echo1, INPUT);
  pinMode(Trig2, OUTPUT);
  pinMode(Echo2, INPUT);
  pinMode(Trig3, OUTPUT);
  pinMode(Echo3, INPUT);

  pinMode(motorR_PIN1, OUTPUT);
  pinMode(motorR_PIN2, OUTPUT);
  pinMode(motorL_PIN1, OUTPUT);
  pinMode(motorL_PIN2, OUTPUT);
  pinMode(motorR_EN, OUTPUT);
  pinMode(motorL_EN, OUTPUT);

  dht.begin();

  Stop();
}

void handleCommand(char command) {
  if (command == 'F') {
    Forward();
    autonomousModeEnabled = false;  // Disable autonomous mode
  } else if (command == 'B') {
    Backward();
    autonomousModeEnabled = false;  // Disable autonomous mode
  } else if (command == 'L') {
    Left();
    autonomousModeEnabled = false;  // Disable autonomous mode
  } else if (command == 'R') {
    Right();
    autonomousModeEnabled = false;  // Disable autonomous mode
  } else if (command == 'S') {
    Stop();
    autonomousModeEnabled = false;  // Disable autonomous mode
  } else if (command == 'A') {
    autonomousModeEnabled = true;  // Enable autonomous mode
  }
}

void Stop() {
  digitalWrite(motorR_PIN1, LOW);
  digitalWrite(motorR_PIN2, LOW);
  digitalWrite(motorL_PIN1, LOW);
  digitalWrite(motorL_PIN2, LOW);
  analogWrite(motorR_EN, ABS);
  analogWrite(motorL_EN, ABS);
}

void Forward() {
  digitalWrite(motorR_PIN1, HIGH);
  digitalWrite(motorR_PIN2, LOW);
  digitalWrite(motorL_PIN1, HIGH);
  digitalWrite(motorL_PIN2, LOW);
  analogWrite(motorR_EN, ABS);
  analogWrite(motorL_EN, ABS);
}

void Backward() {
  digitalWrite(motorR_PIN1, LOW);
  digitalWrite(motorR_PIN2, HIGH);
  digitalWrite(motorL_PIN1, LOW);
  digitalWrite(motorL_PIN2, HIGH);
  analogWrite(motorR_EN, ABS);
  analogWrite(motorL_EN, ABS);
}

void Right() {
  digitalWrite(motorR_PIN1, LOW);
  digitalWrite(motorR_PIN2, HIGH);
  digitalWrite(motorL_PIN1, HIGH);
  digitalWrite(motorL_PIN2, LOW);
  analogWrite(motorR_EN, ABS);
  analogWrite(motorL_EN, ABS);
}

void Left() {
  digitalWrite(motorR_PIN1, HIGH);
  digitalWrite(motorR_PIN2, LOW);
  digitalWrite(motorL_PIN1, LOW);
  digitalWrite(motorL_PIN2, HIGH);
  analogWrite(motorR_EN, ABS);
  analogWrite(motorL_EN, ABS);
}

int Left_Distance_test() {
  digitalWrite(Trig2, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig2, HIGH);
  delayMicroseconds(20);
  digitalWrite(Trig2, LOW);
  float Fdistance = pulseIn(Echo2, HIGH);
  delay(10);

  Fdistance = Fdistance / 29 / 2;
  return (int)Fdistance;
}

int Middle_Distance_test() {
  digitalWrite(Trig3, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig3, HIGH);
  delayMicroseconds(20);
  digitalWrite(Trig3, LOW);
  float Fdistance = pulseIn(Echo3, HIGH);
  delay(10);

  Fdistance = Fdistance / 29 / 2;
  return (int)Fdistance;
}

int Right_Distance_test() {
  digitalWrite(Trig1, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig1, HIGH);
  delayMicroseconds(20);
  digitalWrite(Trig1, LOW);
  float Fdistance = pulseIn(Echo1, HIGH);
  delay(10);

  Fdistance = Fdistance / 29 / 2;
  return (int)Fdistance;
}

void loop() {

  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor");
  } else {
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print(" °C, Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");
  }


  if (Serial.available() > 0) {
    command = Serial.read();
    handleCommand(command);
  }

  if (autonomousModeEnabled) {
    autonomousMode();
  }
}

void autonomousMode() {
  Left_Distance = Left_Distance_test();
  delay(10);
  Middle_Distance = Middle_Distance_test();
  delay(10);
  Right_Distance = Right_Distance_test();
  delay(10);

  if (Middle_Distance <= 20) {
    if (Right_Distance > Left_Distance) {
      if ((Right_Distance <= 10) && (Left_Distance <= 10)) {
        Stop();
        delay(200);
        Backward();
        delay(1000);
      } else {
        Right();
        delay(500);
      }
    } else if (Right_Distance < Left_Distance) {
      if ((Right_Distance <= 10) && (Left_Distance <= 10)) {
        Stop();
        delay(200);
        Backward();
        delay(1000);
      } else {
        Left();
        delay(500);
      }
    }
  } else {
    if (Right_Distance <= 15) {
      Left();
      delay(100);
    } else if (Left_Distance <= 15) {
      Right();
      delay(100);
    } else {
      Forward();
    }
  }
}
`

You should put your question in the post.

Print the character received from Bluetooth.

All your wheels should move because they are all connected to and commanded by the motor driver with the enable pins set to 200 (near maximum). Note which direction the motors are spinning.

@xfpd. Hard as D0 and D1 are used for the HC05....

I see. Thank you.

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