Why is my code not functioning?

  1. 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");
    
  }

}
  1. 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);
 }
}

If there is data to receive, read it, and turn on the LED irrespective of what the data is.
If there's no data, turn the LED off

Is that right?

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 :wink: See About the Installation & Troubleshooting category.

No, if the slave receive 0 LED to be OFF but if the slave receive 1 LED to be ON

So, you need to compare what you have received with a '1' or a '0'.

Yes, Sure!

So, is it working now?

Please post your code, and mark the topic "solved"

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.