I trying to communicate the Mega and Nano using HC-05 Bluetooth Module.
I want to have a two-way communication so I am using four(4) HC-05 since bluetooth only communicate in one way. (Correct me if I'm wrong, I'm still learning...)
So what I did is communicate them using Hardware and Software Serial.
NANO(Hardware Serial) -------------> MEGA(Hardware Serial)
NANO(Software Serial) <------------- MEGA(Software Serial)
this is my code for Nano:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(A0, A1);
void setup() {
BTSerial.begin(38400);
Serial.begin(38400);
pinMode(A5, OUTPUT);
}
int inByte = 0;
int state = 0;
void loop() {
if ( Serial.available() > 0) {
inByte = Serial.read();
Serial.println(inByte);
}
switch (inByte) {
case '1':
digitalWrite(A5, HIGH);
delay(750);
digitalWrite(A5, LOW);
delay(750);
break;
default:
digitalWrite(A5, LOW);
BTSerial.listen();
BTSerial.write('1');
delay(750);
BTSerial.write('0');
delay(750);
break;
}
}
This code for Mega:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(12, 13);
const int buz = 7;
void setup() {
BTSerial.begin(38400);
Serial.begin(38400);
pinMode(buz, INPUT);
pinMode(11, OUTPUT);
}
int state = 0;
int inByte = 0;
void loop() {
state = digitalRead(buz);
if (state == HIGH) {
Serial.write('1');
digitalWrite(11, LOW);
delay(1000);
Serial.write('0');
}
else if (state == LOW) {
Serial.write('0');
BTSerial.listen();
if (BTSerial.available() > 0) {
inByte = BTSerial.read();
}
if (inByte == '1') {
digitalWrite(11, HIGH);
}
if (inByte != '1') {
digitalWrite(11, LOW);
}
}
}
I tried using other Baudrates but still only the Software Serial works.
like 115200 for Hardware Serial and 38400 for Software Serial.