vehicles avoid automatic obstacles and have bluetooth controls

I use an:

  • 01 HC-SR04 ultrasonic sensor
  • 02 Infrared sensor avoids obstacles
  • 01 module bluetooth HC-05
  • 01 mudule infrared control
    I want to control simultaneously bluetooth and infrared automatic when driving, so how? I need help people's! sincerely thanks!
    I wrote the code below:

#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A0
#define ECHO_PIN A1
#define RS A3
#define LS A4
#define MAX_DISTANCE 200
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
boolean goesForward=false;
int distance = 100;
int speedSet = 0;

void setup() {

pinMode(RS, INPUT);
pinMode(RS, INPUT);
myservo.attach(9);
myservo.write(80);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}

void loop() {
int distanceR = 0;
int distanceL = 0;
delay(40);
if(distance<=30)
{
moveStop();
moveBackward();
delay(200);
moveStop();
delay(100);
distanceR = lookRight();
delay(100);
distanceL = lookLeft();
delay(100);

if(distanceR>=distanceL)
{
turnRight();
moveStop();
}else
{
turnLeft();
moveStop();
}
}else
{
moveForward();
if(digitalRead(LS) && !(digitalRead(RS)))
{
motor1.run(FORWARD); motor1.setSpeed(255);
motor2.run(BACKWARD); motor2.setSpeed(255);
motor3.run(BACKWARD); motor3.setSpeed(255);
motor4.run(FORWARD); motor4.setSpeed(255);
delay(100);
moveForward();
}
if(!(digitalRead(LS)) && digitalRead(RS))
{
motor1.run(BACKWARD);
motor1.setSpeed(255);
motor2.run(FORWARD);
motor2.setSpeed(255);
motor3.run(FORWARD);
motor3.setSpeed(255);
motor4.run(BACKWARD);
motor4.setSpeed(255);
delay(100);
moveForward();
}
}
distance = readPing();
}

int lookRight()
{
myservo.write(30);
delay(400);
int distance = readPing();
delay(100);
myservo.write(80);
return distance;
}

int lookLeft()
{
myservo.write(150);
delay(400);
int distance = readPing();
delay(100);
myservo.write(80);
return distance;
delay(100);
}

int readPing() {
delay(70);
int cm = sonar.ping_cm();
if(cm==0)
{
cm = 250;
}
return cm;
}

void moveStop() {

motor1.run(RELEASE); motor1.setSpeed(0);
motor2.run(RELEASE); motor2.setSpeed(0);
motor3.run(RELEASE); motor3.setSpeed(0);
motor4.run(RELEASE); motor4.setSpeed(0);
}
void moveForward() {

motor1.run(FORWARD); motor1.setSpeed(255);
motor2.run(FORWARD); motor2.setSpeed(255);
motor3.run(FORWARD); motor3.setSpeed(255);
motor4.run(FORWARD); motor4.setSpeed(255);
}
void moveBackward() {

motor1.run(BACKWARD); motor1.setSpeed(255);
motor2.run(BACKWARD); motor2.setSpeed(255);
motor3.run(BACKWARD); motor3.setSpeed(255);
motor4.run(BACKWARD); motor4.setSpeed(255);
}
void turnRight() {

motor1.run(BACKWARD); motor1.setSpeed(255);
motor2.run(FORWARD); motor2.setSpeed(255);
motor3.run(FORWARD); motor3.setSpeed(220);
motor4.run(BACKWARD); motor4.setSpeed(255);
delay(300);
moveForward();
}
void turnLeft() {
motor1.run(FORWARD); motor1.setSpeed(255);
motor2.run(BACKWARD); motor2.setSpeed(255);
motor3.run(BACKWARD); motor3.setSpeed(255);
motor4.run(FORWARD); motor4.setSpeed(255);
delay(300);
moveForward();
}

Yes, you do.

Post your code like this:

#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A0
#define ECHO_PIN A1
#define RS A3
#define LS A4
#define MAX_DISTANCE 200
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo;   
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
boolean goesForward=false;
int distance = 100;
int speedSet = 0;

void setup() {
   
  pinMode(RS, INPUT);
  pinMode(RS, INPUT);
  myservo.attach(9); 
  myservo.write(80);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop() {
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
  if(distance<=30)
 {
  moveStop();
  moveBackward();
  delay(200);
  moveStop();
  delay(100);
  distanceR = lookRight();
  delay(100);
  distanceL = lookLeft();
  delay(100);

  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }else
  {
    turnLeft();
    moveStop();
  }
 }else
 {
  moveForward();
  if(digitalRead(LS) && !(digitalRead(RS)))
  {
      motor1.run(FORWARD);       motor1.setSpeed(255);
      motor2.run(BACKWARD);      motor2.setSpeed(255);
      motor3.run(BACKWARD);      motor3.setSpeed(255);       
      motor4.run(FORWARD);       motor4.setSpeed(255);
      delay(100);
      moveForward();
  }
  if(!(digitalRead(LS)) && digitalRead(RS))
  {
      motor1.run(BACKWARD);
      motor1.setSpeed(255);
      motor2.run(FORWARD);
      motor2.setSpeed(255);
      motor3.run(FORWARD); 
      motor3.setSpeed(255);     
      motor4.run(BACKWARD);
      motor4.setSpeed(255);
      delay(100);
      moveForward();
  }
 }
 distance = readPing();
}

int lookRight()
{
    myservo.write(30);
    delay(400);
    int distance = readPing();
    delay(100);
    myservo.write(80);
    return distance;
}

int lookLeft()
{
    myservo.write(150);
    delay(400);
    int distance = readPing();
    delay(100);
    myservo.write(80);
    return distance;
    delay(100);
}

int readPing() {
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}

void moveStop() {
   
      motor1.run(RELEASE);      motor1.setSpeed(0);
      motor2.run(RELEASE);      motor2.setSpeed(0);
      motor3.run(RELEASE);      motor3.setSpeed(0);
      motor4.run(RELEASE);      motor4.setSpeed(0);
  }
  void moveForward() {

      motor1.run(FORWARD);       motor1.setSpeed(255);
      motor2.run(FORWARD);       motor2.setSpeed(255);
      motor3.run(FORWARD);       motor3.setSpeed(255);
      motor4.run(FORWARD);       motor4.setSpeed(255);
}
void moveBackward() {
   
      motor1.run(BACKWARD);      motor1.setSpeed(255);
      motor2.run(BACKWARD);      motor2.setSpeed(255);
      motor3.run(BACKWARD);      motor3.setSpeed(255);
      motor4.run(BACKWARD);      motor4.setSpeed(255); 
} 
void turnRight() {

      motor1.run(BACKWARD);      motor1.setSpeed(255);
      motor2.run(FORWARD);       motor2.setSpeed(255);
      motor3.run(FORWARD);       motor3.setSpeed(220);     
      motor4.run(BACKWARD);      motor4.setSpeed(255);
  delay(300);
 moveForward();     
}
 void turnLeft() {
      motor1.run(FORWARD);       motor1.setSpeed(255);
      motor2.run(BACKWARD);      motor2.setSpeed(255);
      motor3.run(BACKWARD);      motor3.setSpeed(255);       
      motor4.run(FORWARD);       motor4.setSpeed(255);
  delay(300);
   moveForward();
}

Paul

I want to control simultaneously bluetooth and infrared automatic when driving, so how?

You tell us. What you describe is like having two people yelling directions while you drive a car blindfolded. Turn left. No, turn right. Stop. No, hit the gas.

Would drive me nuts. Either you want the car to drive itself, avoiding obstacles, OR you want to drive it, and YOU make it avoid obstacles.