@OP
You may practice the following Tutorial (tested using UNO-NANO) and then you may correct your sketch of post #1. When a Step/Section is done. put Tick Mark on it.
1. Check that your hardware setup agrees with the setup of Fig-1.
Figure-1:
2. == Slave Configuaration== by AT Commands
(1) Remove 5V from Slave-BT.
(2) Connect EN-pin of Slave-BT with 3.3V.
(3) Press and Hold the micro switch of Slave-BT.
(4) Connect 5V with Slave-BT.
(5) Wait for3/4 seconds and then release the micro switch.
(6) Check that the Red LED of Slave-BT blinks once in 2-sec indicating that the Slave-BT has entered into AT Command Mode.
(7) Upload the following sketch into Slave-UNO (UNO-2 of Fig-1).
#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11); //SRX at UNO = 10, STX = 11
void setup()
{
Serial.begin(9600);
SUART.begin(38400); //AT command Baud Rate
}
void loop()
{
if (SUART.available())
{
char x = SUART.read();
Serial.print(x);
}
if (Serial.available())
{
char c = Serial.read();
SUART.print(c);
}
}
(8) Open Serial Monitor (SM) of UNO-2 with BD = 9600 along with "Both NL & CR" option for the Line ending Tab.
(9) Enter AT+RMAAD (To clear any paired devices) in the InputBox of SM of UNO-2 and then click on Send Button. The BT will respond with OK message.
(10) Send AT+ROLE=0 (To set BT-2 as Slave) command to BT-2. The respond should be OK.
(11) Send AT+ADDR (To get the address of Slave-BT) command to BT-2. Note down the address as it will be used during Master configuration). For my BT it is: 21:13:A36B (It will be entered as: 21,11,A36B during Master configuration).
(12) Send AT+PSWD (to know password/PIN of Slave-BT). It must be same as the Master; otherwise no pairing will occur. command to Slave-BT. For my BT it is: 1234.
(13) Send AT+CMODE=1 (Slave connect mode) command to Slave-BT.
3. //===Master Configuration=== by AT Commands
(14) Bring Master-BT into AT Command Mode following the same steps of Slave-BT.
(15) Upload the following sketch into UNO-1.
#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11); //SRX at UNO = 10, STX = 11
void setup()
{
Serial.begin(9600);
SUART.begin(38400); //AT command Baud Rate
}
void loop()
{
if (SUART.available())
{
char x = SUART.read();
Serial.print(x);
}
if (Serial.available())
{
char c = Serial.read();
SUART.print(c);
}
}
(16) Send AT+RMAAD command to Master-BT.
(17) Send AT+ROLE=1 (To set it as Master) command to Master-BT.
(18) Send AT+PSWD=1234 command to Master-BT (Password/PIN should be same as Slave).
(19) Send AT+CMODE=0 (Master connect mode) command to Master-BT.
(20) Send AT+BIND=21,11,A36B command to Master-BT (the Slave address found in Step-11; the colons (: ) are replaced by commas (,)).
4. //=== End of Configuration===
(21) Remove 3.3V and 5V connections from both Master-BT and Slave-BT.
(22 Connect the 5V supplies to both BTs.
(23) Check that the BTs have been paired. The Red LEDs of both BTs blink once a while.
5. ===Checking communication between UNO-1 and UNO-2 via BTs==
Every time you press the Ledbut switch at the UNO-1 side, the blueLed at the UNO-2 side will just flash for once. And at the same time, the Serial Monitor of UNO-2 will show this message: "A Message is received from Master".
(24) Upload the following Sketch in UNO-1.
#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11); //SRX at UNO = 10, STX = 11
#define Ledbut 3
void setup()
{
Serial.begin(9600);
SUART.begin(9600);
pinMode(Ledbut, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(Ledbut) == LOW)
{
SUART.print('L');
}
delay(500);
}
(25) Upload the following sketch in UNO-2.
#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11); //SRX at UNO = 10, STX = 11
#define blueLed 13
void setup()
{
Serial.begin(9600);
SUART.begin(9600);
pinMode(blueLed, OUTPUT);
}
void loop()
{
if (SUART.available())
{
char x = SUART.read();
if (x == 'L')
{
digitalWrite(blueLed, HIGH);
delay(200);
digitalWrite(blueLed, LOW);
delay(200);
Serial.println("Message received from Master.");
}
}
}
(26) Open SM of UNO-2 with Bd = 9600.
(27) Press and release Ledbut switch at Master side.
(28) Check that the blueLed at Slave/UNO-2 side just flashes for once.
(29) Check that the SM of UNO-2 shows the following message.
A Message is received from Master.
6. ==End of Tutorial==.