MASTER, I have code a master and slave code for HC-05 Bluetooth module then connected my BT as slave and master well. but the code is not working. the master code is used to detect the temperature through lm35 then send signal to slave device if the temp below 35 C. here is the code for master.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,10); //BT Tx & Rx is connected to Arduino #3 & #2
int val;
int tempPin = A2;
int state = 1;
int potValue = 0;
void setup()
{
pinMode(tempPin,INPUT);
Serial.begin(9600);
//Serial.begin(9600);
mySerial.begin(38400);
//mySerial.println("AT");
// updateSerial();
}
void loop()
{
//mySerial.write(state);
val = analogRead(tempPin);
float mv = ( val/1024.0)*5000;
float cel = mv/10;
float farh = (cel*9)/5 + 32;
delay(1000);
if (cel<=35)
{
mySerial.write("1");
}
else {
mySerial.write("0");
}
}
SLAVE, The slave device code here: used to receive signal from master device then if temp below 35 LED on else off.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,10); //bt Tx & Rx is connected to Arduino #3 & #2
int state;
#define led 2
void setup() {
// put your setup code here, to run once:
mySerial.begin(38400);
Serial.begin(9600);
pinMode(led,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(mySerial.available() > 0){ // Checks whether data is comming from the serial port
state = mySerial.read(); // Reads the data from the serial port
Serial.println(state);
digitalWrite(led,HIGH);
delay(1000);
}
else{
digitalWrite(led,LOW);
}
}
You are not connecting the BT Tx and BT Rx pins to the correct Arduino pins. They go to 11 and 10, not 2 and 3.
mySerial.begin(38400);
I would use 9600 baud for software serial. It is not always reliable at 38400. Communication/data mode on the HC05's defaults to 9600. 38400 is default for AT mode. Did you reprogram the
HC05's to use 38400 in data mode?
Does the blinking pattern on the slave indicate that it is connected to the master?
As TheMemberFormerlyKnownAsAWOL says, what do you really want to happen when your slave receives from the master.
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project See About the Installation & Troubleshooting category.