my hc05 keeps disconnecting after few seconds

Hi,
I am trying to control a simple robot with 2 motors using a joystick via bluetooth
for this i have used 2 arduino nanos, one connected to the 2 motors of the robot and the other to my joystick
next i have used 2 hc05s, 1 to each arduino and i have binded one hc05 to the other using at commands

also, i have used software serial.
the connections are below

slave module-
hc05 slave- vcc-5v
** gnd-gnd**
** rx-a1 of arduino**
** tx-a0 of arduino**
the motor1 is connected to pins 6 and 7 and motor 2 to pins 8 and 9

hc05 master- vcc-5v
** gnd-gnd**
** rx-a2**
** tx-a3**
joystick connections- vcc-vin
** gnd-gnd**
** vry-a0 of arduino**
** vrx-a1 of arduino**

the codes uploaded are-

SLAVE CODE-

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(14, 15); // RX | TX

int dir = 0;

void setup()
{
  Serial.begin(9600);
  BTSerial.begin(9600);

  pinMode( 6 , OUTPUT);
  pinMode( 7 , OUTPUT);
  pinMode( 8 , OUTPUT);
  pinMode( 9 , OUTPUT);
}

void loop()
{
  if (BTSerial.available() > 0) {
  dir = BTSerial.read(); 
  }

  if (dir == '1') {
  digitalWrite( 6 , LOW);
  digitalWrite( 7 , LOW);
  digitalWrite( 8 , LOW);
  digitalWrite( 9 , LOW);
  }
  if (dir == '2') {
  digitalWrite( 6 , HIGH);
  digitalWrite( 7 , LOW);
  digitalWrite( 8 , LOW);
  digitalWrite( 9 , LOW);
  }
  if (dir == '3') {
  digitalWrite( 6 , LOW);
  digitalWrite( 7 , LOW);
  digitalWrite( 8 , HIGH);
  digitalWrite( 9 , LOW);
  }
  if (dir == '4') {
  digitalWrite( 6 , HIGH);
  digitalWrite( 7 , LOW);
  digitalWrite( 8 , HIGH);
  digitalWrite( 9 , LOW);
  }
  if (dir == '5') {
  digitalWrite( 6 , LOW);
  digitalWrite( 7 , HIGH);
  digitalWrite( 8 , LOW);
  digitalWrite( 9 , HIGH);
  }
}

MASTER CODE-

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(17, 16); // RX | TX

int __ardublockAnalogRead(int pinNumber)
{
  pinMode(pinNumber, INPUT);
  return analogRead(pinNumber);
}

void setup()
{
  Serial.begin(9600);
  BTSerial.begin(9600);
}

void loop()
{
  if (( ( ( ( __ardublockAnalogRead(A1) ) < ( 600 ) ) && ( ( __ardublockAnalogRead(A1) ) > ( 400 ) ) ) && ( ( ( __ardublockAnalogRead(A0) ) > ( 400 ) ) && ( ( __ardublockAnalogRead(A0) ) < ( 600 ) ) ) ))
  {
    BTSerial.write('1');
  }
  if (( ( __ardublockAnalogRead(A0) ) < ( 400 ) ))
  {
    BTSerial.write('4');
  }
  if (( ( ( __ardublockAnalogRead(A0) ) > ( 600 ) ) && ( ( __ardublockAnalogRead(A0) ) < ( 1025 ) ) ))
  {
    BTSerial.write('5');
  }
  if (( ( __ardublockAnalogRead(A1) ) < ( 400 ) ))
  {
    BTSerial.write('2');
  }
  if (( ( ( __ardublockAnalogRead(A1) ) > ( 600 ) ) && ( ( __ardublockAnalogRead(A1) ) < ( 1025 ) ) ))
  {
    BTSerial.write('3');
  }
}

now the real problem is that when i power both on the hc05s successfully pair and everything works
but after few seconds the slave hc05 unpairs and starts rapidly blinking whereas the master hc05 continues blinking the way it was

pls help
and do let me know if you need any other info

I think you are blaming the wrong thing. It is not a matter of HC-05 disconnecting, it HC-05 being disconnected by something else. Your project does work, so your code is presumably kosher. Your problem may be down to intermittent, or inadequate, power, and your innocent Bluetooth is merely a signal to that effect i.e. the first to suffer. HC-05 is not famous for low power consumption. How is the robot powered?

Nick_Pyner:
I think you are blaming the wrong thing. It is not a matter of HC-05 disconnecting, it HC-05 being disconnected by something else. Your project does work, so your code is presumably kosher. Your problem may be down to intermittent, or inadequate, power, and your innocent Bluetooth is merely a signal to that effect i.e. the first to suffer. HC-05 is not famous for low power consumption. How is the robot powered?

hi,
firstly thanks for helping me out, its my first post on this forum :slight_smile:

i had actually considered the reason being low power being provided
but both counter-parts are powered by 2 battery packs each having 6 1.5v AA batteries
i also tried powering the arduinos through my computer
so i dont think power's a problem
but both times the slave module disconnected after 15-30 secs

new update
hello,
I repowered both arduinos a while later and sadly now nothing is working

both hc05 seem paired and are blinking together but other than that nothing is working
i am moving the joystick but the motors arent moving the slightest

it seems as if the master module isnt sending any data to the slave module at all

mudit_agrawal:
both counter-parts are powered by 2 battery packs each having 6 1.5v AA batteries

Just as well I refrained from making any snide comments about using 9v PP3s.. 6xAA sounds reasonable, but they may not be up to scratch if they are powering other robot-type stuff as well as Arduino. And I wouldn't put too much trust in the supply from computer either, so I'm still suss about the power. "Power" also means bad application of good supply, i.e. bad joints or other poor wiring. I understand Nano's regulator isn't as crude as Uno's but it still may not be that great. I'm not familiar with your code, but there are no Strings or w.h.y in there that suggest it might eventually fail.

Nick_Pyner:
Just as well I refrained from making any snide comments about using 9v PP3s.. 6xAA sounds reasonable, but they may not be up to scratch if they are powering other robot-type stuff as well as Arduino. And I wouldn't put too much trust in the supply from computer either, so I'm still suss about the power. "Power" also means bad application of good supply, i.e. bad joints or other poor wiring. I understand Nano's regulator isn't as crude as Uno's but it still may not be that great. I'm not familiar with your code, but there are no Strings or w.h.y in there that suggest it might eventually fail.

hi,
i have dedicated the battery packs to just this project, so i am pretty sure its adequate
i have powered other robots in past with similar battery pack along with bluetooth
secondly i am using shields for both counterparts, so i dont think loose wiring is going to be a problem either
lastly,
the principle behind the master code is that the arduino serial writes 5 integer values based on the direction to joystick is in
and then the slave bt module instructs the arduino to change the direction of rotation based on the value it recieves
currently, the bt modules are connecting but other than that nothing is working
is there some way to read the values the slave module is recieving through software serial
because if there is, it would be a lot easier to pin point the prob if power isnt a issue

I'm not into robots and all I can think of is on-board LCDs that show signal sent and signal received. I might also point out that, when Blueteeth are connected to each other, it might only prove that they both have power and are indeed properly configured, and proves nothing as far as the Arduinos are concerned. The Bluetooth<> Arduino connection may be faulty, or even non-existant.

Nick_Pyner:
I'm not into robots and all I can think of is on-board LCDs that show signal sent and signal received. I might also point out that, when Blueteeth are connected to each other, it might only prove that they both have power and are indeed properly configured, and proves nothing as far as the Arduinos are concerned. The Bluetooth<> ASrduino connection may be faulty, or even non-existant.

thats a good idea, ill connect lcd's and see if the slave module is recieving anything
if that doesnt work, then as you said, there is a great chance that the connection between bt and arduino is faulty
as the bluetooth modules are clearly paired i dont think the issue lies in that

On reflection, you might also note that, IF you are certain that power is not a problem, you could check what's going by moving Blueteeth to hardware serial thereby enabling you to check the serial traffic on the serial monitor. I believe you can have two instances of the IDE open on one PC. Note that, if you do this, you need to disconnect Bluetooth when you upload the revised programme.

Nick_Pyner:
On reflection, you might also note that, IF you are certain that power is not a problem, you could check what's going by moving Blueteeth to hardware serial thereby enabling you to check the serial traffic on the serial monitor. I believe you can have two instances of the IDE open on one PC. Note that, if you do this, you need to disconnect Bluetooth when you upload the revised programme.

yes i am planning to use hardware serial because that will help me pin point the problem
and yes i can have two instances of ide open
anyways, thanks a lot for helping
ill update and post whats the outcome of this attempt