HC-05 receiving data from Android but not sending to Androidr

I was trying to build a car which can be 1) controlled manually (through remote as well as bluetooth, which are working fine), 2) put to obstacle avoiding mode,
3) put to line following mode

Schematic (very rough diagram, I have tried my best, I am new to all this):

Code (I took various codes from the net and tried to mix them according to my project):

Edit: I have updated the code and made a lot of changes in the code (but the wiring remains same).

The Code:

#include <IRremote.h>
#include <SoftwareSerial.h>
#include <Servo.h>    //Servo motor library. This is standard library
#include <NewPing.h>  //Ultrasonic sensor function library. You must install this library
#include "DHT.h"

//==============================================
//     Data  Definitions
//==============================================

#define ttp223 8
#define buzzer 12

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 500;   // the debounce time; increase if the output flickers

#define DHTPIN A0      // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11  // DHT 11
DHT dht(DHTPIN, DHTTYPE);


//==============================================
//     Manual Control Definitions
//==============================================

SoftwareSerial BTSerial(10, 11);  // pin10->TX of BT, pin11->RX of BT
#define irPin 9
IRrecv irrecv(irPin);
decode_results rslts;

//==============================================
//     Line Follower Definitions
//==============================================

#define R_S A6  //ir sensor Right
#define L_S A7  //ir sensor Left

//==============================================
//     Obstacle Avoiding Definitions
//==============================================

#define trig_pin A2  //analog input 1
#define echo_pin A1  //analog input 2

#define maximum_distance 200

NewPing sonar(trig_pin, echo_pin, maximum_distance);  //sensor function
Servo servo_motor;                                    //our servo name

boolean goesForward = false;
int distance = 100;


//==============================================
//     General Definitions
//==============================================


// Motor A
#define enA 6
#define in1 7
#define in2 4

// Motor B
#define enB 5
#define in3 3
#define in4 2

//PWM 3, 5, 6, 9, 10, 11

String irNum;
String btNum = "o";
bool send = true;

int mode = 0;
int c = 1;
String data = "";
String dht11_value = "";
bool fetch = false;

void BTControl();
void IRControl();
void ObstacleControl();
void LineControl();

void Forward();
void Back();
void Left();
void Right();
void Stop();
void fetchSensorData();

void (*functionModePointers[4])(){
  BTControl, IRControl, ObstacleControl, LineControl
};

void (*functionMovesPointers[5])(){
  Forward, Back, Left, Right, Stop
};

void setup() {

  //==============================================
  //     Motor Setup
  //==============================================

  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);


  //==============================================
  //     Data  Setup
  //==============================================


  dht.begin();

  //==============================================
  //     Manual Control Setup
  //==============================================


  Serial.begin(9600);

  pinMode(ttp223, INPUT);
  irrecv.enableIRIn();

  BTSerial.begin(9600);
  Serial.println("Enter AT commands:");

  //==============================================
  //     Line Follower Setup
  //==============================================

  pinMode(R_S, INPUT);
  pinMode(L_S, INPUT);

  //==============================================
  //     Obstacle Avoiding Setup
  //==============================================

  servo_motor.attach(13);  //our servo pin

  servo_motor.write(120);
  delay(2000);
  distance = readPing();
  delay(100);
}


void loop() {

  int ttpData = digitalRead(ttp223);
  if (ttpData == HIGH && (millis() - lastDebounceTime) > debounceDelay) {
    lastDebounceTime = millis();
    mode = (mode + 1) % 4;
    Serial.println(mode);
    // delay(1000);
    send = true;
  }


  if (send == true) {
    data = "mode after d is entered= " + String(mode);
    Serial.println(data);
    BTSerial.println(data);

    if (dht11_value != "")
      BTSerial.println(dht11_value);
    send = false;

    // Serial.println("send is false");
  }

  functionModePointers[mode]();  //working
}

int decode(String data) {

  if (data == "T" || data == "44")
    return 0;
  else if (data == "B" || data == "1d")
    return 1;
  else if (data == "L" || data == "1c")
    return 2;
  else if (data == "R" || data == "48")
    return 3;
  else if (data == "OK" || data == "O" || data == "K" || data == "5c")
    return 4;
  else if (data == "d" || data == "D")
    return 5;
  else if (data == "s" || data == "S") {
    return 6;
    send = true;
  }
}

void BTControl() {
  while (BTSerial.available()) {
    // Serial.write(BTSerial.read()); //writes full words to serial monitor
    char incomingByte = BTSerial.read();
    if (incomingByte >= 97 && incomingByte <= 122) {
      btNum = incomingByte;
      send = true;
    }
    Serial.println(btNum);
  }
  if (decode(btNum) == 5 && fetch != true) {
    // fetchSensorData();
    fetch = true;
  } 
  else if (decode(btNum) != 5){
    functionMovesPointers[decode(btNum)]();  //not working
    fetch = false;
  } 
}

void IRControl() {
  if (irrecv.decode()) {
    Serial.println(rslts.value);
    String irNum = String(irrecv.decodedIRData.command, HEX);
    Serial.println(irrecv.decodedIRData.command, HEX);
    Serial.println(irNum);
    irrecv.resume();
  }
  // Add a small delay to avoid overwhelming the serial buffer
  delay(50);
}

void ObstacleControl() {
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20) {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);
    distanceRight = lookRight();
    delay(300);

    if (distance >= distanceLeft) {
      turnRight();
      moveStop();
    } else {
      turnLeft();
      moveStop();
    }
  } else {
    moveForward();
  }
  distance = readPing();
}

void LineControl() {
  if ((digitalRead(R_S) == 0) && (digitalRead(L_S) == 0)) { Forward(); }  //if Right Sensor and Left Sensor are at White color then it will call forword function
  if ((digitalRead(R_S) == 1) && (digitalRead(L_S) == 0)) { Right(); }    //if Right Sensor is Black and Left Sensor is White then it will call turn Right function
  if ((digitalRead(R_S) == 0) && (digitalRead(L_S) == 1)) { Left(); }     //if Right Sensor is White and Left Sensor is Black then it will call turn Left function
  if ((digitalRead(R_S) == 1) && (digitalRead(L_S) == 1)) { Stop(); }     //if Right Sensor and Left Sensor are at Black color then it will call Stop function
}

void fetchSensorData() {
  // delay(2000);
  float h = dht.readHumidity();
}

//==============================================
//     Manual Control Functions
//==============================================

void Forward() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 150);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);
}

void Back() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

void Right() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);
}

void Left() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);
}

void Stop() {
  analogWrite(enA, 0);
  analogWrite(enB, 0);
}

//==============================================
//     Obstacle Avoiding Functions
//==============================================

int lookRight() {
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int lookLeft() {
  servo_motor.write(180);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

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

void moveStop() {
  digitalWrite(in3, LOW);
  digitalWrite(in1, LOW);
  digitalWrite(in4, LOW);
  digitalWrite(in2, LOW);
}

void moveForward() {
  if (!goesForward) {
    goesForward = true;

    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    analogWrite(enA, 150);

    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    analogWrite(enB, 150);
  }
}

void moveBackward() {
  goesForward = false;

  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

void turnRight() {

  //turning right little
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);

  delay(500);

  //moving backward little
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

void turnLeft() {

  //turning left little
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  analogWrite(enB, 150);

  delay(500);

  //moving backward little
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 150);

  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 150);
}

My Tries:

(i) Even if the condition if (decode(btNum) == 5 && fetch != true) {} in BTControl () is false and the function fetchSensorData() is not executed (i.e the line fetchSensorData(); is it not commented) then also HC05 is not sending.

(ii) in BTControl (), if I comment the line fetchSensorData(); then it works perfectly fine.

(iii) I also added delay (2000) to the function fetchSensorData() , that also didnt work.

(iv) I also tried the DHT Sensors Non-Blocking Library...that also didnt work.

Please help me solve this issue. Thanks a lot..

If your diagram is complete, you're missing a GND connection from Arduino to everything else.

1 Like

Post the picture of the Hobby Car with which your items of post #1 will be engaged,

Tell clearly which Module is not worikng the way you want.

In the above, you have said that Car is working fine!

Not so, he's saying BT and IR are working fine; the "working fine" relates to content within the (), hence "working fine" is within the ().

I am sorry for the mistake. All the GND connections are there.

Guys, I am so sorry for the confusion. I meant to say that my android phone can control the movements of the car with the help of hc05 (like forward, backward, left turn, right turn, stop). But it is unable to send data to my android device terminal (i am using a bluetooth serial terminal app from playstore). I have listed what I have tried...to solve the issue...in my question (plz see, thanks).

Thanks a lot for the help..

Then please post an updated drawing in a new message, so that the erroneous one doesn't continue to render confusion in the thread.

What signal do you want to send from Arduino NANO to the Bluetooth Terminal of your Android Phone?

Hope I did not miss any wiring connection

Currently, I can send data from phone to arduino (any single character).

But arduino, through HC05, is unable to send any character / string to phone.

Actually I wanted to send back the temperature and humidity data (collected from the DTH11 sensor) to phone when I send "D or d" character from my phone to arduino through HC05.

1. You have wanted to mean that you wish to send 'D' or 'd' as a command to the Arduino NANO; in responese, the ARduino NANO will send Temperature and Humidity signal of DHT11 sensor to the Bluetooth Terminal of your Android Phone -- is this correct?

2. Can you show the Temperature and Humidity value of DHT11 sensor on Serial Monitor?

3. Check that your Bluetooth Wiring with NANO agrees with Fig-1:
HC510-11Nano
Figure-1: