Problem with HC-05 and <SoftwareSerial.h>

Good Morning All,

I'm working on a project that has two parts, a MASTER side and a SLAVE side. The MASTER side had a reed switch that when opened, an LED on the SLAVE side changes from green to red. Works just fine.

On the SLAVE side there is a button that when pressed, a relay on the MASTER side closes for a quarter of a second. This also works just fine. The problem is when the reed switch is open and a signal is being sent to the SLAVE, the push button still kind of works but not as I expect it would. If I press the button, the relay clicks a few times if at all. Sometimes I need to press it a few times to get it to work.

What I think I need to do in my code is somehow stop everything at both the MASTER and SLAVE when the button is pressed so the signal can get from the SLAVE to the MASTER uninterrupted. Below is my code and a video of what's happening. Thank you in advance.

VIDEO
Garage Door Opener Issues

MASTER CODE

#include <SoftwareSerial.h>
#define redLed    A0
#define greenLed  A1
#define blueLed   A2

SoftwareSerial mySerial(3, 2); // Rx | Tx

#define GarSwitch   4
#define relay       5

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  mySerial.begin(38400);
  pinMode(GarSwitch, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  digitalWrite(redLed, LOW);
  digitalWrite(greenLed, LOW);
  digitalWrite(blueLed, LOW);
}

void loop() {

  // Sending
  int Garage = digitalRead(GarSwitch);

  if (Garage == HIGH) {
    mySerial.print('a');
  }

  // Receiving
  if (mySerial.available())
    Serial.write(mySerial.read());    

  if (mySerial.read() == 'b') {
    digitalWrite(relay, LOW);
    delay(30);
  } else {
    digitalWrite(relay, HIGH);
  }
}

SLAVE CODE

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2); // Rx | Tx
#define redLed    7
#define greenLed  6
#define blueLed   5
#define Button    4
#define pwrLed    A0

void setup()
{
  Serial.begin(115200);
  mySerial.begin(38400);

  pinMode(pwrLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
  digitalWrite(pwrLed, HIGH);
}

void loop()
{
  // Receiving
  if (mySerial.available())
    Serial.write(mySerial.read());

  if (mySerial.read() == 'a')
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    delay(25);
  }
  else
  {
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
  }



  // Sending
  int button = digitalRead(Button);

  if (button == LOW)
  {
    mySerial.print('b');
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, LOW);
    digitalWrite(blueLed, HIGH);
  } else {
digitalWrite(blueLed, LOW);
digitalWrite(greenLed,LOW);
digitalWrite(redLed, LOW);

  }
}

From your master code
Let's assume that you received an 'b'. The first mySerial.read() will remove that from the serial buffer (and it gets printed on Serial). So that received is gone and the second mySerial.read() might very well read nothing (-1).

You should read once storing the result in a variable and next use that variable for print and react.

PS
The second mySerial.read() is not conditionally executed; it's always executed.

Thanks for the info. Is there any chance you could point me on how to do that? I use variables lightly because I don't understand them all that well.

This would be a reworked version of the snip that I quoted.

if (mySerial.available())
{
    char ch = mySerial.read();
    Serial.write(ch);    

  if (ch == 'b') {
    digitalWrite(relay, LOW);
  }
  else
  {
    ...
    ...
  }
}

You can fill in the dots :wink:

Please explain what you have difficulty with?

I guess the way you show it makes sense to me now. I've never done that before, had no formal training. I'm going to look at some sites to better understand how they work. Thanks for everything.

Just so you know, I'm a bald guy and I'm tearing my hair out LOL. So I adjusted the code accordingly (I think) and the button no longer works. I'm at a loss and there isn't a video on youtube that deals with the scenario I'm facing.

Here's The Master Code

#include <SoftwareSerial.h>
#define redLed    A0
#define greenLed  A1
#define blueLed   A2
int ch;

SoftwareSerial mySerial(3, 2); // Rx | Tx

#define GarSwitch   4
#define relay       5

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  mySerial.begin(38400);
  pinMode(GarSwitch, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  digitalWrite(redLed, LOW);
  digitalWrite(greenLed, LOW);
  digitalWrite(blueLed, LOW);
}

void loop() {

  // Sending
  int Garage = digitalRead(GarSwitch);

  if (Garage == HIGH) {
    mySerial.print('a');
  }

  // Receiving
  if (mySerial.available())
    char ch = mySerial.read();
    Serial.write(ch);

  if (ch == 'b') {
    digitalWrite(relay, LOW);
    delay(30);
  } else {
    digitalWrite(relay, HIGH);
  }
}

Please pay attention to the additional {} that I used in the previous snip. I have some doubts that your code compiles but can't check it.

Ahh, gotcha. So not it works as far as I can see the 'b' in the serial monitor but the relay is now always closed. I tried switching the LOW to HIGH and the HIGH to LOW but that didn't work. Should I be doing something in the void setup() to correct this?

@sterretje and after all that, it still buzzes the relay when the reed switch is open. Same as in the video. There has to be a function that stops the code when the button is pressed. I'm searching but coming up dry.

If you still need help, please show your update sketch.

Yeah im definitely going to need help. I played around with it when i got home but im out again. Im a truck driver for a living but I will be at it again later tonight. Thanks again.

@sterretje Good morning Sir,

As stated before, I'm a truck driver so I'm not home that often. Anyhoo, I'm back home now for the next 5 days. I'm going to play around with the code and see if I can't get it working. I'm sure I'll be needing help, just wanted to let you know that I'm back so if you'll have some time to help me, I'll be forever in your debt. Thanks a bunch in advance.

Glenn

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