I have 2 HC-05 modules connected with 2 UNO, one is in master role and the other slave, now I click the button on master, I could read the value altering in Serial Monitor, and the 2 HC-05 were paired - the led blinking in about 2Hz every 5 seconds, while the slave value of led State didn't changed(I have shifted PORT so as to Serial Monitor),
could anyone tell me if there's error in the code:
the Master:
// Basic Bluetooth sketch HC-05_master_38400
// Connect the HC-05 module and communicate using the serial monitor(baud rate: 9600)
//
// The HC-05 defaults to commincation mode when first powered on.
// to be placed into AT mode by pushdown and holding the "EN" button when power-on if needed.
// the default baud rate for communication mode is 38400(master - slave)
// the defualt role of HC-05 is slave.
//
//
// Pins
// BT VCC to Arduino 5V out.
// BT GND to GND
// BT RX to Arduino pin 13 (through a 1117 3.3v regulator)
// BT TX to Arduino pin 12 (no need voltage divider)
//
// LED(via 1.5kΩ) - D8
// Button(pull-up to 5v) - D7
#include <SoftwareSerial.h>
SoftwareSerial BTserial(12, 13); // RX | TX
// Connect the HC-05 TX to Arduino pin 12 RX.
// Connect the HC-05 RX to Arduino pin 13 TX through a voltage divider.
const byte ledPin = 8;
const byte buttonPin = 7;
bool ledState = false;
bool buttonState = HIGH;
//char c = ' ';
void setup()
{
// start th serial communication with the host computer
Serial.begin(9600);
Serial.println("Arduino with HC-05 is ready");
// start communication with the HC-05 using 38400
BTserial.begin(38400);
Serial.println("BTserial started at 38400");
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
ledState = digitalRead(ledPin);
buttonState = digitalRead(buttonPin);
Serial.print("buttonState = ");
Serial.println(buttonState);
// Keep reading from HC-05 and send to Arduino Serial Monitor
//if (BTserial.available())
//{
//c = BTserial.read();
//Serial.write(c);
//}
// Keep reading from Arduino Serial Monitor and send to HC-05
//if (Serial.available())
//{
// c = Serial.read();
// mirror the commands back to the serial monitor
// makes it easy to follow the commands
//Serial.write(c);
//BTserial.write(c);
//}
if(buttonState == HIGH)
BTserial.write('1');
else
BTserial.write('0');
delay(10)
}
the Slave:
// Basic bluetooth test sketch. HC-05_slave_38400
//
//
// Uses hardware serial to talk to the host computer and software serial for communication with the bluetooth module
//
// Pins
// BT VCC to Arduino 5V out.
// BT GND to GND
// BT RX to Arduino pin 13 (through a 1117 3.3v regulator)
// BT TX to Arduino pin 12 (no need voltage divider)
//
// When a command is entered in the serial monitor on the computer
// the Arduino will relay it to the bluetooth module and display the result.
//
// LED(via 1.5kΩ) - D8
// Button(pull-up to 5v) - D7
#include <SoftwareSerial.h>
SoftwareSerial BTserial(12, 13); // RX | TX
const byte ledPin = 8;
const byte buttonPin = 7;
char ledState = '1';
bool buttonState = HIGH;
void setup()
{
Serial.begin(9600);
Serial.println("Arduino with HC-06 is ready");
// HC-06 default baud rate is 9600
// HC-06 has only one role - "salve", HC-05 is capable of altering between "slave" and "master".
// HC-05 default baud rate is 38400
BTserial.begin(38400);
Serial.println("BTserial started at 38400");
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
//ledState = digitalRead(ledPin);
buttonState = digitalRead(buttonPin);
if(ledState == '1')
digitalWrite(ledPin, HIGH);
else if(ledState == '0')
digitalWrite(ledPin, LOW);
Serial.print("ledState = ");
Serial.println(ledState);
// Keep reading from HC-06 and send to Arduino Serial Monitor
if (BTserial.available())
//Serial.write(BTserial.read());
ledState = BTserial.read();
// Keep reading from Arduino Serial Monitor and send to HC-06
//if (Serial.available())
//BTserial.write(Serial.read());
delay(10);
}