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